Using Selenium for Short and Long of Waits in Web Applications

Posted by Infocampus HR on December 16th, 2017

Frequently, a Web page conveys many Web components which set aside opportunity to stack, and if this is too long, clients tend to move to another Web page, bringing about the loss of significant business openings.

Most Web applications we manage are produced utilizing Ajax and JavaScript. In such cases, when a page is stacked by the program, the Web components will set aside some opportunity to stack and the stacking time can be diverse in each run. This prompts diverse exemptions, bringing about a report with the wrong outcomes despite the fact that the Web application is working effectively.

Handling the delays with accuracy:-

On seeing a postponement in the presence of a UI component on the page, how about we assume we utilize Thread.sleep() to deal with it. Whenever we run the test, the UI component stacks in a split second. Be that as it may, shouldn't something be said about Thread.sleep(), which we brought into our code? Regardless it ends the execution, along these lines inciting the additional hold up time despite the fact that it's not required. At long last, executing our whole test suite sets aside its own sweet opportunity to wrap up.

So to make computerization vigorous and precise, and additionally to synchronize our test instrument with the application under test by dealing with these deferrals progressively, we utilize holds up gave by Selenium.

Sorts of holds up:-

  1. Implicit hold up: A verifiable hold up is to educate WebDriver to survey the DOM for a specific measure of time when endeavoring to discover a component or components, if these are not promptly accessible. The default setting is 0. When set, the verifiable sit tight is set for the life of the WebDriver protest case.

Affirmation:

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

At the point when to utilize: Not prescribed, yet superior to utilizing Thread.sleep().

  1. PageLoadTimeout hold up: This hold up is centered around the time a Web page should be stacked – as far as possible the time that the content dispenses for a Web page to be shown before continuing with different operations on the Web components introduce on the page. On the off chance that the page stacks inside the time, at that point the content proceeds. On the off chance that the page does not stack inside the timeout, the content will be halted by a TimeoutException.

Affirmation:

driver.manage().timeouts().pageLoadTimeout(50, TimeUnit.SECONDS);

At the point when to utilize: Whenever you have to guarantee a page is stacked in the ideal time before continuing to play out an operation on WebElements.

  1. SetScriptTimeout: This sit tight is pertinent just for JavascriptExecutor, which gives us two techniques to content execution — executeScript and executeAsyncScript.

The executeScript technique will hinder the execution of the following line of code till the execution of this strategy is finished, and afterward it moves to the following line of code. The executeAsyncScript strategy does not hinder the execution of the following line of code till the execution of this technique is finished.

Since the executeAsyncScript strategy doesn't hinder the execution of the following line of code, we utilize driver.manage().timeouts().setScriptTimeout(5,SECONDS); to determine the measure of time for a nonconcurrent content to complete the execution before tossing a blunder.

Presentation:

driver.manage().timeouts().setScriptTimeout(5, TimeUnit.SECONDS);

At the point when to utilize: Used to indicate the measure of time for a nonconcurrent content to complete execution, or else it will require its own investment to execute and may enable a bug to disappear.

  1. FluentWait: Each FluentWait occurrence characterizes the greatest measure of time to sit tight for a condition, and additionally the recurrence with which to check the condition. The client may arrange the hold up to overlook particular sorts of special cases while holding up, for example, NoSuchElementExceptions while hunting down a component on the page, and to disregard the exemption to proceed with content execution in the event that the component isn't found.
  2. Explicit hold up: This enables the client to guide the driver to sit tight for specific conditions to happen before continuing further in the code. WebDriverWait of course calls the ExpectedCondition each 500 milliseconds until the point that it returns effectively.

Assertion:

WebDriverWait hold up = new WebDriverWait(driver, 10);

wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id='login-button']"))).click();

At the point when to utilize: When the component sets aside a long opportunity to stack and furthermore when there is a need to check the property of a component (e.g., component is interactive).

Viable utilization of holds up in a genuine

Utilizing a mix of pageLoadTimeout + SetScriptTimeout and ExplicitWait in your system guarantees that the page is stacked at the ideal time, and that every single offbeat content are executed in a stipulated time. ExplicitWait checks the different properties of various components in light of the kind of operation that should be performed. On the off chance that there are any disappointments, it's anything but difficult to discover the underlying driver for them. Utilizing this blend of holds up in your system can convey the most strong and precise outcomes.

About Author:

A best training institute Infocampus offers selenium training in Bangalore. Contact 9738001024 to attend free demo classes conducting on weekdays and weekends. Infocampus is one of the selenium training institutes in Bangalore. It also provides 100% placement assistance and placed many candidates with good mnc’s. Learn selenium at Infocampus.

Like it? Share it!


Infocampus HR

About the Author

Infocampus HR
Joined: December 10th, 2016
Articles Posted: 792

More by this author