//Max_TimeOut variable holds the time in seconds for the maximum time to wait before the control flows to NoSuchElementException block.
int Max_TimeOut=60;
public boolean isObjExists(WebDriver driver,By locator)
{
//I am putting the code in try catch because if the object does not exist, it throws exception.
try
{
//Before throwing exception, it will wait for the Max_timeout specified
WebDriverWait wait=new WebDriverWait(driver,Max_TimeOut);
wait.until(ExpectedConditions.elementToBeClickable(locator));
//If the element found, then it returns true
return true;
}
catch(NoSuchElementException exception)
{
//If the element is not found, then it returns false
return false;
}
}
This function can be invoked in like below:
By locator=By.name("Email");
if(obj.isObjExists(driver, locator))
{
Reporter.log("Object exists");
WebElement uNameElement=driver.findElement(locator);
uNameElement.sendKeys("abcd");
}
else
{
Reporter.log("Object does not exist");
}
No comments:
Post a Comment