Search code examples
c#google-chromeseleniumthread-sleepautomated-tests

How do I Avoid using Thread.Sleep with C# Selenium 2.44 Chrome Driver?


Here are the details of my Development Environment:

-Visual Studio 2012 Ultimate with Update 4

-Google Chrome Version 38.0.2125.111 m

-Windows 7 Professional with 32-bit Operating System

-Selenium .NET 2.44.0

-NUnit

With Selenium 2.44.0's Firefox driver, the automated UI works quickly and properly. Moreover, I did Not have to use any Thread.Sleep with the C# code that uses the Selenium 2.44.0's Firefox driver

Sample C# code that uses Selenium 2.44.0's Firefox driver

       IWait<IWebDriver> wait = new OpenQA.Selenium.Support.UI.WebDriverWait(driver, TimeSpan.FromSeconds(30.00));
        wait.Until(driver1 => ((IJavaScriptExecutor)drvArg).ExecuteScript("return       document.readyState").Equals("complete"));
                 String find1stLnkXpath   = "//div[@class='jarviswidget jarviswidget-sortable']/descendant::div[@role='content']/descendant::div[@id='grid-container']/descendant::table[@class='k-selectable' and @role='grid']/tbody/tr[1]/td/descendant::a[@class='k-button k-button-icontext k-grid-view']";
        Thread.Sleep(threadSleepTimeArg);
        wait.Until(drv => (true == Exists(drv, By.XPath(find1stLnkXpath))));
        wait.Until(drv => isClickable(drv, By.XPath(find1stLnkXpath)));

        wait.Until(drv => (true == Exists(drv, By.XPath(find1stLnkXpath))));
        // k-button k-button-icontext k-grid-view
        // class="k-selectable" role="grid"
        IWebElement viewCustDetlnk = wait.Until(ElementIsClickable(By.XPath(find1stLnkXpath)));
        viewCustDetlnk.Click();

Sadly, the Selenium 2.44.0's Chrome Driver ( Chromedriver Win32 2.12 ) runs slowly, and I am forced to use Thread.Sleep with the C# code that uses the Selenium 2.44.0's Chrome Driver in order to ensure that the automated UI works properly.

Sample C# code that uses Selenium 2.44.0's Chrome Driver ( Chromedriver Win32 2.12 )

 IWait<IWebDriver> wait = new OpenQA.Selenium.Support.UI.WebDriverWait(driver, TimeSpan.FromSeconds(30.00));



            wait.Until(driver1 => ((IJavaScriptExecutor)drvArg).ExecuteScript("return       document.readyState").Equals("complete"));
                     String find1stLnkXpath   = "//div[@class='jarviswidget jarviswidget-sortable']/descendant::div[@role='content']/descendant::div[@id='grid-container']/descendant::table[@class='k-selectable' and @role='grid']/tbody/tr[1]/td/descendant::a[@class='k-button k-button-icontext k-grid-view']";

           Thread.Sleep(3000);

            wait.Until(drv => (true == Exists(drv, By.XPath(find1stLnkXpath))));
            wait.Until(drv => isClickable(drv, By.XPath(find1stLnkXpath)));

            wait.Until(drv => (true == Exists(drv, By.XPath(find1stLnkXpath))));

             IWebElement viewCustDetlnk = wait.Until(ElementIsClickable(By.XPath(find1stLnkXpath)));
            viewCustDetlnk.Click();

Please tell me how I should modify the code for the Selenium 2.44.0's Chrome Driver ( Chromedriver Win32 2.12 ) in such a way that I can Avoid using Thread.Sleep


Solution

  • Within our application's GUI, we have a div html element that behaves as an invisible loading screen, we needed to wait for it to Not exist anymore before we can click the button. For the test code that uses the Selenium 2.44.0's Firefox driver, the Wait object can wait until the invisible loading screen does Not exist anymore before the code tries to find the button object.

    Strangely, the test code that uses the Selenium 2.44.0's Chrome Driver ( Chromedriver Win32 2.12 ) needs to use the Wait object to wait until the invisible loading screen does Not exist immediately before my code tries to even click the button object:

    wait.Until(drv => ! Exists(drv, By.CssSelector("div.loading-screen"); 
    btn.Click();
    

    Selenium 2.44.0's Chrome Driver ( Chromedriver Win32 2.12 ) still seems to have some minor problems.