Web driver does have inbuilt functions to wait for the presence or even visibility of element on screen. However, at time we come across situations where non of the built in methods really work.
So here below is a custom method that is designed to use with any type of selenium webdriver frameworks. Lets have a look at the method::
public static void waitForEelement(final By locator, int timeOut) {
for (int second = 0;; second++) {
if (second >= timeOut) fail("timeout waiting for element");
try {
if ((getSelenium().getWrappedDriver().findElements(locator).size()) > 0){
break;
}
} catch (Exception e) {}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
This method can be called upon as follows:
waitForEelement(By.xpath("//div[@class='signin']"),60);
So here below is a custom method that is designed to use with any type of selenium webdriver frameworks. Lets have a look at the method::
public static void waitForEelement(final By locator, int timeOut) {
for (int second = 0;; second++) {
if (second >= timeOut) fail("timeout waiting for element");
try {
if ((getSelenium().getWrappedDriver().findElements(locator).size()) > 0){
break;
}
} catch (Exception e) {}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
This method can be called upon as follows:
waitForEelement(By.xpath("//div[@class='signin']"),60);
Thank you for the article was very helpful.
ReplyDeleteTest Automation
Thanks
DeleteI have a small question .
ReplyDeleteHow does this method waitForEelement(By.xpath("//div[@class='signin']"),60);
work if the Element changes random?
Test Automation
If it is a dynamically changing object, you need to externalize the object and store it to a variable and then pass that variable to the 'wait'
DeleteFor eg: if class name is dynamic, in this case, 'signin'. You need to get 'signin' by using getAttribute of class command and assign that to a variable say 'dynamicName' and pass it to 'wait' like below.
waitForEelement(By.xpath("//div[@class='"+dynamicName+"']"),60);
so that it will handle any value that is passed to the variable 'dynamicName'.
Hope I could clear your doubt.
Really great post, I simply unearthed your site and needed to say that I have truly appreciated perusing your blog entries.
ReplyDeleteclick here
Selenium Training in Bangalore|
Selenium Training in Chennai
This is an awesome post. Really very informative and creative contents. These concept is a good way to enhance the knowledge.
ReplyDeleteindustrial safety courses in chennai
It's a wonderful post and very helpful, thanks for all this information about software Testing.You are including better information regarding this topic in an effective way.Thank you so much.
ReplyDeleteSelenium Training in chennai | Selenium Training in annanagar | Selenium Training in omr | Selenium Training in porur | Selenium Training in tambaram | Selenium Training in velachery