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”);
——————————————————————————————————————————
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);
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.
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
TakesScreenshot Example
Java
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
importjava.io.File;
importjava.io.IOException;
importorg.apache.commons.io.FileUtils;
importorg.openqa.selenium.OutputType;
importorg.openqa.selenium.TakesScreenshot;
importorg.openqa.selenium.WebDriver;
importorg.openqa.selenium.firefox.FirefoxDriver;
publicclassScreenshotEx{
public static void main(String[]args)throwsIOException{