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

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

Source:Browser.java Github

copy

Full Screen

1package teammates.e2e.pageobjects;2import java.io.File;3import java.io.IOException;4import java.nio.file.Files;5import java.nio.file.Paths;6import java.util.HashMap;7import java.util.Map;8import java.util.Stack;9import java.util.concurrent.TimeUnit;10import org.openqa.selenium.JavascriptExecutor;11import org.openqa.selenium.ScriptTimeoutException;12import org.openqa.selenium.TimeoutException;13import org.openqa.selenium.WebDriver;14import org.openqa.selenium.chrome.ChromeDriver;15import org.openqa.selenium.chrome.ChromeOptions;16import org.openqa.selenium.firefox.FirefoxDriver;17import org.openqa.selenium.firefox.FirefoxOptions;18import org.openqa.selenium.firefox.FirefoxProfile;19import org.openqa.selenium.firefox.ProfilesIni;20import org.openqa.selenium.support.ui.WebDriverWait;21import teammates.e2e.util.TestProperties;22import teammates.test.FileHelper;23/**24 * A programmatic interface to the Browser used to test the app.25 */26public class Browser {27 private static final String PAGE_LOAD_SCRIPT;28 static {29 try {30 PAGE_LOAD_SCRIPT = FileHelper.readFile("src/e2e/resources/scripts/waitForPageLoad.js");31 } catch (IOException e) {32 throw new RuntimeException(e);33 }34 }35 /**36 * Indicates whether the app is being used by an admin.37 */38 public boolean isAdminLoggedIn;39 /**40 * The {@link WebDriver} object that drives the Browser instance.41 */42 WebDriver driver;43 /**44 * Keeps track of multiple windows opened by the {@link WebDriver}.45 */46 private final Stack<String> windowHandles = new Stack<>();47 public Browser() {48 this.driver = createWebDriver();49 this.driver.manage().window().maximize();50 this.driver.manage().timeouts().pageLoadTimeout(TestProperties.TEST_TIMEOUT * 2, TimeUnit.SECONDS);51 this.driver.manage().timeouts().setScriptTimeout(TestProperties.TEST_TIMEOUT, TimeUnit.SECONDS);52 this.isAdminLoggedIn = false;53 }54 /**55 * Switches to new browser window for browsing.56 */57 public void switchToNewWindow() {58 String curWin = driver.getWindowHandle();59 for (String handle : driver.getWindowHandles()) {60 if (!handle.equals(curWin) && !windowHandles.contains(curWin)) {61 windowHandles.push(curWin);62 driver.switchTo().window(handle);63 break;64 }65 }66 }67 /**68 * Waits for the page to load. This includes all AJAX requests and Angular animations in the page.69 */70 public void waitForPageLoad() {71 waitForPageLoad(false);72 }73 /**74 * Waits for the page to load. This includes all AJAX requests and Angular animations in the page.75 *76 * @param excludeToast Set this to true if toast message's disappearance should not be counted77 * as criteria for page load's completion.78 */79 public void waitForPageLoad(boolean excludeToast) {80 try {81 WebDriverWait wait = new WebDriverWait(driver, TestProperties.TEST_TIMEOUT);82 wait.until(driver -> {83 return "complete".equals(84 ((JavascriptExecutor) driver).executeAsyncScript(PAGE_LOAD_SCRIPT, excludeToast ? 1 : 0)85 );86 });87 } catch (ScriptTimeoutException e) {88 System.out.println("Page could not load completely. Trying to continue test.");89 }90 }91 /**92 * Waits for the page to load by only looking at the page's readyState.93 */94 public void waitForPageReadyState() {95 WebDriverWait wait = new WebDriverWait(driver, TestProperties.TEST_TIMEOUT);96 wait.until(driver -> {97 return "complete".equals(((JavascriptExecutor) driver).executeScript("return document.readyState"));98 });99 }100 /**101 * Closes the current browser window and switches back to the last window used previously.102 */103 public void closeCurrentWindowAndSwitchToParentWindow() {104 driver.close();105 driver.switchTo().window(windowHandles.pop());106 }107 /**108 * Closes the current browser.109 */110 public void close() {111 driver.quit();112 }113 /**114 * Visits the given URL.115 */116 public void goToUrl(String url) {117 if (TestProperties.BROWSER.equals(TestProperties.BROWSER_CHROME)) {118 // Recent chromedriver has bug in setting page load timeout, which can potentially cause infinitely long waits119 ((JavascriptExecutor) driver).executeScript("window.location.href='" + url + "'");120 return;121 }122 try {123 driver.get(url);124 } catch (TimeoutException e) {125 System.out.println("Page could not load completely. Trying to continue test.");126 }127 }128 private WebDriver createWebDriver() {129 System.out.print("Initializing Selenium: ");130 String downloadPath;131 try {132 downloadPath = new File(TestProperties.TEST_DOWNLOADS_FOLDER).getCanonicalPath();133 System.out.println("Download path: " + downloadPath);134 } catch (IOException e) {135 throw new RuntimeException(e);136 }137 String browser = TestProperties.BROWSER;138 if (TestProperties.BROWSER_FIREFOX.equals(browser)) {139 System.out.println("Using Firefox with driver path: " + TestProperties.GECKODRIVER_PATH);140 String firefoxPath = TestProperties.FIREFOX_PATH;141 if (!firefoxPath.isEmpty()) {142 System.out.println("Custom path: " + firefoxPath);143 System.setProperty("webdriver.firefox.bin", firefoxPath);144 }145 System.setProperty("webdriver.gecko.driver", TestProperties.GECKODRIVER_PATH);146 FirefoxProfile profile;147 if (TestProperties.isDevServer()) {148 profile = new FirefoxProfile();149 } else {150 // Get user data from browser to bypass google blocking automated log in.151 // Log in manually to teammates to use that log in data for e2e tests.152 ProfilesIni profileIni = new ProfilesIni();153 profile = profileIni.getProfile(TestProperties.FIREFOX_PROFILE_NAME);154 if (profile == null) {155 throw new RuntimeException("Firefox profile not found. Failed to create webdriver.");156 }157 }158 // Allow CSV files to be download automatically, without a download popup.159 // This method is used because Selenium cannot directly interact with the download dialog.160 // Taken from http://stackoverflow.com/questions/24852709161 profile.setPreference("browser.download.panel.shown", false);162 profile.setPreference("browser.helperApps.neverAsk.openFile", "text/csv,application/vnd.ms-excel");163 profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "text/csv,application/vnd.ms-excel");164 profile.setPreference("browser.download.folderList", 2);165 profile.setPreference("browser.download.dir", downloadPath);166 FirefoxOptions options = new FirefoxOptions().setProfile(profile);167 if (TestProperties.isDevServer()) {168 options.addArguments("-private");169 }170 return new FirefoxDriver(options);171 }172 if (TestProperties.BROWSER_CHROME.equals(browser)) {173 System.out.println("Using Chrome with driver path: " + TestProperties.CHROMEDRIVER_PATH);174 System.setProperty("webdriver.chrome.driver", TestProperties.CHROMEDRIVER_PATH);175 Map<String, Object> chromePrefs = new HashMap<>();176 chromePrefs.put("download.default_directory", downloadPath);177 chromePrefs.put("download.prompt_for_download", false);178 ChromeOptions options = new ChromeOptions();179 options.setExperimentalOption("prefs", chromePrefs);180 options.addArguments("--allow-file-access-from-files");181 if (TestProperties.isDevServer()) {182 options.addArguments("incognito");183 } else {184 // Get user data from browser to bypass google blocking automated log in.185 // Log in manually to teammates to use that log in data for e2e tests.186 if (TestProperties.CHROME_USER_DATA_PATH.isEmpty()187 || !Files.exists(Paths.get(TestProperties.CHROME_USER_DATA_PATH))) {188 throw new RuntimeException("Chrome user data path not found. Failed to create webdriver.");189 }190 options.addArguments("user-data-dir=" + TestProperties.CHROME_USER_DATA_PATH);191 }192 return new ChromeDriver(options);193 }194 throw new RuntimeException("Using " + browser + " is not supported!");195 }196}...

Full Screen

Full Screen

Source:TimeoutManagement.java Github

copy

Full Screen

1package com.webdriver.training.webelements;234import 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;1011public class TimeoutManagement {1213 //Webdriver.Timeouts interface is used to manages implicit timeouts14 //First we need reference to WebDriver.Options using WebDriver.manage() method15 //Then using Webdriver.Options.timeouts() method we receive reference to WebDriver.Timeouts instance1617 //compare this test with test from BrowserNavigation class18 @Test19 public void navigateWithImplicitTimeout() {20 WebDriver driver = new ChromeDriver();21 WebDriver.Options options = driver.manage();22 WebDriver.Timeouts timeouts = options.timeouts();2324 //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 has26 //been found, or this timeout expires before throwing a {@link NoSuchElementException}.27 timeouts.implicitlyWait(2, TimeUnit.SECONDS);2829 //amount of time to wait for a page load to complete before throwing an error30 timeouts.pageLoadTimeout(10, TimeUnit.SECONDS);3132 WebDriver.Navigation navigator = driver.navigate();33 navigator.to("http://www.google.com");3435 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']"));4950 //Thanks to implicit wait we eliminated Thread.sleeps!!!!51 }525354 //Unlike implicit timeouts which is part of selenium-api jar, there is also explicit timeout located in selenium-support jar55 //Explicit timeout can be used for particular web elements56 //Our main class is WebDriverWait which extends FluentWait<WebDriver> class57 //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 time58 //We can use predefined conditions collected in ExpectedConditions class or create custom using ExpectedCondition interface59} ...

Full Screen

Full Screen

Interface WebDriver.Options

Using AI Code Generation

copy

Full Screen

1import org.openqa.selenium.WebDriver;2import org.openqa.selenium.chrome.ChromeDriver;3import org.openqa.selenium.chrome.ChromeOptions;4import org.openqa.selenium.firefox.FirefoxDriver;5import org.openqa.selenium.firefox.FirefoxOptions;6import org.openqa.selenium.ie.InternetExplorerDriver;7import org.openqa.selenium.ie.InternetExplorerOptions;8import org.openqa.selenium.opera.OperaDriver;9import org.openqa.selenium.opera.OperaOptions;10import org.openqa.selenium.remote.DesiredCapabilities;11import org.openqa.selenium.safari.SafariDriver;12import org.openqa.selenium.safari.SafariOptions;13public class DriverFactory {14 public static WebDriver driver = null;15 public static WebDriver getDriver(String browser) {16 if (browser.equalsIgnoreCase("chrome")) {17 System.setProperty("webdriver.chrome.driver", "src/main/resources/drivers/chromedriver.exe");18 ChromeOptions options = new ChromeOptions();19 options.addArguments("--start-maximized");20 options.addArguments("--disable-notifications");21 driver = new ChromeDriver(options);22 } else if (browser.equalsIgnoreCase("firefox")) {23 System.setProperty("webdriver.gecko.driver", "src/main/resources/drivers/geckodriver.exe");24 FirefoxOptions options = new FirefoxOptions();25 options.addArguments("--start-maximized");26 options.addArguments("--disable-notifications");27 driver = new FirefoxDriver(options);28 } else if (browser.equalsIgnoreCase("ie")) {29 System.setProperty("webdriver.ie.driver", "src/main/resources/drivers/IEDriverServer.exe");30 InternetExplorerOptions options = new InternetExplorerOptions();31 options.addCommandSwitches("--start-maximized");32 options.addCommandSwitches("--disable-notifications");33 driver = new InternetExplorerDriver(options);34 } else if (browser.equalsIgnoreCase("opera")) {35 System.setProperty("webdriver.opera.driver", "src/main/resources/drivers/operadriver.exe");36 OperaOptions options = new OperaOptions();37 options.addArguments("--start-maximized");38 options.addArguments("--disable-notifications");39 driver = new OperaDriver(options);40 } else if (browser.equalsIgnoreCase("safari")) {41 SafariOptions options = new SafariOptions();42 options.addArguments("--start-maximized");43 options.addArguments("--disable-notifications");44 driver = new SafariDriver(options);45 }46 return driver;47 }48}49import org.openqa.selenium.WebDriver;50import org.openqa.selenium.chrome.ChromeDriver;51import org.openqa.selenium

Full Screen

Full Screen

Interface WebDriver.Options

Using AI Code Generation

copy

Full Screen

1import org.openqa.selenium.WebDriver;2import org.openqa.selenium.chrome.ChromeDriver;3import org.openqa.selenium.chrome.ChromeOptions;4import org.openqa.selenium.firefox.FirefoxDriver;5import org.openqa.selenium.firefox.FirefoxOptions;6import org.openqa.selenium.ie.InternetExplorerDriver;7import org.openqa.selenium.ie.InternetExplorerOptions;8import org.openqa.selenium.opera.OperaDriver;9import org.openqa.selenium.opera.OperaOptions;10import org.openqa.selenium.remote.CapabilityType;11import org.openqa.selenium.remote.DesiredCapabilities;12import org.openqa.selenium.safari.SafariDriver;13import org.openqa.selenium.safari.SafariOptions;14public class WebDriverOptions {15 public static void main(String[] args) {16 System.setProperty("webdriver.chrome.driver","E:\\Selenium\\chromedriver.exe");17 System.setProperty("webdriver.gecko.driver","E:\\Selenium\\geckodriver.exe");18 System.setProperty("webdriver.ie.driver","E:\\Selenium\\IEDriverServer.exe");19 System.setProperty("webdriver.opera.driver","E:\\Selenium\\operadriver.exe");20 System.setProperty("webdriver.safari.driver","E:\\Selenium\\safaridriver.exe");21 ChromeOptions chromeOptions = new ChromeOptions();22 FirefoxOptions firefoxOptions = new FirefoxOptions();23 InternetExplorerOptions internetExplorerOptions = new InternetExplorerOptions();24 OperaOptions operaOptions = new OperaOptions();25 SafariOptions safariOptions = new SafariOptions();26 DesiredCapabilities desiredCapabilities = new DesiredCapabilities();27 desiredCapabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);28 desiredCapabilities.setCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR, "accept");29 desiredCapabilities.setCapability(CapabilityType.HAS_NATIVE_EVENTS, true);30 desiredCapabilities.setCapability(CapabilityType.SUPPORTS_JAVASCRIPT, true);31 desiredCapabilities.setCapability(CapabilityType.SUPPORTS_FINDING_BY_CSS, true);

Full Screen

Full Screen

Interface WebDriver.Options

Using AI Code Generation

copy

Full Screen

1package selenium;2import org.openqa.selenium.By;3import org.openqa.selenium.WebDriver;4import org.openqa.selenium.WebElement;5import org.openqa.selenium.chrome.ChromeDriver;6import org.openqa.selenium.chrome.ChromeOptions;7public class SeleniumWebDriverOptionsClass {8 public static void main(String[] args) {9 System.setProperty("webdriver.chrome.driver", "C:\\Users\\sandeep\\Downloads\\chromedriver_win32\\chromedriver.exe");10 ChromeOptions options = new ChromeOptions();11 options.addArguments("start-maximized");12 WebDriver driver = new ChromeDriver(options);13 WebElement element = driver.findElement(By.name("q"));14 element.sendKeys("Selenium");15 element.submit();16 System.out.println("Page title is: " + driver.getTitle());17 driver.quit();18 }19}20package selenium;21import org.openqa.selenium.By;22import org.openqa.selenium

Full Screen

Full Screen

Interface WebDriver.Options

Using AI Code Generation

copy

Full Screen

1package org.example;2import org.openqa.selenium.*;3import org.openqa.selenium.chrome.ChromeDriver;4{5 public static void main( String[] args )6 {7 System.setProperty("webdriver.chrome.driver","C:\\Users\\User\\Downloads\\chromedriver_win32\\chromedriver.exe");8 WebDriver driver = new ChromeDriver();9 driver.manage().window().setSize(new Dimension(600, 800));10 driver.quit();11 }12}

Full Screen

Full Screen

Interface WebDriver.Options

Using AI Code Generation

copy

Full Screen

1public class WebDriverOptions {2 public static void main(String[] args) {3 System.setProperty("webdriver.chrome.driver","E:\\Selenium\\chromedriver.exe");4 System.setProperty("webdriver.gecko.driver","E:\\Selenium\\geckodriver.exe");5 System.setProperty("webdriver.ie.driver","E:\\Selenium\\IEDriverServer.exe");6 System.setProperty("webdriver.opera.driver","E:\\Selenium\\operadriver.exe");7 System.setProperty("webdriver.safari.driver","E:\\Selenium\\safaridriver.exe");8 ChromeOptions chromeOptions = new ChromeOptions();9 FirefoxOptions firefoxOptions = new FirefoxOptions();10 InternetExplorerOptions internetExplorerOptions = new InternetExplorerOptions();11 OperaOptions operaOptions = new OperaOptions();12 SafariOptions safariOptions = new SafariOptions();13 DesiredCapabilities desiredCapabilities = new DesiredCapabilities();14 desiredCapabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);15 desiredCapabilities.setCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR, "accept");16 desiredCapabilities.setCapability(CapabilityType.HAS_NATIVE_EVENTS, true);17 desiredCapabilities.setCapability(CapabilityType.SUPPORTS_JAVASCRIPT, true);18 desiredCapabilities.setCapability(CapabilityType.SUPPORTS_FINDING_BY_CSS, true);

Full Screen

Full Screen

Interface WebDriver.Options

Using AI Code Generation

copy

Full Screen

1package selenium;2import org.openqa.selenium.By;3import org.openqa.selenium.WebDriver;4import org.openqa.selenium.WebElement;5import org.openqa.selenium.chrome.ChromeDriver;6import org.openqa.selenium.chrome.ChromeOptions;7public class SeleniumWebDriverOptionsClass {8 public static void main(String[] args) {9 System.setProperty("webdriver.chrome.driver", "C:\\Users\\sandeep\\Downloads\\chromedriver_win32\\chromedriver.exe");10 ChromeOptions options = new ChromeOptions();11 options.addArguments("start-maximized");12 WebDriver driver = new ChromeDriver(options);13 WebElement element = driver.findElement(By.name("q"));14 element.sendKeys("Selenium");15 element.submit();16 System.out.println("Page title is: " + driver.getTitle());17 driver.quit();18 }19}20package selenium;21import org.openqa.selenium.By;22import org.openqa.selenium

Full Screen

Full Screen

Interface WebDriver.Options

Using AI Code Generation

copy

Full Screen

1package org.example;2import org.openqa.selenium.*;3import org.openqa.selenium.chrome.ChromeDriver;4{5 public static void main( String[] args )6 {7 System.setProperty("webdriver.chrome.driver","C:\\Users\\User\\Downloads\\chromedriver_win32\\chromedriver.exe");8 WebDriver driver = new ChromeDriver();9 driver.manage().window().setSize(new Dimension(600, 800));10 driver.quit();11 }12}

Full Screen

Full Screen

Interface WebDriver.Options

Using AI Code Generation

copy

Full Screen

1package selenium;2import org.openqa.selenium.By;3import org.openqa.selenium.WebDriver;4import org.openqa.selenium.WebElement;5import org.openqa.selenium.chrome.ChromeDriver;6import org.openqa.selenium.chrome.ChromeOptions;7public class SeleniumWebDriverOptionsClass {8 public static void main(String[] args) {9 System.setProperty("webdriver.chrome.driver", "C:\\Users\\sandeep\\Downloads\\chromedriver_win32\\chromedriver.exe");10 ChromeOptions options = new ChromeOptions();11 options.addArguments("start-maximized");12 WebDriver driver = new ChromeDriver(options);13 WebElement element = driver.findElement(By.name("q"));14 element.sendKeys("Selenium");15 element.submit();16 System.out.println("Page title is: " + driver.getTitle());17 driver.quit();18 }19}20package selenium;21import org.openqa.selenium.By;22import org.openqa.selenium

Full Screen

Full Screen

Interface WebDriver.Options

Using AI Code Generation

copy

Full Screen

1package org.example;2import org.openqa.selenium.*;3import org.openqa.selenium.chrome.ChromeDriver;4{5 public static void main( String[] args )6 {7 System.setProperty("webdriver.chrome.driver","C:\\Users\\User\\Downloads\\chromedriver_win32\\chromedriver.exe");8 WebDriver driver = new ChromeDriver();9 driver.manage().window().setSize(new Dimension(600, 800));10 driver.quit();11 }12}

Full Screen

Full Screen

Selenium 4 Tutorial:

LambdaTest’s Selenium 4 tutorial is covering every aspects of Selenium 4 testing with examples and best practices. Here you will learn basics, such as how to upgrade from Selenium 3 to Selenium 4, to some advanced concepts, such as Relative locators and Selenium Grid 4 for Distributed testing. Also will learn new features of Selenium 4, such as capturing screenshots of specific elements, opening a new tab or window on the browser, and new protocol adoptions.

Chapters:

  1. Upgrading From Selenium 3 To Selenium 4?: In this chapter, learn in detail how to update Selenium 3 to Selenium 4 for Java binding. Also, learn how to upgrade while using different build tools such as Maven or Gradle and get comprehensive guidance for upgrading Selenium.

  2. What’s New In Selenium 4 & What’s Being Deprecated? : Get all information about new implementations in Selenium 4, such as W3S protocol adaption, Optimized Selenium Grid, and Enhanced Selenium IDE. Also, learn what is deprecated for Selenium 4, such as DesiredCapabilites and FindsBy methods, etc.

  3. Selenium 4 With Python: Selenium supports all major languages, such as Python, C#, Ruby, and JavaScript. In this chapter, learn how to install Selenium 4 for Python and the features of Python in Selenium 4, such as Relative locators, Browser manipulation, and Chrom DevTool protocol.

  4. Selenium 4 Is Now W3C Compliant: JSON Wireframe protocol is retiring from Selenium 4, and they are adopting W3C protocol to learn in detail about the advantages and impact of these changes.

  5. How To Use Selenium 4 Relative Locator? : Selenium 4 came with new features such as Relative Locators that allow constructing locators with reference and easily located constructors nearby. Get to know its different use cases with examples.

  6. Selenium Grid 4 Tutorial For Distributed Testing: Selenium Grid 4 allows you to perform tests over different browsers, OS, and device combinations. It also enables parallel execution browser testing, reads up on various features of Selenium Grid 4 and how to download it, and runs a test on Selenium Grid 4 with best practices.

  7. Selenium Video Tutorials: Binge on video tutorials on Selenium by industry experts to get step-by-step direction from automating basic to complex test scenarios with Selenium.

Selenium 101 certifications:

LambdaTest also provides certification for Selenium testing to accelerate your career in Selenium automation testing.

Run Selenium automation tests on LambdaTest cloud grid

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

Test Your Web Or Mobile Apps On 3000+ Browsers

Signup for free

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful