DEV Community

hjqueeen
hjqueeen

Posted on

await functions in JavaScript

When writing tests using Selenium, a crucial aspect, especially when a web page behaves asynchronously, is the "waiting" part. During the wait, the code should be designed to wait until specific conditions are met. The technical aspects for this include "Explicit Waits" and "Implicit Waits."

Explicit Waits:

Explicit waits involve waiting until specific conditions are met.
The WebDriverWait class can be used to wait for specific conditions.
For example, you can wait until a specific element appears or until a certain condition is satisfied.

const { Builder, By, until } = require('selenium-webdriver');

// ...

const driver = new Builder().forBrowser('chrome').build();
await driver.get('https://example.com');

const element = await driver.findElement(By.id('myElementId'));

// set to wait
await driver.wait(until.elementLocated(By.id('myElementId')), 5000);

Enter fullscreen mode Exit fullscreen mode

Implicit Waits:

Implicit waits are set globally, causing the WebDriver to wait until specific conditions are met.

const { Builder, By } = require('selenium-webdriver');

// ...

const driver = new Builder().forBrowser('chrome').build();
await driver.manage().setTimeouts({ implicit: 5000 });

await driver.get('https://example.com');

// set to wait
const element = await driver.findElement(By.id('myElementId'));

Enter fullscreen mode Exit fullscreen mode

It can be set using the implicitlyWait method. Explicit waits are generally recommended as they allow more flexibility in writing tests, waiting only until specific conditions are met. Be cautious when using implicit waits as they are a global setting and can impact the entire code. Optionally, you can mix and match both approaches based on your requirements.

Difference between driver.wait and driver.manage().setTimeout

Clarity of Waiting Conditions:

By using driver.wait, you can explicitly wait until specific conditions are met. In the above code, it is set to wait for a maximum of 5 seconds until the element with 'myElementId' appears.
manage().setTimeouts({ implicit: 5000 }) represents an implicit wait time, globally set, causing the WebDriver to automatically wait in various states of the page. This means it applies not to specific conditions but to all elements on the page.

Condition Satisfaction:

With driver.wait, the script waits until the specified condition is satisfied or a timeout occurs, proceeding to the next step upon condition satisfaction.
manage().setTimeouts({ implicit: 5000 }) waits for a maximum of 5 seconds, even if no elements on the page are loaded. In other words, the code continues to execute after the specified wait time, regardless of whether the condition is satisfied.

Scope of Application:

Using driver.wait allows you to wait for specific elements, such as waiting for a particular element's attribute to change.
manage().setTimeouts({ implicit: 5000 }) applies globally to all elements on the page.

Therefore, the choice depends on specific situations and requirements. If you need to explicitly wait for a specific element, using driver.wait is preferable. If you want to set a general wait time for overall page loading, manage().setTimeouts({ implicit: 5000 }) might be more appropriate.

Several standby features in Selenium WebDriver

Using until.elementLocated(locator) and until.elementIsVisible(element):

Waits until a specific element is located or becomes visible on the screen.

await driver.wait(until.elementLocated(By.id('myElementId')), 5000);
await driver.wait(until.elementIsVisible(driver.findElement(By.id('myElementId'))), 5000);
Enter fullscreen mode Exit fullscreen mode

Using until.titleIs(title):

Waits until the page title matches a specific value.

await driver.wait(until.titleIs('Expected Title'), 5000);
Enter fullscreen mode Exit fullscreen mode

Using until.titleContains(titleSnippet):

Waits until the page title contains a specific substring.

await driver.wait(until.titleContains('Snippet'), 5000);
Enter fullscreen mode Exit fullscreen mode

Using until.urlIs(url) and until.urlContains(urlSnippet):

Waits until the current page URL matches a specific value or contains a specific substring.

await driver.wait(until.urlIs('https://example.com'), 5000);
await driver.wait(until.urlContains('example'), 5000);
Enter fullscreen mode Exit fullscreen mode

These methods are employed with driver.wait, allowing you to choose the appropriate until condition based on your specific scenario. By combining these waiting mechanisms, you can create robust and stable test scripts.

Top comments (0)