­

Subscribe

About Me

My photo
Welcome to my corner of the internet! I’m a software engineer by day, mechanical engineer by degree, some-time traveler, music lover, and self-proclaimed motivational speaker by... well, any spare minute I can find. When I’m not busy debugging code, you can find me lost in a new place, jamming to a mix of Indian and Western music (yes, I’m the one who creates playlists that make no sense), or pondering the mysteries of the universe, from physics to geopolitics. Oh, and if you need some motivation or a good laugh, I’m your go-to person—I'm basically a walking TED talk. Stick around for random musings, technical rants, and possibly some okayish poetry ✍️!

Translate

Wednesday, February 12, 2025

Selenium Webdriver (Part 2) - Waits

 Types of Waits:

1. Implicit Wait

2. Explicit Wait

  1. Webdriver Wait
  2. Fluent Wait

Implicit Wait-

Pros-

1. Declared globally. Wait for a max of n number of seconds before you throw error.
2. Readable code.

Cons-
1. Performance cause issues
2. if implicit wait is set to 20 s say, then we cannot find which components are slowly loading.

Explicit Wait-

Pros-
1. Specific to that locator only.
2. Wait is applied at only targeted elements. So no performance issues.

Cons-
1. More written code and disturbs readability.


_____________________________________________________________________________


Explicit Waits are of two types:

   1. Webdriver Wait
   2. Fluent Wait

Both implement the Wait interface.
FluentWait doesn't have methods to use, so we need to create custom methods for it to use it.


________________________________________________________________________________

Syntax/examples:

1. Fluent Wait

Wait<Webdriver> wait = new FluentWait<Webdriver>();
.withTimeout(10,SECONDS)
.pollingEvery(2,SECONDS)
.ignoring(NoSuchElementException.class)

2. Implicit Wait

driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); // Duration.of(5) not working

3. Explicit Wait 

WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("element_id")));
wait.until(ExpectedConditions.textToBePresentInElementLocated(By.className("message"), "Success"));
wait.until(ExpectedConditions.alertIsPresent());

4. Thread.sleep

Halts the code for n seconds.










          


No comments:

Post a Comment

Persistent Automation Testing Interview Questions (5+ YOE)

What is Serialization and Deserialization and how to perform it? WAP for changing d to D in "I am from India" WAP for counting no....