通过硒测试运行MVN的错误



我是新来的使用硒,在运行MVN验证我的测试后,我发现了一个问题。我正在使用WebDriver使用Java Junit。

这是我的测试代码:

import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;
import org.junit.*;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
public class SeleniumTest {
  private WebDriver driver;
  private String baseUrl;
  private boolean acceptNextAlert = true;
  private StringBuffer verificationErrors = new StringBuffer();
  public static void main(String[] args) {
      System.setProperty("webdriver.gecko.driver", "/home/francop/Downloads/geckodriver");
      WebDriver driver = new FirefoxDriver();
      driver.get("http://localhost:8448/login");
      driver.quit();
  }
  @Before
  public void setUp() throws Exception {
      System.setProperty("webdriver.gecko.driver", "/home/francop/Downloads/geckodriver");
    driver = new FirefoxDriver();
    baseUrl = "http://localhost:8448/login";
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
  }
  @Test
  public void testSelenium() throws Exception {
    driver.get(baseUrl);
    driver.findElement(By.name("username")).clear();
    driver.findElement(By.name("username")).sendKeys("dbaranowski");
    driver.findElement(By.name("password")).clear();
    driver.findElement(By.name("password")).sendKeys("dbaranowski");
    driver.findElement(By.cssSelector("input[type="submit"]")).click();
    driver.findElement(By.id("searchBtn")).click();
    driver.findElement(By.id("icon")).click();
    driver.findElement(By.id("filter-button")).click();
    driver.findElement(By.id("clear-button")).click();
    driver.findElement(By.id("filter-button")).click();
    driver.findElement(By.id("clear-button")).click();
    driver.findElement(By.id("input")).click();
    driver.findElement(By.xpath("//div[@id='contentWrapper']/div/paper-listbox/paper-item")).click();
    driver.findElement(By.id("filter-button")).click();
    driver.findElement(By.id("scrim")).click();
    driver.findElement(By.xpath("(//input[@value='MORON                         '])[4]")).click();
    driver.findElement(By.cssSelector("#infoBtn > #icon")).click();
    driver.findElement(By.xpath("//paper-dialog[@id='affectedClientsDetail']/div[5]/paper-button[3]")).click();
    driver.findElement(By.xpath("(//input[@value='MORON                         '])[16]")).click();
    driver.findElement(By.cssSelector("#infoBtn > #icon")).click();
    driver.findElement(By.id("insertClientBtn")).click();
    driver.findElement(By.xpath("//vaadin-grid[@id='grid']/div/div[3]")).click();
    driver.findElement(By.xpath("(//input[@value='MORON                         '])[13]")).click();
    driver.findElement(By.cssSelector("#infoBtn > #icon")).click();
    driver.findElement(By.xpath("//vaadin-grid[@id='grid']/div/div[3]")).click();
    driver.findElement(By.xpath("(//input[@value='MORON                         '])[16]")).click();
    driver.findElement(By.cssSelector("#infoBtn > #icon")).click();
    driver.findElement(By.id("insertClientBtn")).click();
    driver.findElement(By.xpath("//vaadin-grid[@id='grid']/div/div[3]")).click();
    driver.findElement(By.xpath("(//input[@value='MORON                         '])[13]")).click();
    driver.findElement(By.cssSelector("#infoBtn > #icon")).click();
    driver.findElement(By.id("infoBtn")).click();
    driver.findElement(By.id("insertClientBtn")).click();
    driver.findElement(By.xpath("(//input[@id='input'])[13]")).clear();
    driver.findElement(By.xpath("(//input[@id='input'])[13]")).sendKeys("0387722559");
    driver.findElement(By.xpath("(//iron-icon[@id='icon'])[32]")).click();
    driver.findElement(By.xpath("//iron-collapse[@id='collapse1']/div[2]/paper-button")).click();
    driver.findElement(By.id("modifyInt")).click();
    driver.findElement(By.xpath("//paper-dialog[@id='affectedClientsDetail']/div[3]/paper-button")).click();
    driver.findElement(By.cssSelector("#exportBtn > #icon")).click();
    driver.findElement(By.cssSelector("body.layout.vertical > #exportDialog > div.buttons > paper-button.x-scope.paper-button-0")).click();
    driver.findElement(By.cssSelector("#selection-button > #icon")).click();
    driver.findElement(By.xpath("//button[@type='button']")).click();
    driver.findElement(By.id("gwt-uid-3")).click();
    driver.findElement(By.id("gwt-uid-4")).click();
    driver.findElement(By.id("gwt-uid-5")).click();
    driver.findElement(By.id("gwt-uid-6")).click();
    driver.findElement(By.id("gwt-uid-7")).click();
    driver.findElement(By.id("gwt-uid-8")).click();
    driver.findElement(By.id("gwt-uid-9")).click();
    driver.findElement(By.cssSelector("#gwt-uid-10 > span.v-on > div")).click();
    driver.findElement(By.id("gwt-uid-11")).click();
    driver.findElement(By.id("gwt-uid-12")).click();
    driver.findElement(By.id("gwt-uid-13")).click();
    driver.findElement(By.cssSelector("#gwt-uid-14 > span.v-on > div")).click();
    driver.findElement(By.cssSelector("#gwt-uid-15 > span.v-on > div")).click();
    driver.findElement(By.cssSelector("#gwt-uid-16 > span.v-on > div")).click();
    driver.findElement(By.id("gwt-uid-16")).click();
    driver.findElement(By.id("gwt-uid-15")).click();
    driver.findElement(By.id("gwt-uid-14")).click();
    driver.findElement(By.id("gwt-uid-13")).click();
    driver.findElement(By.id("gwt-uid-12")).click();
    driver.findElement(By.xpath("//button[@type='button']")).click();
    driver.findElement(By.cssSelector("#copy-button > #icon")).click();
    driver.findElement(By.id("gwt-uid-47")).click();
    driver.findElement(By.cssSelector("label.vaadin-grid.style-scope")).click();
    driver.findElement(By.cssSelector("#selection-button > #icon")).click();
    driver.findElement(By.id("icon")).click();
    driver.findElement(By.xpath("//div[@id='tabsContent']/paper-tab[2]/div")).click();
    driver.findElement(By.id("icon")).click();
    driver.findElement(By.id("paperToggle")).click();
    driver.findElement(By.xpath("//div[@id='tabsContent']/paper-tab[2]/div")).click();
    driver.findElement(By.id("paperToggle")).click();
    driver.findElement(By.xpath("(//div[@id='checkboxContainer'])[23]")).click();
    driver.findElement(By.xpath("(//iron-icon[@id='icon'])[10]")).click();
    driver.findElement(By.xpath("(//div[@id='buttons']/paper-checkbox)[3]")).click();
    driver.findElement(By.xpath("(//div[@id='buttons']/paper-checkbox)[2]")).click();
    driver.findElement(By.xpath("(//div[@id='checkboxContainer'])[20]")).click();
    driver.findElement(By.xpath("(//iron-icon[@id='icon'])[9]")).click();
    driver.findElement(By.xpath("//div[@id='tabsContent']/paper-tab/div")).click();
    driver.findElement(By.id("filter-button")).click();
    driver.findElement(By.id("scrim")).click();
    driver.findElement(By.id("searchBtn")).click();
    driver.findElement(By.cssSelector("#logoutBtn > #icon")).click();
    driver.findElement(By.cssSelector("#logoutDialog > div.buttons > paper-button.x-scope.paper-button-0")).click();
    driver.findElement(By.name("username")).clear();
    driver.findElement(By.name("username")).sendKeys("");
    driver.findElement(By.name("password")).clear();
    driver.findElement(By.name("password")).sendKeys("");
  }
  @After
  public void tearDown() throws Exception {
    driver.quit();
    String verificationErrorString = verificationErrors.toString();
    if (!"".equals(verificationErrorString)) {
      fail(verificationErrorString);
    }
  }
  private boolean isElementPresent(By by) {
    try {
      driver.findElement(by);
      return true;
    } catch (NoSuchElementException e) {
      return false;
    }
  }
  private boolean isAlertPresent() {
    try {
      driver.switchTo().alert();
      return true;
    } catch (NoAlertPresentException e) {
      return false;
    }
  }
  private String closeAlertAndGetItsText() {
    try {
      Alert alert = driver.switchTo().alert();
      String alertText = alert.getText();
      if (acceptNextAlert) {
        alert.accept();
      } else {
        alert.dismiss();
      }
      return alertText;
    } finally {
      acceptNextAlert = true;
    }
  }
}

我使用硒IDE来生成Firefox,并将其导出为Java/Junit/Web Driver。将其集成到src/test/java/中,并将必要依赖性添加到我的pom.xml

当我在终端MVN中运行时,验证它会加载测试并以失败的方式完成:

Failed tests: 
SeleniumTest.testSelenium on testSelenium(SeleniumTest)(SeleniumTest)
  Run 1: SeleniumTest.setUp:29 » IllegalAccess tried to access class org.openqa.seleniu...
  Run 2: SeleniumTest.tearDown:130 NullPointer

这是完整的错误日志:

java.lang.IllegalAccessError: tried to access class org.openqa.selenium.os.ExecutableFinder from class org.openqa.selenium.firefox.FirefoxBinary
    at org.openqa.selenium.firefox.FirefoxBinary.locateFirefoxBinariesFromPlatform(FirefoxBinary.java:412)
    at org.openqa.selenium.firefox.FirefoxBinary.<init>(FirefoxBinary.java:108)
    at java.util.Optional.orElseGet(Optional.java:267)
    at org.openqa.selenium.firefox.FirefoxDriver.toExecutor(FirefoxDriver.java:204)
    at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:108)
    at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:104)
    at SeleniumTest.setUp(SeleniumTest.java:29)
testSelenium on testSelenium(SeleniumTest)(SeleniumTest)  Time elapsed: 0.184 sec  <<< FAILURE!
java.lang.NullPointerException: null
    at SeleniumTest.tearDown(SeleniumTest.java:130)

还试图使用XVFB使用无防测试,但我甚至无法在本地机器中验证它。我正在使用Ubuntu 17.10

我无法为此找到任何逐步实现。或将测试集成到Jenkins Pipeline

的Jenkinsfile中

您应该在壁虎驱动程序中添加扩展名,以解决其中一个错误。

  System.setProperty("webdriver.gecko.driver", "/home/francop/Downloads/geckodriver.exe");
  WebDriver driver = new FirefoxDriver();

最新更新