Implicit Wait,Explicit Wait,Fluent Wait,TakesScreenshot
Synchronization
The communication between Selenium WebDriver script and the Application under Test (AUT) is known as synchronization.
Resolution:
Insert wait statements in the WebDriver script.
Unconditional Wait:
Unconditional Wait is achieved using Thread.sleep(long milliseconds). Thread is class in java and sleep is a static method.
This needs to be added before the statement which needs to be find out.
Thread.sleep(5000); // 5 seconds…
driver.findElement(By.id(“username”)).sendKeys(“selenium”);
——————————————————————————————————————————
Conditional Wait:
a. IMPLICIT WAIT
1 2 3 4 5 |
driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS); |
This statement is applicable for all the findElement() and findElements() methods in the program.
Scenario
- Available immediately proceed to next line
- Not available immediately and available after 5 few seconds goto next line.
- Not Available with in 30 seconds wait for 30 secs and throw exception
b. EXPLICIT WAIT
Explicit Wait is mainly useful to apply wait on a single element. In order to achieve the Explicit Wait, we have to use a class called WebDriverWait. AJAX calls are handled using explicit wait in selenium webdriver.
- WebDriverWait wait = new WebDriverWait(driver,30);
- wait.until(ExpectedConditions.visibilityOfElementLocated(By.id(“”));
- wait.until(ExpectedConditions.isAlertPresent());
- wait.until(ExpectedConditions.textToBePresentElement(Expected,By.id(“”)));
- wait.until(ExpectedConditions.titleContains(“”));
- wait.until(ExpectedConditions.titleIs(“”));
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
WebDriverWait wait = new WebDriverWait(driver,30); WebElement e =wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("searchInput"))); e.sendKeys("find the textbox"); Boolean b = wait.until(ExpectedConditions.titleContains("India")); if(b) { System.out.println("Title contains india"); } else { System.out.println("Title doesnt contain india"); } |
WARNING: Do not mix implicit and explicit waits. Doing so can cause unpredictable wait times. For example setting an implicit wait of 10 seconds and an explicit wait of 15 seconds, could cause a timeout to occur after 20 seconds.
Reference URL : https://www.seleniumhq.org/docs/04_webdriver_advanced.jsp
NoSuchElementException Occurs due to following reasons:
1. Property values are Dynamic.
2. Locator is wrong
3. Element is in the frame.
4. Element is taking time to load.
5. Status/Message is taking time to display.
Taking a Screenshot
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
import java.io.File; import java.io.IOException; import org.apache.commons.io.FileUtils; import org.openqa.selenium.OutputType; import org.openqa.selenium.TakesScreenshot; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class ScreenshotEx { public static void main(String[] args) throws IOException { System.setProperty("webdriver.gecko.driver","geckodriver.exe"); WebDriver driver = new FirefoxDriver(); driver.get("http://www.indeed.co.in"); screenShot(driver); JavascriptExecutor } public static void screenShot(WebDriver driver) throws IOException { //Converting one type to another type is known as type casting TakesScreenshot tsh = (TakesScreenshot) driver; File srcFile = tsh.getScreenshotAs(OutputType.FILE); System.out.println(srcFile.getAbsolutePath()); File destFile = new File("indeed"+System.currentTimeMillis()+".jpg"); //https://mvnrepository.com/artifact/commons-io/commons-io/2.6 //Download the jar and add the jar to the build path //Refresh the Eclipse project to view the File Created. FileUtils.copyFile(srcFile, destFile); } } |
Using a FirefoxProfile
1 2 3 4 5 6 7 8 9 |
FirefoxProfile fp = new FirefoxProfile(); // set something on the profile... DesiredCapabilities dc = DesiredCapabilities.firefox(); dc.setCapability(FirefoxDriver.PROFILE, fp); WebDriver driver = new RemoteWebDriver(dc); |
Using ChromeOptions
1 2 3 4 5 6 7 8 9 |
ChromeOptions options = new ChromeOptions(); // set some options DesiredCapabilities dc = DesiredCapabilities.chrome(); dc.setCapability(ChromeOptions.CAPABILITY, options); WebDriver driver = new RemoteWebDriver(dc); |