WebDriver/Selenium 2 在 primefaces 组件中无法正确找到标签文本

发布于 2024-11-06 10:57:29 字数 2214 浏览 4 评论 0原文

我使用 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);

我知道我在那里写的预期条件很糟糕,但这只是为了看看它是否有效。这2种方法的结果是一样的。返回的值是 dataTable 首次呈现时的第一个名称。

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

我想知道 primefaces 是否有任何类型的事件我可以监听并将其放入 ExpectedCondition 中以结束竞争条件,但无法找到类似的内容。

你们觉得我应该做什么?我是否缺少任何解决方案?

I've written a test using webdrive like this:

//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());

What the test does is it opens a Firefox browser, goes to the site and logs in. Then it clicks on the search button of the page and waits for the datatable to appear. When the datatable appears with the results, the test clicks on the first column to order it in ascending order. Then it does the assertEquals() to see if the first name is indeed the first name (one that I inserted in the database in the beginning of the test).

The problem I have is that this test only works if I run it on debug mode. If I run it with the Run As... jUnit test in Eclipse the return of firstTableDivision.getText() is the first content the table division had when it was first rendered.

I tried using an ExpectedCondition like so:

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

and also tried driver.manage().timeouts().implicitlyWait(new Long(15), TimeUnit.SECONDS);.

The expected condition I wrote up there is awful, I know, but it was only to see if it would work. The result with this 2 methods was the same. The returned value is the first name the dataTable had when it was first rendered.

I searched for this kind of problem and discovered that it probably is a race condition between the FirefoxDriver and the DOM in the browser.

I was wondering if the primefaces had any sort of event I could listen to put it in the ExpectedCondition to end the race condition but wasn't able to find anything like that.

What do you guys think I should do? I'm I missing any solution for this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

隔纱相望 2024-11-13 10:57:29

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

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

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

Have you tried disabling "id prepending" in the form?

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

By disabling this you achieve to get the id's you assigned on the form elements. Maybe that can help.
Regards.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文