Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best approach for Timeout using Selenium using Webdriver

I have run into a problem. My web page has a DropDownList control. Once the DropDownList value changes (by selecting a different value), the page refreshes and it renders the contents.

And then I have to use Thread.Sleep(2000); before it goes and FindElement.

My question: What is the best way to wait till the page loads?

I have so many instances of Thread.Sleep(2000) in my code that I am beginning to think this is not the best way to approach the problem.

Here is my code:

[TestInitialize()]
public void Setup()
{
    if (BaseIntegrationTest.browserType.Equals(BaseIntegrationTest.IE))
    {
        driver = new InternetExplorerDriver();
    }
    else if (BaseIntegrationTest.browserType.Equals(BaseIntegrationTest.CHROME))
    {
        //driver = new ChromeDriver();
    }
    else if (BaseIntegrationTest.browserType.Equals(BaseIntegrationTest.FIREFOX))
    {
        driver = new FirefoxDriver();
    }
}

And the second part:

[TestMethod]
public void testVerifyData()
{
    // ...................
    // ...................
    driver.FindElement(By.XPath("//*[@id='ctl00_NavigationControl1_lnke']")).Click();

    Thread.Sleep(2000);

    //select from the dropdownlist.
    IWebElement catagory = driver.FindElement(By.Id("ctl00_ContentPlaceHolder1_Filter"));
    SelectElement selectCatagory = new SelectElement(catagory);
    selectCatagory.SelectByText("Employee");

    Thread.Sleep(2000);
    // ...................
    // ...................
}
like image 493
Nick Kahn Avatar asked Nov 23 '25 10:11

Nick Kahn


2 Answers

Thread.Sleep() is a very discouraged way to implement your waits

This code is outlined on the selenium documentation http://seleniumhq.org/docs/04_webdriver_advanced.html

WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
IWebElement category = wait.Until<IWebElement>((d) =>
    {
        return d.FindElement(By.Id("ctl00_ContentPlaceHolder1_Filter"));
    });

That is an example of an explicit wait where selenium will not execute any actions until your element is found

An example of an implicit wait is:

driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));
IWebElement category = driver.FindElement(By.Id("ctl00_ContentPlaceHolder1_Filter"));

In implicit waits the driver will wait for a given amount of time and poll the DOM for any elements that do not exist.

EDIT

public WaitForElement(string el_id)
{
    WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
    IWebElement category = wait.Until<IWebElement>((d) =>
    {
        return d.FindElement(By.Id(el_id));
    });
}
like image 124
Greg Avatar answered Nov 26 '25 01:11

Greg


I solve this problem by using WebDriverWait too.

But I set wait till the last element shown up (ex: the footer, the last item in the list).

WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
wait.Until(d => d.FindElement(By.Id("footer")).Displayed);

or

WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
wait.Until(d => d.FindElements(By.TagName("li")).Last().Displayed);
like image 39
lct_005 Avatar answered Nov 25 '25 23:11

lct_005



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!