How to use Interface WebDriver.Timeouts class of org.openqa.selenium package

Best Selenium code snippet using org.openqa.selenium.Interface WebDriver.Timeouts

Run Selenium automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

copy
1package waiters;
2
3import org.openqa.selenium.By;
4import org.openqa.selenium.WebElement;
5import org.openqa.selenium.support.ui.ExpectedCondition;
6
7/**
8 * Интерфейс стандартного набора ожиданий
9 * @author Pavel Balahonov <p.balahonov@corp.mail.ru>
10 */
11public interface WaiterInt {
12  boolean waitForCondition(ExpectedCondition condition);
13
14  long IMPLICITLY_WAIT_SECOND = Integer.parseInt(System.getProperty("webdriver.timeouts.implicitlywait", "5000")) / 1000;
15}
16
Full Screen
copy
1package com.webdriver.training.webelements;
2
3
4import org.junit.Test;
5import org.openqa.selenium.By;
6import org.openqa.selenium.WebDriver;
7import org.openqa.selenium.WebElement;
8import org.openqa.selenium.chrome.ChromeDriver;
9import java.util.concurrent.TimeUnit;
10
11public class TimeoutManagement {
12
13    //Webdriver.Timeouts interface is used to manages implicit timeouts
14    //First we need reference to WebDriver.Options using WebDriver.manage() method
15    //Then using Webdriver.Options.timeouts() method we receive reference to WebDriver.Timeouts instance
16
17    //compare this test with test from BrowserNavigation class
18    @Test
19    public void navigateWithImplicitTimeout() {
20        WebDriver driver = new ChromeDriver();
21        WebDriver.Options options = driver.manage();
22        WebDriver.Timeouts timeouts = options.timeouts();
23
24        //amount of time the driver should wait when searching for an element if it is not immediately present.
25        //driver should poll the page until the element has
26        //been found, or this timeout expires before throwing a {@link NoSuchElementException}.
27        timeouts.implicitlyWait(2, TimeUnit.SECONDS);
28
29        //amount of time to wait for a page load to complete before throwing an error
30        timeouts.pageLoadTimeout(10, TimeUnit.SECONDS);
31
32        WebDriver.Navigation navigator = driver.navigate();
33        navigator.to("http://www.google.com");
34
35        WebElement searchBox = driver.findElement(By.name("q"));
36        searchBox.sendKeys("Selenium WebDriver");
37        WebElement searchButton = driver.findElement(By.name("btnG"));
38        searchButton.click();
39        searchBox.clear();
40        searchBox.sendKeys("Packt Publishing");
41        searchButton.click();
42        driver.findElement(By.xpath("//a[text() = 'Packt Publishing']"));
43        navigator.back();
44        driver.findElement(By.xpath("//a[text() = 'Selenium WebDriver']"));
45        navigator.forward();
46        driver.findElement(By.xpath("//a[text() = 'Packt Publishing']"));
47        navigator.refresh();
48        driver.findElement(By.xpath("//a[text() = 'Packt Publishing']"));
49
50        //Thanks to implicit wait we eliminated Thread.sleeps!!!!
51    }
52
53
54    //Unlike implicit timeouts which is part of selenium-api jar, there is also explicit timeout located in selenium-support jar
55    //Explicit timeout can be used for particular web elements
56    //Our main class is WebDriverWait which extends FluentWait<WebDriver> class
57    //Because FluentWait<WebDriver> implements Wait<WebDriver> interface we can use T Wait.until(Function<? super WebDriver, T> isTrue) method to evaluate our expected condition in defined amount of time
58    //We can use predefined conditions collected in ExpectedConditions class or create custom using ExpectedCondition interface
59}
60
Full Screen
copy
1package io.magentys.cinnamon.webdriver;
2
3import io.magentys.cinnamon.webdriver.actions.synthetic.DomEvent;
4import io.magentys.cinnamon.webdriver.actions.synthetic.SyntheticEvent;
5import io.magentys.cinnamon.webdriver.conditions.Condition;
6import io.magentys.cinnamon.webdriver.support.ui.CinnamonExpectedConditions;
7import org.openqa.selenium.Alert;
8import org.openqa.selenium.JavascriptExecutor;
9import org.openqa.selenium.WebDriver;
10import org.openqa.selenium.WebElement;
11import org.openqa.selenium.support.ui.FluentWait;
12import org.openqa.selenium.support.ui.WebDriverWait;
13
14import static io.magentys.cinnamon.webdriver.Timeouts.defaultTimeout;
15import static io.magentys.cinnamon.webdriver.WebDriverContainer.getWebDriverContainer;
16
17public final class Browser {
18
19    /**
20     * Opens a new web page in the current browser window.
21     *
22     * @param url The URL to load
23     */
24    public static void open(String url) {
25        getWebDriver().get(url);
26    }
27
28    public static String currentUrl() {
29        return getWebDriver().getCurrentUrl();
30    }
31
32    /**
33     * Gets the title of the current page.
34     *
35     * @return The title of the current page
36     */
37    public static String title() {
38        return getWebDriver().getTitle();
39    }
40
41    public static void back() {
42        getWebDriver().navigate().back();
43    }
44
45    public static void forward() {
46        getWebDriver().navigate().forward();
47    }
48
49    public static void refresh() {
50        getWebDriver().navigate().refresh();
51    }
52
53    /**
54     * Closes the browser window that has focus.
55     */
56    public static void close() {
57        getWebDriver().close();
58    }
59
60    public static CinnamonTargetLocator switchTo() {
61        return new CinnamonTargetLocator(getWebDriver(), getWindowTracker());
62    }
63
64    public static Alert alert() {
65        return alert(defaultTimeout());
66    }
67
68    public static Alert alert(final Timeout timeout) {
69        return switchTo().alert(timeout);
70    }
71
72    public static void fireEvent(final WebElement element, final DomEvent event) {
73        syntheticEvent().fireEvent(element, event);
74    }
75
76    /**
77     * Executes JavaScript in the context of the currently selected frame or window. The script
78     * fragment provided will be executed as the body of an anonymous function.
79     *
80     * @param script The JavaScript to execute
81     * @param args The arguments to the script. May be empty
82     * @param <T> One of Boolean, Long, Double, String, List or WebElement
83     * @return One of Boolean, Long, Double, String, List or WebElement. Or null.
84     * @see org.openqa.selenium.JavascriptExecutor#executeScript(String, Object...)
85     */
86    @SuppressWarnings("unchecked")
87    public static <T> T executeScript(String script, Object... args) {
88        return (T) ((JavascriptExecutor) getWebDriver()).executeScript(script, args);
89    }
90
91    /**
92     * Gets the Options interface
93     * <p>
94     * Binds to an instance of CinnamonWebDriverOptions. Uses Javascript to manage cookies if the browser
95     * is either InternetExplorer or Safari.
96     *
97     * @return WebDriver.Options
98     * @see org.openqa.selenium.WebDriver.Options
99     */
100    public static WebDriver.Options manage() {
101        return new CinnamonWebDriverOptions(getWebDriver());
102    }
103
104    public static void waitUntil(final Condition<WebDriver> condition) {
105        waitUntil(condition, defaultTimeout());
106    }
107
108    public static void waitUntil(final Condition<WebDriver> condition, final Timeout timeout) {
109        wait(timeout).until(CinnamonExpectedConditions.conditionToBe(condition));
110    }
111
112    private static FluentWait<WebDriver> wait(final Timeout timeout) {
113        return new WebDriverWait(getWebDriver(), timeout.getSeconds());
114    }
115
116    /**
117     * Causes the currently executing thread to sleep (temporarily cease execution) for the specified number of
118     * milliseconds.
119     * <p>
120     * This method is not recommended. Instead, you should identify a more robust synchronisation solution.
121     *
122     * @param millis The time to sleep in milliseconds
123     * @see Thread#sleep(long)
124     */
125    public static void sleep(long millis) {
126        try {
127            Thread.sleep(millis);
128        } catch (InterruptedException e) {
129            Thread.currentThread().interrupt();
130        }
131    }
132
133    public static WebDriver getWebDriver() {
134        return getWebDriverContainer().getWebDriver();
135    }
136
137    private static WindowTracker getWindowTracker() {
138        return getWebDriverContainer().getWindowTracker();
139    }
140
141    private static SyntheticEvent syntheticEvent() {
142        return new SyntheticEvent(getWebDriver());
143    }
144}
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

...Most popular Stackoverflow questions on Interface-WebDriver.Timeouts

    No relevant questions found for this class 😞

Run Selenium Automation Tests on LambdaTest Cloud Grid

Trigger Selenium automation tests on a cloud-based Grid of 3000+ real browsers and operating systems.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)