只有通过Jenkins构建才能找到元素

时间:2016-02-03 10:42:59

标签: java jenkins selenium-webdriver automated-tests selenium-chromedriver

我的自动测试有问题, 我的测试通过Eclipse运行没有问题。

但是当我想在Jenkins上运行这个测试时,它总是在同一行上失败(在同一个选择器上)。

  

运行TestSuite启动ChromeDriver 2.21.371459   (36d3d07f660ff2bc1bf28a75d1cdabed0983e7c4)在21173港口   允许连接。测试运行:1,失败:1,错误:0,   跳过:0,经过的时间:34.563秒<<<失败! - 在TestSuite中   f(webdriver.Single_match_ticket)经过的时间:33.86秒<<<   失败! org.openqa.selenium.TimeoutException:在19 之后超时   等待元素可点击的秒:By.cssSelector:    a [id ='PopularOpener']> span [tittle ='All'] 构建信息:版本:   '2.50.1',修订版:'d7fc91b29de65b790abb01f3ac5f7ea2191c88a7',时间:   '2016-01-29 11:11:26'系统信息:主持人:'Ivan-HP',ip:   '192.168.221.103',os.name:'Windows 7',os.arch:'amd64',os.version:   '6.1',java.version:'1.8.0_65'驱动程序信息:   org.openqa.selenium.chrome.ChromeDriver功能   [{applicationCacheEnabled = false,rotate = false,   mobileEmulationEnabled = false,chrome = {chromedriverVersion = 2.21.371459   (36d3d07f660ff2bc1bf28a75d1cdabed0983e7c4),   userDataDir = C:\ WINDOWS \ TEMP \ scoped_dir676_8604},   takesHeapSnapshot = true,databaseEnabled = false,handlesAlerts = true,   hasTouchScreen = false,版本= 48.0.2564.97,platform = XP,   browserConnectionEnabled = false,nativeEvents = true,   acceptSslCerts = true,locationContextEnabled = true,   webStorageEnabled = true,browserName = chrome,takesScreenshot = true,   javascriptEnabled = true,cssSelectorsEnabled = true}]会话ID:   634bd38ceca35f9fabe19d967ad5d847 at   webdriver.Single_match_ticket.f(Single_match_ticket.java:85)

     

结果:

     

测试失败:Single_match_ticket.f:85»超时19后超时   几秒钟等待......

以下是我的代码的一部分:

public void f() throws Exception {

    WebDriver driver;
    System.setProperty("webdriver.chrome.driver", "webdriver\\chromedriver.exe");



    // open Google Chrome




    driver = new ChromeDriver();
    // Maximize window
    driver.manage().window().maximize();
    driver.manage().deleteAllCookies();

    WebDriverWait wait = new WebDriverWait(driver, 19);

    driver.navigate().to("TESTED SITE");

  
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("input[name='username']")));

    // Set values for username and pass
    driver.findElement(By.cssSelector("input[name='username']")).sendKeys("1testuser");
    driver.findElement(By.cssSelector("input[name='password']")).sendKeys("testtest");
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("button[type='submit']")));

    // Click on LogIn button
    driver.findElement(By.cssSelector("button[type='submit']")).click();

    wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//*[@id='userName']")));

    String Username = driver.findElement(By.xpath(".//*[@id='userName']")).getText();

    String Username1 = "1testuser";

    if (!Username1.equals(Username)) {
      throw new Exception("You are not logged in");

    } else {

      wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("div.logout-btn.balance.visible")));
      String Balance = driver.findElement(By.cssSelector("div.logout-btn.balance.visible")).getText();

      String[] parts = Balance.split("\\.");
      String part1 = parts[0];

      String secondpart = "";

      if (part1.contains(",")) {

        secondpart = part1.replace(",", "");
      } else {
        secondpart = part1;

      }

      int BalanceInt = Integer.parseInt(secondpart);

      if (BalanceInt > 200) {

        wait.until(ExpectedConditions
          .visibilityOfElementLocated(By.cssSelector("a[id='PopularOpener'] > span[title='All']")));

        driver.findElement((By.cssSelector("a[id='PopularOpener'] > span[title='All']"))).click();

        

        for (int i = 0; i < 3; i++) {

3 个答案:

答案 0 :(得分:1)

测试在此行上失败:

wait.until(ExpectedConditions
      .visibilityOfElementLocated(By.cssSelector("a[id='PopularOpener'] > span[title='All']")));

尝试选择此元素而不是像css一样选择

By.cssSelector("a[id='PopularOpener'] > span[title='All']" 

通过其他内容选择它,例如id,name,xpath。

答案 1 :(得分:1)

或者如果仅通过Jenkins失败,请检查您的本地计算机与您通过Jenkins运行测试的计算机之间的区别。 检查java版本,Chrome驱动程序,操作系统版本等。

答案 2 :(得分:0)

我使用JavascriptExecutor解决了这个问题。

JavascriptExecutor js =(JavascriptExecutor)驱动程序;
                js.executeScript( “Categories.readPopular()”);

此功能在J

上正常工作