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:
-
Do we have an answer here?Dhiman– Dhiman2015年10月29日 13:58:37 +00:00Commented Oct 29, 2015 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 MinhIAmMilinPatel– IAmMilinPatel2016年05月27日 03:51:37 +00:00Commented May 27, 2016 at 3:51
-
1Your code looks overly complex for what you're trying to do, but from the looks of it, it should work as intended. Odd.FDM– FDM2016年05月27日 05:35:41 +00:00Commented May 27, 2016 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– George2016年09月24日 10:39:13 +00:00Commented Sep 24, 2016 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– FDM2017年06月21日 09:47:47 +00:00Commented Jun 21, 2017 at 9:47
2 Answers 2
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– Dhiman2015年10月29日 15:44:45 +00:00Commented Oct 29, 2015 at 15:44
-
2Exactly. I generally find implicit waits work better / are more stable in practice.ECiurleo– ECiurleo2015年10月29日 16:35:14 +00:00Commented Oct 29, 2015 at 16:35
-
2Please do not use implicit waits, they are considered bad practice. Read more here: stackoverflow.com/questions/10404160/…GKS1– GKS12015年11月29日 00:19:02 +00:00Commented Nov 29, 2015 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– FDM2016年05月27日 05:33:54 +00:00Commented May 27, 2016 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– ECiurleo2016年06月13日 15:22:13 +00:00Commented Jun 13, 2016 at 15:22
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 commentIAmMilinPatel– IAmMilinPatel2016年05月27日 03:52:01 +00:00Commented May 27, 2016 at 3:52
Explore related questions
See similar questions with these tags.