any alternative to Thread.sleep()

2,302 views
Skip to first unread message

Tanusree Mukherjee

unread,
Aug 1, 2012, 4:53:34 PM8/1/12
to seleniu...@googlegroups.com
Can anyone advice me any alternative for Thread.sleep(0, when ever I'm taking it off, my test is failing as the element is not found.

@BeforeMethod
public static void  setUp() throws Exception {
System.out.println("Common @BeforeMethod setUp");
driver = new InternetExplorerDriver();
driver.manage().timeouts().implicitlyWait(120, TimeUnit.SECONDS);
driver.get(baseUrl);
}


I also wrote a method for delay but when ever I'm calling it , it's not giving me the alternative of Thread.sleep(),

For my test case, the scenario is , I need to select a tab from some menu , then directing to that page,then selecting item from  drop down boxes, then based on that I need to click on search button. Now  I definitely need some wait method. But I don't know what can I use as an alternative to Thread.sleep!


public static void explicitWait(final String locator, final int type) {
WebElement myDynamicElement = (new WebDriverWait(driver, 10))
 .until(new ExpectedCondition<WebElement>(){
public WebElement apply(WebDriver d) {
switch (type) {
case 1 : return d.findElement(By.cssSelector(locator));
case 2 : return d.findElement(By.xpath(locator));
case 3 : return d.findElement(By.id(locator));
case 4 : return d.findElement(By.name(locator));
case 5 : return d.findElement(By.linkText(locator));
default:
return d.findElement(By.id(locator));
}
}});
}
Any help will be highly appreciated.
--
Regards.
Tanusree

Stefan Teixeira

unread,
Aug 1, 2012, 5:38:12 PM8/1/12
to seleniu...@googlegroups.com
Use WebDriverWait, check out this post: http://automatingsoftwaretesting.wordpress.com/2011/09/17/why-webdriverwait-thread-sleep/

After you read it, check the ExpectedConditions class in the API: http://selenium.googlecode.com/svn/trunk/docs/api/java/org/openqa/selenium/support/ui/ExpectedConditions.html


Regards,
Stefan.

2012/8/1 Tanusree Mukherjee <tanumm...@gmail.com>

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To post to this group, send email to seleniu...@googlegroups.com.
To unsubscribe from this group, send email to selenium-user...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Tanusree Mukherjee

unread,
Aug 1, 2012, 5:52:33 PM8/1/12
to seleniu...@googlegroups.com
Thanks Stefan.
I'll try this solution.
Best Regards.
Tanusree
--
Regards.
Tanusree

Karthik Kulkarni

unread,
Aug 2, 2012, 1:58:38 AM8/2/12
to seleniu...@googlegroups.com
you can also use

driver.manage().timeouts().<which ever _method>(5,TimeUnits. <Time unit of your choice>)

Stefan Teixeira

unread,
Aug 2, 2012, 2:29:44 PM8/2/12
to seleniu...@googlegroups.com
The implicit wait (driver.manage().timeouts().implicitlyWait(seconds, TimeUnits.SECONDS)) is also an option. But I don't like this approach very much, because when you need to verify if an element is not present, the driver will wait for the entire time you defined to return false.


2012/8/2 Karthik Kulkarni <kulkarni...@gmail.com>:

ephraim vallejo

unread,
Aug 2, 2012, 2:57:29 PM8/2/12
to seleniu...@googlegroups.com
you can try something like this
WebElement someItem = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//")));
someItem.click();

and because it is a wait.  This waits for however long you set your wait for.

you can also try explicit waits.
Reply all
Reply to author
Forward
0 new messages