0

I am automating in Java Selenium, without using Page Factory.

I have an Add Candidate Button element that is clicked in two tests. One is straightforward, after entering InviteToCompany page I click this button and proceed. However, another requires me to go past InviteToCompany page, but then use Go Back Arrow to go back to 'InviteToCompany' page and click Add Candidate Button again. This is when StaleElementReferenceException appears.

I have written such a piece of code to deal with this issue, however I am wondering, if there is a cleaner and neater way, than catching exception for second test, to proceed.

    public InviteToCompanyPO clickAddCandidateBtn() {
        try {
            getClickableElement(addCandidateBtn).click();
        } catch (StaleElementReferenceException e) {
            log.warn("StaleElementReferenceException caught, retrying...", e);
            getClickableElement(addCandidateBtn).click();
        }
        return new InviteToCompanyPO(driver);
    }

Before I had to write second test (the one causing staleness), this method simply looked like this:

    public InviteToCompanyPO clickAddCandidateBtn() {
        getClickableElement(addCandidateBtn).click();
        return new InviteToCompanyPO(driver);
    }

I tried writing something like this:

    public InviteToCompanyPO clickAddCandidateBtn() {
        wait
                .ignoring(StaleElementReferenceException.class)
                .until(ExpectedConditions.elementToBeClickable(addCandidateBtn))
                .click();
        return new InviteToCompanyPO(driver);
    }

but it doesn't work.

1 Answers1

0

I guess you are using a page factory?
Anyway, when you back to the InviteToCompany page and need to click Add Candidate Button again you will need to get that element again.
I mean to pass the locator or By of that element and get the WebElement object again with driver.findElement() method.
This causes by the fact that WebElement is actually a reference (pointer) to the actual Web Element on the page. And when you navigating from the page to other page the references to the objects on the previous page becoming invalid, Stale. When you open the previous page again it is rendered again, so you need to get the references (pointers) to elements on that page again.

Prophet
  • 32,350
  • 22
  • 54
  • 79
  • No I am not using page factory. I am using such code to add candidate, it uses 'new' everytime it visits this page: public ApplicationStep addCandidate(User user, UserType userType) { log.debug("Add candidate with the following details: {}.", user); new ApplyPO(driver) .getApplyCandidatePO() .clickAddCandidateBtn() .invite(userType) .fillData(user, true) .addCandidate(); return this; } – RafalLovesWorking Sep 11 '22 at 20:45
  • It is not so clear how that new works. – Prophet Sep 11 '22 at 21:03
  • I am not using spring, when I go back to page with this button, I create new page object, different from one that was used to procced turher when I visited that page for the first time – RafalLovesWorking Sep 13 '22 at 15:20