开发者

WebDriver/Selenium 2 doesn't find tag text properly in primefaces component

开发者 https://www.devze.com 2023-03-05 10:23 出处:网络
I\'ve written a test using webdrive like this: //put stuff in the database fillDatabaseWithParticipants();

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 firs开发者_运维技巧t 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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消