1

In Page Object factory:

By popup=By.xpath("//button[test()='NO THANKS']");

public List<WebElement> getPopUpSize(){
return driver.findElements(popup);
}

public WebElement getPopUp(){
return driver.findElement(popup);
}

Calling above methods into Testcase:

LandingPage l = new LandingPage(driver);
if(l.getPopUpSize().size()>0)
{
l.getPopUp().click();
}

I didn't understand why do we have to create a list just to cancel single pop up?

Jackie M
  • 65
  • 7
  • I'm not sure what you're asking. Do you not understand why you need to check for the presence of the popup? Or do you not understand why `findElements` returns a `List`? Or something else? – tgdavies Apr 24 '22 at 05:17
  • Can you provide more info, on exactly what isn't working, and what the expected output is? – Alicia Sykes Apr 24 '22 at 15:15

1 Answers1

1

No, you do not need findElements for a single web element. Use findElement instead or Explicit waits as illustrated below:

  1. Using ExplicitWaits

Code:

public WebElement getPopUpWebElement(){
    return driver.findElement(popup);
}

and in test method:

    try {
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(30));
        wait.until(ExpectedConditions.elementToBeClickable(getPopUpWebElement())).click();
    }
    catch(Exception e){
        System.out.println("Could not click on pop up");
        e.printStackTrace();
    }
cruisepandey
  • 28,520
  • 6
  • 20
  • 38