jscover.maven.QUnitWebDriverRunner Maven / Gradle / Ivy
The newest version!
package jscover.maven;
import static java.lang.String.format;
import org.apache.maven.plugin.MojoFailureException;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
public class QUnitWebDriverRunner extends WebDriverRunnerBase {
public void waitForTestsToComplete(WebDriver webClient) {
new WebDriverWait(webClient, Duration.ofSeconds(timeOutSeconds)).until(ExpectedConditions.textToBePresentInElementLocated(By.id("qunit-testresult"), "tests completed"));
}
public void verifyTestsPassed(WebDriver webClient) throws MojoFailureException {
int failingCount = webClient.findElements(By.className("fail")).size();
if (failingCount != 0) {
for (String failure : getFailures(webClient))
log.error(failure);
throw new MojoFailureException("Number of failing tests: " + failingCount);
}
}
public List getFailures(WebDriver webClient) {
List failures = new ArrayList<>();
List elements = webClient.findElements(By.className("fail"));
for (WebElement element : elements) {
List descriptions = element.findElements(By.className("test-name"));
if (descriptions.size() != 1)
continue;
for (WebElement message : element.findElements(By.className("test-source"))) {
failures.add(format("%s - %s", descriptions.get(0).getText(), getTextViaJS(webClient, message)));
}
}
return failures;
}
private String getTextViaJS(WebDriver webClient, WebElement webElement) {
String script = "return arguments[0].innerText";
return (String) ((JavascriptExecutor) webClient).executeScript(script, webElement);
}
}