Saturday, September 19, 2020

Explicit Wait|Explicit Wait|Fluent Wait


 Helper Class

=========

package com.businesshelper1;


import java.util.List;

import java.util.concurrent.TimeUnit;


import org.openqa.selenium.By;

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;


public class Helper {

public static WebDriver driver;

public static String driverpath="D:\\SeleniumJar_08_28_2020\\chromedriver_win32 (4)\\chromedriver.exe\\";

public static String url="https://www.google.com/webhp?authuser=3";

/**

     * This Method is used to launch App

     * 

     */

public static void launchApp(){

System.setProperty("webdriver.chrome.driver", driverpath);

driver=new ChromeDriver();

driver.get(url);

maxmizeBrowser();

}

/**

     * This Method is used to max browser

     * 

     */

public static void maxmizeBrowser(){

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

}

/**

     * This Method is used to close borwser

     * 

     */

public static void closeBrowser(){

driver.close();

}

/**

     * This Method is used to compare String

     * @param actual is String Type

     * @param Expected is String Type

     */

public static void compareString(String actual,String expected){

if(actual.equals(expected)){

System.out.println("************ Test Cased is passed ***********");

}

else{

System.out.println("************ Test Cased is Failed ***********");

}

}


    /**

     * This Method is used to Select RadioButton & Checkbox

     * @param WebElemet is ele para

     *

     * 

     */

public static WebElement veifySelected(WebElement ele){

ele.click();

boolean status=ele.isSelected();

if(status){

//System.out.println("Radio button is selected");

System.out.println("Check box is selected");

}

else{

//System.out.println("Radio button is not selected");

System.out.println("Check box is selected");

}

return ele;

}

/**

     * This Method is used to wait certain amount of time

     * @param integer type

     *

     * 

     */

public static void implicitWait(int a){

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

}

/**

     * This Method is used to wait specific Element 

     * @param integer type

     *

     * 

     */

public static WebElement explicityWait(WebDriver driver,int a,String str){

WebDriverWait wait=new WebDriverWait(driver, a);

return wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(str)));

}

/**

     * This Method is used to Fluent Wait 

     * @param integer type

     *

     * 

     */

public static WebElement fluentWait(WebDriver driver,int a,int b,String xpath ){

Wait<WebDriver> wait =new FluentWait(driver)

.withTimeout(a,TimeUnit.SECONDS)

.pollingEvery(b,TimeUnit.SECONDS)

.ignoring(NoSuchFieldException.class);

return wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(xpath)));

}


}

========================================================================
package com.seleniumpractice;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
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 com.businesshelper.Helper;

public class ExplicityWait1 {

public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver","D:\\SeleniumJar_08_28_2020\\chromedriver_win32 (4)\\chromedriver.exe\\");
WebDriver driver=new ChromeDriver();
driver.get("https://www.youtube.com/");
driver.manage().window().maximize();
//driver.findElement(By.xpath("//input[@name='search_query']")).sendKeys("Guru Randhawa",Keys.ENTER);
//WebDriverWait wait=new WebDriverWait(driver, null, null, 0, 0)
/*
WebDriverWait wait=new WebDriverWait(driver,10);
WebElement ele=wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//input[@name='search_query']")));
ele.sendKeys("Guru Randhawa",Keys.ENTER);
*/
/*
Wait<WebDriver> wait=new FluentWait(driver)
.withTimeout(10, TimeUnit.SECONDS)
.pollingEvery(5, TimeUnit.SECONDS)
        .ignoring(NoSuchElementException.class);
WebElement ele=wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//input[@name='search_query']")));
ele.sendKeys("Guru Randhawa",Keys.ENTER);
*/
//WebElement ele=com.businesshelper1.Helper.explicityWait(driver, 10,"//input[@name='search_query']");
WebElement ele=com.businesshelper1.Helper.fluentWait(driver,10,5,"//input[@name='search_query']" );
ele.sendKeys("GURU Randhawa",Keys.ENTER);
Thread.sleep(1000);
driver.findElement(By.xpath("//*[@id=\"video-title\"]/yt-formatted-string")).click();
Thread.sleep(10000);
driver.findElement(By.xpath("//*[@id=\"movie_player\"]/div[1]/video")).click();

}

}

No comments:

Post a Comment

Q) How To Find Duplicate Characters In A String In Java?

Step1:- Creating a HashMap containing char as key and it's occurrences as value. Step2:- Converting given string to char array. Step3:- ...