Reputation: 1087
Why is my Explicit Wait not working?
My Implicit wait works but my Explicit wait doesn't seem to use the assigned timeout?
For example if i set the Explicit timeout to 300Seconds it will revert to the implicit timeout or if i comment out the implicit timeout it will throw an error / timeout exception straight away.
Code Used:
public class Base_Page extends TestListenerAdapter {
public @FindBy(css = ".ajax_loader") WebElement ajaxLoadScreen;
public @FindBy(css = "#preloaderSpinner") WebElement preloadSpinner;
public WebDriver driver;
public String packageName;
public String className;
public WebDriverWait wait;
protected JavascriptExecutor jsExecutor;
public Base_Page(WebDriver driver) throws Exception {
this.driver = driver;
this.wait = new WebDriverWait(this.driver, 300);
this.driver.manage().window().maximize();
this.driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
this.driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
Properties p = new Properties();
FileInputStream fi = new FileInputStream(Constant.CONFIG_PROPERTIES_DIRECTORY);
p.load(fi);
this.browser_type = p.getProperty("browser");
this.page_url = p.getProperty("url");
}
public void loadPage() throws Exception {
this.driver.get(page_url);
}
public void clickMyAccount() {
driver.findElement(By.xpath(".//*[@id='account_links']/li[1]/a2")).click();
}
public void clickHelp() {
this.driver.findElement(By.xpath(".//*[@id='help_links']/li[1]/a")).click();
}
Upvotes: 0
Views: 1983
Reputation: 1861
No, it is not a matter of assignment Phil. As Josh spotted correctly you have defined an explicit wait here:
this.wait = new WebDriverWait(this.driver, 300)
;
but you are not actually using it. In order to use it you need one more line just below your wait variable (I prefer to use Boolean and then decide to click or not based on the result!):
Boolean elementPresent = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//*[@id='account_links']/li[1]/a2"))).isDisplayed());
Now, you can go ahead and comment your implicit wait. So basically, after you run your code above, your program will wait for 300sec (that's a tad too much, but you might have a special requirement!) for the element denoted in the xpath to appear.
Le'ts assume that the webElement does appear after just 4 seconds have passed. Then it will immediately return TRUE (this is handy not needing to wait the whole waiting time). Otherwise, it will continue polling until the full 300 seconds have passed and then of course return FALSE. So you can manipulate the flow from then onwards, i.e.:
If(elementPresent==true){
//click element
}
else{
System.out.println("Oops! Couldn't locate element!")
}
Of course it is better to stick this wait in a method (with possible parameters, xpathLocator and timeOut) but for now here is some code, so you can verify ti works:
public void clickMyAccount() {
WebDriver wait = new WebDriverWait(driver, 300);
Boolean elementPresent = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//*[@id='account_links']/li[1]/a2"))).isDisplayed());
If(elementPresent==true){
driver.findElement(By.xpath(".//*[@id='account_links']/li[1]/a2")).click();
}
else{
System.out.println("Oops! Couldn't locate element!")
}
Hope this helps!
Upvotes: 3