Synchronization,Thread.sleep(time in millis),ImplicitWait
https://youtu.be/vtltAlKpyjs
ExplicitWait is mainly useful to apply wait on a single element.Synchronizing the state between the browser and its DOM, and your WebDriver script.This means that for as long as the condition returns a falsy value, it will keep trying and waiting.In order to achieve the ExplicitWait, we have to use a java class called WebDriverWait.
- alert is present
- element exists
- element is visible
- title contains
- title is
- visible text
File Upload Using Robot Class :
Video Link : https://youtu.be/UKUe0wpAN2k
Scenario 1:
Upload the TextFile and convert into PDF:C:\\SeleniumArtifacts\\ConvertTxttoPDF.txt
wait for visible Text
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 package seleniumexamples;import java.awt.AWTException;import java.awt.Robot;import java.awt.Toolkit;import java.awt.datatransfer.StringSelection;import java.awt.event.KeyEvent;import java.util.concurrent.TimeUnit;import org.openqa.selenium.By;import org.openqa.selenium.WebDriver;import org.openqa.selenium.chrome.ChromeDriver;import org.openqa.selenium.support.ui.ExpectedConditions;import org.openqa.selenium.support.ui.Select;import org.openqa.selenium.support.ui.WebDriverWait;import io.github.bonigarcia.wdm.WebDriverManager;public class ExplicitWaitScenario1 {/** Scenario 1:==========Upload the TextFile and convert into PDF:C:\\SeleniumArtifacts\\ConvertTxttoPDF.txtwait for visible Text*/static WebDriver driver;public static void main(String args[]) throws InterruptedException, AWTException{WebDriverManager.chromedriver().setup();driver = new ChromeDriver();driver.get("https://www.zamzar.com/");driver.manage().window().maximize();driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);fileUpload("C:\\SeleniumArtifacts\\ConvertTxttoPDF.txt");Select fileUploadOption = new Select(driver.findElement(By.id("convert-format")));fileUploadOption.selectByVisibleText("pdf");driver.findElement(By.id("convert-button")).click();WebDriverWait wait = new WebDriverWait(driver,45);Boolean result = wait.until(ExpectedConditions.textToBePresentInElementLocated(By.tagName("h1"), "All Done!"));System.out.println("Text Displayed Successful " + result);}public static void fileUpload(String filePath) throws InterruptedException, AWTException{driver.findElement(By.xpath("//button[@class='btn btn-default btn-lg']")).click();StringSelection clipBoardPath = new StringSelection(filePath);Toolkit.getDefaultToolkit().getSystemClipboard().setContents(clipBoardPath, null);Thread.sleep(5000);//Robot ClassRobot robot = new Robot();//Keyboard Action : CTRL+Vrobot.keyPress(KeyEvent.VK_CONTROL);robot.keyPress(KeyEvent.VK_V);robot.keyRelease(KeyEvent.VK_CONTROL);robot.keyRelease(KeyEvent.VK_V);//Keyboard Action : Enterrobot.keyPress(KeyEvent.VK_ENTER);robot.keyPress(KeyEvent.VK_ENTER);Thread.sleep(6000);}}
Scenario 2:
Upload the TextFile and convert into PDF : C:\\SeleniumArtifacts\\ConvertTxttoPDF.txt
Initial Title of the page : Zamzar – video converter, audio converter, image converter, eBook converter
wait for title is : Zamzar – File conversion progress
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 package seleniumexamples;import java.awt.AWTException;import java.awt.Robot;import java.awt.Toolkit;import java.awt.datatransfer.StringSelection;import java.awt.event.KeyEvent;import org.openqa.selenium.By;import org.openqa.selenium.WebDriver;import org.openqa.selenium.chrome.ChromeDriver;import org.openqa.selenium.support.ui.ExpectedConditions;import org.openqa.selenium.support.ui.Select;import org.openqa.selenium.support.ui.WebDriverWait;import io.github.bonigarcia.wdm.WebDriverManager;public class ExplicitWaitScenario2 {/** Scenario 2:==========Upload the TextFile and convert into PDF : C:\\SeleniumArtifacts\\ConvertTxttoPDF.txtwait for title is*/static WebDriver driver;public static void main(String args[]) throws InterruptedException, AWTException{WebDriverManager.chromedriver().setup();driver = new ChromeDriver();driver.get("https://www.zamzar.com/");driver.manage().window().maximize();fileUpload("C:\\SeleniumArtifacts\\ConvertTxttoPDF.txt");Select fileUploadOption = new Select(driver.findElement(By.id("convert-format")));fileUploadOption.selectByVisibleText("pdf");driver.findElement(By.id("convert-button")).click();WebDriverWait wait = new WebDriverWait(driver,45);Boolean result = wait.until(ExpectedConditions.titleIs("Zamzar - File conversion progress"));System.out.println("Title is visible::: " + result );}public static void fileUpload(String filePath) throws InterruptedException, AWTException{driver.findElement(By.xpath("//button[@class='btn btn-default btn-lg']")).click();StringSelection clipBoardPath = new StringSelection(filePath);Toolkit.getDefaultToolkit().getSystemClipboard().setContents(clipBoardPath, null);Thread.sleep(5000);//Robot ClassRobot robot = new Robot();//Keyboard Action : CTRL+Vrobot.keyPress(KeyEvent.VK_CONTROL);robot.keyPress(KeyEvent.VK_V);robot.keyRelease(KeyEvent.VK_CONTROL);robot.keyRelease(KeyEvent.VK_V);//Keyboard Action : Enterrobot.keyPress(KeyEvent.VK_ENTER);robot.keyPress(KeyEvent.VK_ENTER);Thread.sleep(6000);}}
Scenario 3:
Upload the JarFile : C:\\SeleniumArtifacts\\SeleniumLatestVersion\\selenium-java-3.141.59\\client-combined-3.141.59.jar
wait for alert is Present
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 package seleniumexamples;import java.awt.AWTException;import java.awt.Robot;import java.awt.Toolkit;import java.awt.datatransfer.StringSelection;import java.awt.event.KeyEvent;import org.openqa.selenium.Alert;import org.openqa.selenium.By;import org.openqa.selenium.WebDriver;import org.openqa.selenium.chrome.ChromeDriver;import org.openqa.selenium.support.ui.ExpectedConditions;import org.openqa.selenium.support.ui.WebDriverWait;import io.github.bonigarcia.wdm.WebDriverManager;public class ExplicitWaitScenario3 {/** Scenario 3:==========Upload the JarFile : C:\\SeleniumArtifacts\\SeleniumLatestVersion\\selenium-java-3.141.59\\client-combined-3.141.59.jarwait for alert is Present*/static WebDriver driver;public static void main(String args[]) throws InterruptedException, AWTException{WebDriverManager.chromedriver().setup();driver = new ChromeDriver();driver.get("https://www.zamzar.com/");driver.manage().window().maximize();fileUpload("C:\\SeleniumArtifacts\\SeleniumLatestVersion\\selenium-java-3.141.59\\client-combined-3.141.59.jar");WebDriverWait wait = new WebDriverWait(driver,45);Alert alrt = wait.until(ExpectedConditions.alertIsPresent());String alertMsgText = alrt.getText();System.out.println("Alert Msg::" + alertMsgText);alrt.dismiss();}public static void fileUpload(String filePath) throws InterruptedException, AWTException{driver.findElement(By.xpath("//button[@class='btn btn-default btn-lg']")).click();StringSelection clipBoardPath = new StringSelection(filePath);Toolkit.getDefaultToolkit().getSystemClipboard().setContents(clipBoardPath, null);Thread.sleep(5000);//Robot ClassRobot robot = new Robot();//Keyboard Action : CTRL+Vrobot.keyPress(KeyEvent.VK_CONTROL);robot.keyPress(KeyEvent.VK_V);robot.keyRelease(KeyEvent.VK_CONTROL);robot.keyRelease(KeyEvent.VK_V);//Keyboard Action : Enterrobot.keyPress(KeyEvent.VK_ENTER);robot.keyPress(KeyEvent.VK_ENTER);}}
Scenario 4:
Upload the TextFile and Convert into PDF: C:\\SeleniumArtifacts\\ConvertTxttoPDF.txt
wait for element is visible :Download
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 package seleniumexamples;import java.awt.AWTException;import java.awt.Robot;import java.awt.Toolkit;import java.awt.datatransfer.StringSelection;import java.awt.event.KeyEvent;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.Select;import org.openqa.selenium.support.ui.WebDriverWait;import io.github.bonigarcia.wdm.WebDriverManager;public class ExplicitWaitScenario4 {/** Scenario 4:==========Upload the TextFile and Convert into PDF: C:\\SeleniumArtifacts\\ConvertTxttoPDF.txtwait for element is visible*/static WebDriver driver;public static void main(String args[]) throws InterruptedException, AWTException{WebDriverManager.chromedriver().setup();driver = new ChromeDriver();driver.get("https://www.zamzar.com/");driver.manage().window().maximize();fileUpload("C:\\SeleniumArtifacts\\ConvertTxttoPDF.txt");Select fileUploadOption = new Select(driver.findElement(By.id("convert-format")));fileUploadOption.selectByVisibleText("pdf");driver.findElement(By.id("convert-button")).click();WebDriverWait wait = new WebDriverWait(driver,45);WebElement downloadWE = wait.until(ExpectedConditions.visibilityOfElementLocated(By.linkText("Download")));System.out.println(downloadWE.isDisplayed());System.out.println(downloadWE.isEnabled());}public static void fileUpload(String filePath) throws InterruptedException, AWTException{driver.findElement(By.xpath("//button[@class='btn btn-default btn-lg']")).click();StringSelection clipBoardPath = new StringSelection(filePath);Toolkit.getDefaultToolkit().getSystemClipboard().setContents(clipBoardPath, null);Thread.sleep(5000);//Robot ClassRobot robot = new Robot();//Keyboard Action : CTRL+Vrobot.keyPress(KeyEvent.VK_CONTROL);robot.keyPress(KeyEvent.VK_V);robot.keyRelease(KeyEvent.VK_CONTROL);robot.keyRelease(KeyEvent.VK_V);//Keyboard Action : Enterrobot.keyPress(KeyEvent.VK_ENTER);robot.keyPress(KeyEvent.VK_ENTER);Thread.sleep(6000);}}
Conclusion:
All the 4 Scenario’s will help to learn ExplicitWait.
We can use ExplicitWait in the programs to wait for a particular Element.