4

While creating test scripts in Selenium WebDriver using C#, I have used Explicit wait (after going through this link) and have also gone through SO and SQA links for the same. So, I implemented it like this:

Class1.cs

namespace SeleniumProject {
    public class WaitClass
    {
        public static void WaitForElementLoad(FirefoxDriver driver, By by, int timeoutInSeconds)
        {
            //string name = "TEST";
            if (timeoutInSeconds > 0)
            {
                WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(timeoutInSeconds));
                wait.Until(ExpectedConditions.ElementIsVisible(by));
            }
        }
    } }

and then used it in Class2.cs

namespace SeleniumProject
{
    [TestClass]
    public class StartTesting
    {
        public static FirefoxDriver driver;
        [TestMethod]
        public static void Main(string[] args)
        {

            driver = new FirefoxDriver();
            driver.Navigate().GoToUrl("http://www.test.com/");
            driver.Manage().Window.Maximize();
            String title = driver.Title;
            int timeinterval = 10;
            driver.FindElement(By.LinkText("Test Link")).Click();
            WaitClass.WaitForElementLoad(driver, By.LinkText("Child Test link 2"), timeinterval);
            driver.FindElement(By.LinkText("Child Test link 2")).Click();
            //System.Threading.Thread.Sleep(3000);
            WaitClass.WaitForElementLoad(driver, By.Name("firstname"), timeinterval);
            driver.FindElement(By.Name("firstname")).SendKeys(Firstname);

but, using it this way I get an error message for the Class1, frequency of this error is like 2 out of 10 iterations, because of this I have a feeling that either this is not a good + reliable method for explicit wait or I am doing something wrong here. I was earlier using Timeinterval as 5 secs. but after this error I changed it to 10 seconds and still getting same error randomly.

Error which I get is:

enter image description here

  • Do we have an answer here? – Dhiman Oct 29 '15 at 13:58
  • I also use Selenium C# for several years and I don't have any problem with Explicit Wait. This issue may be this is not a good + reliable method for explicit wait or maybe this comes from your site We need more info to isolate the issue, I think you need to capture the screenshot and print out HTML when the test failed. Comment by: Tam Minh – IAmMilinPatel May 27 '16 at 3:51
  • 1
    Your code looks overly complex for what you're trying to do, but from the looks of it, it should work as intended. Odd. – FDM May 27 '16 at 5:35
  • do you wait for element to be visible and then click/sendkeys and get error ? Try waiting for element to be clickable instead. – George Sep 24 '16 at 10:39
  • Also, can you confirm that when you're getting the error, your page is actually loaded? Maybe you test environment is randomly slow? – FDM Jun 21 '17 at 9:47
1

Personally I used the explicit wait as outlined by SeleniumHQ. I found it quite stable in operation.

http://www.seleniumhq.org/docs/04_webdriver_advanced.jsp

IWebDriver driver = new FirefoxDriver();
driver.Url = "http://somedomain/url_that_delays_loading";
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
IWebElement myDynamicElement = wait.Until<IWebElement>((d) =>
    {
        return d.FindElement(By.Id("someDynamicElement"));
    });

General caveat, I almost always recommend implicit waits if you have an element you are waiting for. That way if it loads quicker than 10 seconds you are not needlessly extending your test runs.

  • So, you recommend that I should use Implicit wait here? Because the Explicit wait many times works but sometimes not. – Dhiman Oct 29 '15 at 15:44
  • 2
    Exactly. I generally find implicit waits work better / are more stable in practice. – ECiurleo Oct 29 '15 at 16:35
  • 2
    Please do not use implicit waits, they are considered bad practice. Read more here: stackoverflow.com/questions/10404160/… – GKS1 Nov 29 '15 at 0:19
  • My framework no longer uses implicit waits (only explicit) and it works perfectly. Thanks @GKS1 for repeating the link everyone should know. ;) Selenium's default implicit wait is 0 for a reason. – FDM May 27 '16 at 5:33
  • @GKS1 if you disagree with my recommendation, please add an answer to the question with an opposing solution. That way people can see different answers and pick whichever solution suits them best! There is never only one answer when it comes to code :) – ECiurleo Jun 13 '16 at 15:22
0

I also use Selenium C# for several years and I don't have any problem with Explicit Wait.

This issue may be

this is not a good + reliable method for explicit wait

or

maybe this comes from your site

We need more info to isolate the issue, I think you need to capture the screenshot and print out HTML when the test failed.

Sorry, I don't have enough reputation to comment.

  • added your answer as a comment – IAmMilinPatel May 27 '16 at 3:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.