All Downloads are FREE. Search and download functionalities are using the official Maven repository.

jscover.maven.MochaWebDriverRunner 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.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 MochaWebDriverRunner extends WebDriverRunnerBase {

  public void waitForTestsToComplete(WebDriver webClient) {
    new WebDriverWait(webClient, Duration.ofSeconds(timeOutSeconds)).until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.className("duration")));
  }


  public void verifyTestsPassed(WebDriver webClient) throws MojoFailureException {
    if (webClient.findElements(By.className("fail")).size() != 0) {
      for (String failure : getFailures(webClient))
        log.error(failure);
      throw new MojoFailureException("Failing on test");
    }
  }

  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.tagName("h2"));
      String testName = descriptions.get(0).getText();
      for (WebElement message :element.findElements(By.className("error")))
        failures.add(format("%s - %s", testName.substring(0, testName.length()-2), message.getText().substring(0, message.getText().indexOf("\n"))));
    }
    return failures;
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy