使用selenium测试运行mvn时出错

时间:2017-05-02 18:19:06

标签: maven selenium jenkins junit xvfb

我是使用Selenium的新手,我在运行mvn verify后发现问题,我的代码中包含了我的测试。我正在使用Java Junit和webdriver。

这是我的测试代码:

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;
    }
  }
}

我使用selenium IDE为firefox生成它并将其导出为Java / Junit / Webdriver。将它集成到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进行hedless测试,但我甚至无法在本地计算机上验证它。我正在使用Ubuntu 17.10

我无法为此找到任何一步一步的实施方案。或者将测试集成到jenkins文件中以获取jenkins管道

1 个答案:

答案 0 :(得分:0)

您应该为Gecko驱动程序添加扩展名以解决其中一个错误。

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