0

I have a Page Object with two ways of finding the same DOM element:

  1. First there's a wrapper for finding the parent transactionsTable, then on this wrapper I call another findElement with the rest of the xpath. This doesn't work in runtime when new instance of Page Object and the actual method is called.

  2. a separate method that uses whole xpath directly. This does work.

Of course I'd prefer to use 1st over 2nd due to reusability. Any ideas why it doesn't work?

TransactionsPage:

    public WebElement getTransactionsTable() {
    return driver.findElement(By.xpath(".//div[@class='col d-none d-lg-block']"));
}

public String getNewestTransactionAmount() {
    //this works
    return driver.findElement(By.xpath(".//div[@class='col d-none d-lg-block']/div[2]//div[@class='col-2 blue-amount text-right']")).getText();
    //this doesn't
    return getTransactionsTable().findElement(By.xpath("/div[2]//div[@class='col-2 blue-amount text-right']")).getText();
}

Of course in getNewestTransactionAmount() I'm using only one return - the above is only for example purposes.

Method is called in TransactionTest - first I call the object itself, then it's method:

    @Test(groups = {"addAndRemoveNewTransaction"})
public void tc0101_inputNewTransaction() {
    new LoginPage(driver)
            .login()
            .getNavBarComponent()
            .getAddTransactionPage()
            .addAmount(amount)
            .addDate(date)
            .selectCategory("categoryTest")
            .selectSubCategory("subCategoryTest")
            .addLocation(location)
            .selectMethod("paymentTest")
            .selectAccount("accountTest")
            .addNewTransaction();

    TransactionsPage transactionsPage = new NavBarComponent(driver)
            .getTransactionsPage();

    assertEquals(transactionsPage.getNewestTransactionAmount(), amount.toString());
}
0

This:

return getTransactionsTable().findElement(By.xpath("/div[2]//div[@class='col-2 blue-amount text-right']")).getText();

won't work because you start your xpath from /. You need to either add a . at the very beginning or remove / which will make your path relative.

Another thing is that here

return driver.findElement(By.xpath(".//div[@class='col d-none d-lg-block']/div[2]//div[@class='col-2 blue-amount text-right']")).getText();

you do not actually need to start your path with . since your search context here is the entire driver (root of DOM), not a particular part of your page.

| improve this answer | |
  • silly mistake, thank you! that helped. – oompaloompa Jun 30 at 22:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.