【问题标题】:WebDriver/Selenium 2 doesn't find tag text properly in primefaces componentWebDriver/Selenium 2 在 primefaces 组件中找不到正确的标记文本
【发布时间】:2011-05-10 20:58:09
【问题描述】:

我使用 webdrive 编写了一个测试,如下所示:

//put stuff in the database
fillDatabaseWithParticipants();

WebDriver driver = new FirefoxDriver();
doLogin(driver);

//finds the search button and clicks it
WebElement searchButton = driver.findElement(By.xpath("/html/body/div/div[8]/div[2]/form/table/tbody/tr[5]/td/button"));
searchButton.click();

//clicks the first column of a Primefaces dataTable component to order the content in ascending order
driver.findElement(By.xpath(("/html/body/div/div[8]/div[2]/form/div/div/table/thead/tr/th"))).click();


//gets the first table division of the first table row
WebElement firstTableDivision = driver.findElement(By.xpath("/html/body/div/div[8]/div[2]/form/div/div/table/tbody/tr/td"));

Assert.assertEquals("Should be the same name", "A name inserted by me in the database", firstTableDivision.getText());

测试的作用是打开 Firefox 浏览器,进入站点并登录。然后单击页面的搜索按钮并等待数据表出现。当数据表与结果一起出现时,测试单击第一列以按升序对其进行排序。然后它执行assertEquals() 来查看名字是否确实是名字(我在测试开始时插入到数据库中的名字)。

我遇到的问题是,这个测试只有在我在调试模式下运行时才有效。如果我在 Eclipse 中使用 Run As... jUnit 测试运行它,firstTableDivision.getText() 的返回值是第一次呈现表格划分时的第一个内容。

我尝试像这样使用ExpectedCondition

ExpectedCondition e = new ExpectedCondition<Boolean>() {
          public Boolean apply(WebDriver d) {
            WebElement changingElement = d.findElement(By.xpath("/html/body/div/div[8]/div[2]/form/div/div/table/tbody/tr/td"));
            System.out.println(changingElement.getText());
            if(changingElement.getText().equalsIgnoreCase("A Name inserted by me")){
                return true;
            } else {
                return false;
            }
          }
        };

    org.openqa.selenium.support.ui.Wait<WebDriver> w = new WebDriverWait(driver, 15);
    w.until(e);

也试过driver.manage().timeouts().implicitlyWait(new Long(15), TimeUnit.SECONDS);

我知道,我在那里写的预期条件很糟糕,但这只是为了看看它是否会起作用。这两种方法的结果是一样的。返回值是 dataTable 首次呈现时的名字。

我搜索了这种问题,发现它可能是FirefoxDriver和浏览器中的DOM之间的竞争条件。

我想知道 primefaces 是否有任何可以收听的事件,我可以将其放入 ExpectedCondition 以结束比赛条件,但找不到类似的事件。

你们觉得我应该怎么做?我错过了任何解决方案吗?

【问题讨论】:

    标签: java junit automated-tests primefaces webdriver


    【解决方案1】:

    您是否尝试过在表单中​​禁用“id prepending”?

    <h:form id="myForm" prependId="false">
    

    通过禁用此功能,您可以获得您在表单元素上分配的 ID。也许这会有所帮助。 问候。

    【讨论】:

    • 我遇到的问题不是在页面中找到元素,而是它返回的元素不是我点击订购表格后的id。
    猜你喜欢
    • 2019-11-03
    • 2011-12-04
    • 2012-06-02
    • 1970-01-01
    • 1970-01-01
    • 2012-04-22
    • 1970-01-01
    • 2012-07-15
    • 1970-01-01
    相关资源
    最近更新 更多