Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/scroll modifier #394

Merged
merged 20 commits into from
Oct 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ All notable changes to AET will be documented in this file.

## Unreleased
**List of changes that are finished but not yet released in any final version.**
- [PR-394](https://github.com/Cognifide/aet/pull/394) Added scroll modifier
- [PR-399](https://github.com/Cognifide/aet/pull/399) Bug fix on side panel - conditional tests ([#398](https://github.com/Cognifide/aet/issues/398))
- [PR-396](https://github.com/Cognifide/aet/pull/396) Added horizontal scrollbar for wide pages ([#393](https://github.com/Cognifide/aet/issues/393))
- [PR-403](https://github.com/Cognifide/aet/pull/403) Conditionally passed tests can be accepted ([#400](https://github.com/Cognifide/aet/issues/400))
Expand All @@ -22,7 +23,6 @@ All notable changes to AET will be documented in this file.

## Version 3.0.0


- [PR-359](https://github.com/Cognifide/aet/pull/359) ([#351](https://github.com/Cognifide/aet/issues/351)) Disabled nu.validator language detection to fix random NPE
- [PR-328](https://github.com/Cognifide/aet/pull/328) Added suite's history
- [PR-303](https://github.com/Cognifide/aet/pull/303) Added `exclude-elements` parameter to [ScreenCollector](https://github.com/Cognifide/aet/wiki/ScreenCollector)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,47 +20,49 @@
import com.cognifide.aet.job.api.collector.CollectorProperties;
import com.cognifide.aet.job.api.exceptions.ParametersException;
import com.cognifide.aet.job.api.exceptions.ProcessingException;
import com.cognifide.aet.job.common.utils.javascript.JavaScriptJobExecutor;
import com.cognifide.aet.vs.ArtifactsDAO;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
import org.apache.commons.io.Charsets;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.osgi.framework.BundleContext;

public class AccessibilityCollector implements CollectorJob {

private static final String DOCUMENT_OUTER_HTML_SCRIPT = "return document.documentElement.outerHTML;";
public static final String NAME = "accessibility";

private static final String DOCUMENT_OUTER_HTML_SCRIPT = "return document.documentElement.outerHTML;";
private static final String PARAM_STANDARD = "standard";
private static final String DEFAULT_STANDARD = "WCAG2AA";

private final ArtifactsDAO artifactsDAO;
private final WebDriver webDriver;
private final BundleContext context;
private final CollectorProperties properties;
private final JavaScriptJobExecutor jsExecutor;

private String standard = DEFAULT_STANDARD;

public AccessibilityCollector(ArtifactsDAO artifactsDAO, CollectorProperties collectorProperties,
WebDriver webDriver, BundleContext context) {
AccessibilityCollector(ArtifactsDAO artifactsDAO, CollectorProperties collectorProperties,
JavaScriptJobExecutor jsExecutor, BundleContext context) {
this.artifactsDAO = artifactsDAO;
this.webDriver = webDriver;
this.context = context;
this.properties = collectorProperties;
this.jsExecutor = jsExecutor;
}

@Override
public CollectorStepResult collect() throws ProcessingException {
String script = getScriptFromFile();
final String html = (String) ((JavascriptExecutor) webDriver)
.executeScript(DOCUMENT_OUTER_HTML_SCRIPT);
final String json = (String) ((JavascriptExecutor) webDriver).executeScript(script, standard);
final String html = jsExecutor.execute(DOCUMENT_OUTER_HTML_SCRIPT)
.getExecutionResultAsString();
final String json = jsExecutor.execute(script, standard).getExecutionResultAsString();
List<AccessibilityIssue> issues = parseIssues(json);
getElementsPositions(issues, html);

Expand All @@ -74,7 +76,7 @@ private String getScriptFromFile() throws ProcessingException {
try {
URL entry = context.getBundle().getEntry("/collectors/accessibility/htmlcs.min.js");
if (entry != null) {
script = IOUtils.toString(entry.openStream(), Charsets.UTF_8);
script = IOUtils.toString(entry.openStream(), StandardCharsets.UTF_8);
} else {
throw new ProcessingException("Can't find accessibility htmlcs scripts in bundle context!");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.cognifide.aet.job.api.collector.CollectorProperties;
import com.cognifide.aet.job.api.collector.WebCommunicationWrapper;
import com.cognifide.aet.job.api.exceptions.ParametersException;
import com.cognifide.aet.job.common.utils.javascript.JavaScriptJobExecutor;
import com.cognifide.aet.vs.ArtifactsDAO;
import java.util.Map;
import org.osgi.framework.BundleContext;
Expand All @@ -44,7 +45,7 @@ public String getName() {
public CollectorJob createInstance(CollectorProperties properties, Map<String, String> parameters,
WebCommunicationWrapper webCommunicationWrapper) throws ParametersException {
AccessibilityCollector collector = new AccessibilityCollector(artifactsDAO, properties,
webCommunicationWrapper.getWebDriver(), context);
new JavaScriptJobExecutor(webCommunicationWrapper.getWebDriver()), context);
collector.setParameters(parameters);
return collector;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import com.cognifide.aet.job.api.collector.WebCommunicationWrapper;
import com.cognifide.aet.job.api.exceptions.ParametersException;
import com.cognifide.aet.job.api.exceptions.ProcessingException;
import com.cognifide.aet.job.common.utils.JsRuntimeWrapper;
import com.cognifide.aet.job.common.utils.javascript.JsRuntimeWrapper;
import com.cognifide.aet.vs.ArtifactsDAO;
import java.io.IOException;
import java.io.StringWriter;
Expand All @@ -47,7 +47,7 @@ public class ClientSidePerformanceCollector implements CollectorJob {

private final BundleContext bundleContext;

public ClientSidePerformanceCollector(ArtifactsDAO artifactsDAO,
ClientSidePerformanceCollector(ArtifactsDAO artifactsDAO,
WebCommunicationWrapper webCommunicationWrapper,
CollectorProperties collectorProperties, BundleContext bundleContext) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,11 @@
import com.cognifide.aet.job.api.collector.CollectorJob;
import com.cognifide.aet.job.api.exceptions.ParametersException;
import com.cognifide.aet.job.api.exceptions.ProcessingException;
import java.io.UnsupportedEncodingException;
import com.cognifide.aet.job.common.utils.javascript.JavaScriptJobExecutor;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.StringUtils;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -39,26 +38,30 @@ public class ExecuteJavaScriptModifier implements CollectorJob {
private static final String BASIC_AUTH_USERNAME = "basicAuthUsername";
private static final String BASIC_AUTH_PASSWORD = "basicAuthPassword";

private final WebDriver webDriver;
private final ExternalSnippetHttpClient httpClient;
private final JavaScriptJobExecutor jsExecutor;

private String cmd;
private String snippetUrl;
private String basicAuth;

ExecuteJavaScriptModifier(WebDriver webDriver, ExternalSnippetHttpClient httpClient) {
this.webDriver = webDriver;
ExecuteJavaScriptModifier(ExternalSnippetHttpClient httpClient,
JavaScriptJobExecutor jsExecutor) {
this.httpClient = httpClient;
this.jsExecutor = jsExecutor;
}

@Override
public CollectorStepResult collect() throws ProcessingException {
final String jsSnippet = getJsSnippet();
if (LOG.isDebugEnabled()) {
LOG.debug("Executing JavaScript command: {} on page: {}", jsSnippet,
webDriver.getCurrentUrl());
CollectorStepResult result;
String jsSnippet = getJsSnippet();
try {
jsExecutor.execute(jsSnippet);
result = CollectorStepResult.newModifierResult();
} catch (ProcessingException ex) {
result = CollectorStepResult.newProcessingErrorResult(ex.getMessage());
}
return javaScriptElement(webDriver, jsSnippet);
return result;
}

private String getJsSnippet() throws ProcessingException {
Expand Down Expand Up @@ -102,29 +105,9 @@ String encodeBasicAuth(String username, String password) {

String authString = usernameOrEmpty + ":" + passwordOrEmpty;
LOG.debug("authentication: '{}'", authString);
byte[] encoded = new byte[0];
try {
encoded = Base64.encodeBase64(authString.getBytes("UTF-8"));
} catch (UnsupportedEncodingException uee) {
LOG.error("Unsupported encoding: UTF-8", uee);
}
byte[] encoded = Base64.encodeBase64(authString.getBytes(StandardCharsets.UTF_8));
LOG.debug("encoded bytes: '{}'", encoded);
return new String(encoded);
}

private CollectorStepResult javaScriptElement(WebDriver driver, String jsSnippet) {
CollectorStepResult result;
try {
((JavascriptExecutor) driver).executeScript(jsSnippet);
result = CollectorStepResult.newModifierResult();
} catch (Exception e) {
final String message = String
.format("Can't execute JavaScript command. jsSnippet: \"%s\". Error: %s ",
jsSnippet, e.getMessage());
result = CollectorStepResult.newProcessingErrorResult(message);
LOG.warn(message, e);
}
return result;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.cognifide.aet.job.api.collector.CollectorProperties;
import com.cognifide.aet.job.api.collector.WebCommunicationWrapper;
import com.cognifide.aet.job.api.exceptions.ParametersException;
import com.cognifide.aet.job.common.utils.javascript.JavaScriptJobExecutor;
import java.util.Map;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
Expand All @@ -39,7 +40,7 @@ public String getName() {
public CollectorJob createInstance(CollectorProperties properties, Map<String, String> parameters,
WebCommunicationWrapper webCommunicationWrapper) throws ParametersException {
ExecuteJavaScriptModifier modifier = new ExecuteJavaScriptModifier(
webCommunicationWrapper.getWebDriver(), httpClient);
httpClient, new JavaScriptJobExecutor(webCommunicationWrapper.getWebDriver()));
modifier.setParameters(parameters);
return modifier;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@
import com.cognifide.aet.job.api.exceptions.ProcessingException;
import com.cognifide.aet.job.common.SeleniumWaitHelper;
import com.cognifide.aet.job.common.modifiers.WebElementsLocatorParams;
import com.cognifide.aet.job.common.utils.javascript.JavaScriptJobExecutor;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.BooleanUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.TimeoutException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverException;
Expand All @@ -39,24 +39,22 @@ public class HideModifier extends WebElementsLocatorParams implements CollectorJ
public static final String NAME = "hide";

private static final Logger LOG = LoggerFactory.getLogger(HideModifier.class);

private static final String LEAVE_BLANK_SPACE_PARAM = "leaveBlankSpace";

private static final boolean LEAVE_BLANK_SPACE_DEFAULT = true;

private static final String DISPLAY_NONE_SCRIPT = "arguments[0].style.display='none'";

private static final String VISIBILITY_FALSE_SCRIPT = "arguments[0].style.visibility='hidden'";

private final WebDriver webDriver;

private final CollectorProperties properties;
private final JavaScriptJobExecutor jsExecutor;

private boolean leaveBlankSpace;

HideModifier(WebDriver webDriver, CollectorProperties properties) {
HideModifier(WebDriver webDriver, CollectorProperties properties,
JavaScriptJobExecutor jsExecutor) {
this.webDriver = webDriver;
this.properties = properties;
this.jsExecutor = jsExecutor;
}

@Override
Expand All @@ -78,17 +76,10 @@ public void setParameters(Map<String, String> params) throws ParametersException

private CollectorStepResult hideElement(By locator, boolean leaveBlankSpace)
throws ProcessingException {

CollectorStepResult result;
try {
String script = retrieveHidingScript(leaveBlankSpace);

SeleniumWaitHelper
.waitForElementToBePresent(webDriver, locator, getTimeoutInSeconds());

List<WebElement> webElements = webDriver.findElements(locator);
for (WebElement element : webElements) {
((JavascriptExecutor) webDriver).executeScript(script, element);
}
hideElements(locator, leaveBlankSpace);
result = CollectorStepResult.newModifierResult();
} catch (TimeoutException e) {
final String message =
Expand All @@ -102,12 +93,22 @@ private CollectorStepResult hideElement(By locator, boolean leaveBlankSpace)
locator.toString(), e.getMessage());
result = CollectorStepResult.newProcessingErrorResult(message);
LOG.warn(message, e);
} catch (Exception e) {
throw new ProcessingException("Can't hide element by " + locator.toString(), e);
}
return result;
}

private void hideElements(By locator, boolean leaveBlankSpace) throws ProcessingException {
String script = retrieveHidingScript(leaveBlankSpace);

SeleniumWaitHelper
.waitForElementToBePresent(webDriver, locator, getTimeoutInSeconds());

List<WebElement> webElements = webDriver.findElements(locator);
for (WebElement element : webElements) {
jsExecutor.execute(script, element);
}
}

private String retrieveHidingScript(boolean leaveBlankSpace) {
String script;
if (leaveBlankSpace) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
import com.cognifide.aet.job.api.collector.CollectorProperties;
import com.cognifide.aet.job.api.collector.WebCommunicationWrapper;
import com.cognifide.aet.job.api.exceptions.ParametersException;
import com.cognifide.aet.job.common.utils.javascript.JavaScriptJobExecutor;
import java.util.Map;
import org.openqa.selenium.WebDriver;
import org.osgi.service.component.annotations.Component;

@Component
Expand All @@ -34,7 +36,9 @@ public String getName() {
@Override
public CollectorJob createInstance(CollectorProperties properties, Map<String, String> parameters,
WebCommunicationWrapper webCommunicationWrapper) throws ParametersException {
HideModifier modifier = new HideModifier(webCommunicationWrapper.getWebDriver(), properties);
WebDriver webDriver = webCommunicationWrapper.getWebDriver();
HideModifier modifier = new HideModifier(webDriver, properties,
new JavaScriptJobExecutor(webDriver));
modifier.setParameters(parameters);
return modifier;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
package com.cognifide.aet.job.common.modifiers.login;

import com.cognifide.aet.job.api.exceptions.ProcessingException;
import com.cognifide.aet.job.common.utils.javascript.JavaScriptJobExecutor;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.TimeoutException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
Expand All @@ -27,25 +27,20 @@
public class LoginFormComponent {

private static final int TIMEOUT = 10;

private static final int MAX_ATTEMPTS = 10;

private static final String VALUE_ATTRIBUTE = "value";

private static final String SET_VALUE_SCRIPT = "arguments[0].value=arguments[1]";

private final WebElement loginInput;

private final WebElement passwordInput;

private final WebElement submitButton;
private final JavaScriptJobExecutor jsExecutor;

private final JavascriptExecutor js;
LoginFormComponent(WebDriver webDriver, String loginInputSelector,
String passwordInputSelector, String submitButtonSelector,
JavaScriptJobExecutor jsExecutor) throws ProcessingException {

public LoginFormComponent(WebDriver webDriver, String loginInputSelector,
String passwordInputSelector,
String submitButtonSelector) throws ProcessingException {
js = (JavascriptExecutor) webDriver;
this.jsExecutor = jsExecutor;
try {
loginInput = getElementByXpath(webDriver, loginInputSelector);
passwordInput = getElementByXpath(webDriver, passwordInputSelector);
Expand Down Expand Up @@ -74,7 +69,7 @@ public void login(String login, String password) throws ProcessingException {
submit();
}

public boolean isFormFilledProperly(String login, String password) {
private boolean isFormFilledProperly(String login, String password) {
return isFilledProperly(loginInput, login) && isFilledProperly(passwordInput, password);
}

Expand All @@ -86,9 +81,9 @@ private void submit() {
submitButton.click();
}

private void fill(WebElement input, String value) {
private void fill(WebElement input, String value) throws ProcessingException {
if (!isFilledProperly(input, value)) {
js.executeScript(SET_VALUE_SCRIPT, input, value);
jsExecutor.execute(SET_VALUE_SCRIPT, input, value);
}
}

Expand Down
Loading