Pasha
Pasha

Reputation: 279

WebDriverWait. Fast check element exist or not exist

I got a problem and I don't know how to solve it right. Situation: user enters login and password, and then he could be at one of two pages. Question is: how to check correctly in which page we are?

1. I want to use WebDriverWait, so my implicitlyWait = 0 ms,

2. I use Page Object pattern, pages were initialized with AjaxElementLocatorFactory

a. So, if I'll do method for check some element like this:

@FindBy(id = "pushOutMessage")
private WebElement messageText;

public boolean pageIsPresent() {
    return messageText.isDisplayed();
}

It will be not right because if page is wrong, then WebDriver will wait N seconds for this element. So it makes my simple tests slow, very-very slow.

b. If I will check element with "findElement" - my implicity waits is 0 ms, so if element was slowly-loaded pageIsPresent returns false, even if page is right.

I hope there is some other way to do this. Need your help!

Upvotes: 2

Views: 699

Answers (2)

Vivek Singh
Vivek Singh

Reputation: 3649

After the user enters or clicks submit or login button you can do something like this:

public Object clickLogin() {
    loginElement.click();
    try{
        new WebDriverWait(driver, 10).until(ExpectedConditions.presenceOfElementedLocatedBy(By.id("pushOutMessage")));
    return PageFactory.initElements(driver, FirstPage.class);
    } catch (TimeoutException te) {
        return PageFactory.initElements(driver, SecondPage.class);
    }
}

Upvotes: 0

Saifur
Saifur

Reputation: 16201

There are multiple ways you can do that. but the easiest way will be to check the elemenet counts

I would rather do

@FindBy(id = "pushOutMessage")
private List<WebElement> elements;

public int pageIsPresent() {
    return elements.size();
}

And, somewhere test the pageIsPresent() is 0 or greater. if greater than 0 we know the page element was returned

And, since you are using pageobject pattern and Java I would recommand you to create an overloading to the baseClass which will check for a selector everytime you instantiate a new pageobject. I have a git repo here with TestNG. that might help

Upvotes: 1

Related Questions