-1

Implicit and Explicit wait don't work.
How to fix it?
How to use FluentWait in PageFactory, so it would not be needed to use locator in the test.
Aim not to use Thread.sleep at all.

Tools used: Selenium, TestNG, WebDriverManager
Website is made on AngularJS.

public class LoginPage {

    private WebDriver driver;

    public StudioMenuPage(WebDriver driver) {
        this.driver = driver;
        PageFactory.initElements(driver, this);
    }

    @FindBy(xpath = "//div[@class='login']")
    private WebElement loginButton;

    public WebElement getLoginButton() {
        return loginButton;
    }
}


public class TestBase {
public static WebDriver driver = null;

@BeforeTest()
public void initialize() {
ChromeOptions options = new ChromeOptions();
options.addArguments("--headless");
options.addArguments("--no-sandbox");
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver(options);
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
    }
}


public class LoginTest extends TestBase {

LoginPage loginPage;    

@Test
private void makeLogin() {

loginPage = new LoginPage(driver);

// Does not work with Implicit Wait:
/* 
loginPage.getLoginButton().click;
*/


// Works with Thread.sleep:
/* 
Thread.sleep(4000);
loginPage.getLoginButton().click;
*/

// Does not work with Explicit Wait:
/* 
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(loginPage.getLoginButton()));
loginPage.getLoginButton().click;
*/

// Works with FluentWait:
/*  
new FluentWait<>(driver).withTimeout(Duration.ofSeconds(5)).pollingEvery(Duration.ofMillis(500))
    .ignoring(WebDriverException.class)
    .until(d -> {
                    WebElement el = d.findElement(By.xpath("//div[@class='login']"));
                    el.click();
                    return el;
                });
*/
}

If Implicit and Explicit waiters are used, following error occurs:

org.openqa.selenium.WebDriverException: unknown error: Element <div class="login">...</div> is not clickable at point (225, 334). Other element would receive the click: <div id="cdk-overlay-0" class="cdk-overlay-pane" dir="ltr" style="pointer-events: auto; top: 316px; left: 201.5px;">...</div>
  (Session info: headless chrome=73.0.3683.86)
  (Driver info: chromedriver=2.46.628411 (3324f4c8be9ff2f70a05a30ebc72ffb013e1a71e),platform=Mac OS X 10.14.4 x86_64) (WARNING: The server did not provide any stacktrace information)
Artur
  • 661
  • 1
  • 6
  • 23
  • If you read the error message, it tells you exactly what happened. You tried to click on ` – JeffC May 01 '19 at 15:27
  • Oh... and claiming that `Implicit and Explicit wait don't work` is a bit silly. Everyone else is able to use them just fine so they are working, you are just using them incorrectly. – JeffC May 01 '19 at 15:27

1 Answers1

2

First: We shall not mix implicit and explicit waits! Doing so can cause unpredictable wait times.

Advise you to use PageFactory with AjaxElementLocatorFactory which will wait up to specified seconds for each element ANYTIME they are accessed and it ignores the CacheLookup tag.

PageFactory.initElements(new AjaxElementLocatorFactory(driver, 15), this);

If the element is not found in the given time interval, it will throw NoSuchElementException exception.

Below example shows how to implement FluentWait in Page Factory -

protected synchronized void waitForElementVisibilityAndClick(WebElement element, int timeOut, String elementName) {
    protected static Wait<WebDriver> wait = null;
    try {
        wait = new FluentWait<WebDriver>((WebDriver) driver).withTimeout(timeOut, TimeUnit.SECONDS).pollingEvery(1,
                TimeUnit.SECONDS);
        wait.until(ExpectedConditions.visibilityOf(element));
        element.click();
    }catch(Exception e) {
    }
}
TheSociety
  • 1,936
  • 2
  • 8
  • 20