-2

I'm fairly new to Selenium and I'm writing a test for a web app using it. In doing this, I'm using assertions to make sure the web app is working correctly. For a few of these assertions, I'm asserting on a web element that has a numeric value in which the expected number is known. The problem is when a change is made that changes this numeric value the change happens gradually based on how fast the internet connection is. Up to this point have resorted to using sleep's to wait for the element to finish refreshing before I use assertions but I would like to make it so this wait is no longer than the time it takes for the element to stop refreshing and thus not have to include sleep's that are either too short or too long.

Zee
  • 1
  • Selenium's actions will wait for a pageload event. If this element is created dynamically without a pageload, you'll want to use a WebDriverWait along with an ExpectedCondition. Then it will poll the DOM until the expectedcondition is met, or the timeout period is reached. (Or a "stale element" exception is thrown...) – pcalkins Jul 02 '19 at 19:58
  • 1
    @orde Selenium 2.0 is REALLY old now... and there are a LOT of really bad answers on that question. Selenium now automatically waits for page load in many/most cases. – JeffC Jul 02 '19 at 21:32
  • @JeffC: crud. I didn't look closely enough at the answer. My bad. I've removed the dupe flag so it won't point to that answer. But this question is still a dupe of "how do I wait?" (which is seemingly asked daily and easily found by basic google searches or just RTM). – orde Jul 02 '19 at 21:37
  • @orde Agreed... we really need a good canonical updated answer to this question and about 20 others that are asked on a daily basis. – JeffC Jul 02 '19 at 23:39

2 Answers2

0

Avoid using sleep and replace it with an implicit wait or use expected condition if applicable. below is c# code for it

int time =10; // set maximum time required for operation
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(time));
wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementIsVisible(By.XPath(""))); //replace "" with your desired xpath

The above code will wait for max 10 seconds for an element to be visible. but if it appears earlier then it will jump to next process so you need not have to wait for a specific time. Also, there are other expected conditions available like ElementExists, ElementToBeClickable etc. I will leave it to you to explore the appropriate option for yourself

if you wan t to use implicit wait specifically use below code

driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(50));
Krunal Patel
  • 257
  • 3
  • 10
  • 1
    You have called it an implicit wait but not implicit wait was used. The code you have posted is an explicit wait. This code likely won't work. See my comment on mslowiak's answer. – JeffC Jul 02 '19 at 21:35
  • Yea, My mistake I jumbled up. Thanks for bringing it to my notice. Edited now with implicit wait example – Krunal Patel Jul 03 '19 at 13:53
0

You should try this:

WebDriverWait wait = new WebDriverWait(webDriver, timeoutInSeconds);
wait.until(ExpectedConditions.visibilityOfElementLocated(<specific locator of element>));

Sleep is not a good option because you wait always expected the amount of time.

In the approach presented above you always wait for the visibility of a specific element. When an element is visible your test steps will go forward. There is no extra wait time which you got with implicit sleep

mslowiak
  • 1,688
  • 12
  • 25
  • This likely won't work given OP's description of the page. The problem is that there are several steps in the flow described: 1. element exists and has a number, 2. user performs an action which causes the page to refresh and update that number, 3. now the page loads and the number is updated. If the script is fast enough (and the page is slow enough), it will catch the value at step 1 instead of waiting for step 3 to happen and then checking. – JeffC Jul 02 '19 at 21:34