Monday 23 May 2022

Fluent Wait in Selenium Webdriver Java with Example | Fluent Wait vs Implicit Wait vs Explicit Wait

                  In this video. we will see Fluent Wait in Selenium Webdriver Java with Example | Fluent Wait vs Implicit Wait vs Explicit Wait 


Program Code:

import java.util.concurrent.TimeUnit;

import java.util.function.Function;


import org.openqa.selenium.By;

import org.openqa.selenium.NoSuchElementException;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.chrome.ChromeDriver;

import org.openqa.selenium.support.ui.ExpectedConditions;

import org.openqa.selenium.support.ui.FluentWait;

import org.openqa.selenium.support.ui.Wait;

import org.openqa.selenium.support.ui.WebDriverWait;


import java.time.Duration;


public class FluentWaitExample {


@SuppressWarnings("deprecation")

public static void main(String[] args) throws InterruptedException 

{

System.setProperty("webdriver.chrome.driver", "C:\\selenium\\chromedriver_win32\\chromedriver.exe");

WebDriver driver=new ChromeDriver();

driver.get("https://in.bookmyshow.com/explore/movies-pune");

driver.manage().window().maximize();

//driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));

//WebDriverWait w =new WebDriverWait(driver, Duration.ofSeconds(5));

Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)

       .withTimeout(Duration.ofSeconds(30))

       .pollingEvery(Duration.ofSeconds(2))

       .ignoring(NoSuchElementException.class);

driver.findElement(By.xpath("(//div[text()='Marathi'])[3]")).click();

//w.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//img[@alt='Chandramukhi (Marathi)']")));

WebElement foo1 = wait.until(new Function<WebDriver, WebElement>() {

     public WebElement apply(WebDriver driver) {

       return driver.findElement(By.xpath("//img[@alt='Chandramukhi (Marathi)']"));

     }

   });

driver.findElement(By.xpath("//img[@alt='Chandramukhi (Marathi)']")).click();

driver.navigate().back();

driver.findElement(By.xpath("(//div[text()='English'])[3]")).click();

//w.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//img[@alt='Top Gun: Maverick']")));

WebElement foo2 = wait.until(new Function<WebDriver, WebElement>() {

     public WebElement apply(WebDriver driver) {

       return driver.findElement(By.xpath("//img[@alt='Top Gun: Maverick']"));

     }

   });

driver.findElement(By.xpath("//img[@alt='Top Gun: Maverick']")).click();

}


}  


Watch following video:


Watch on YouTube: https://www.youtube.com/watch?v=TBqovva7EA4&list=PLhbrpS8rYbc0vBS6Z8SC7OR-zknTBiBHW&index=18

No comments:

Post a Comment