The primary unique argument for starting a local driver includes information about starting the required driver service
on the local machine.
Service object applies only to local drivers and provides information about the browser
driver
WebDriverdriver=newChromeDriver(chromeOptions);
Show full example
packagedev.selenium.drivers;importdev.selenium.BaseTest;importjava.time.Duration;importjava.time.temporal.ChronoUnit;importorg.junit.jupiter.api.Test;importorg.junit.jupiter.api.Assertions;importorg.openqa.selenium.PageLoadStrategy;importorg.openqa.selenium.UnexpectedAlertBehaviour;importorg.openqa.selenium.WebDriver;importorg.openqa.selenium.chrome.ChromeOptions;importorg.openqa.selenium.remote.CapabilityType;importorg.openqa.selenium.chrome.ChromeDriver;publicclassOptionsTestextendsBaseTest{@TestpublicvoidsetPageLoadStrategyNormal(){ChromeOptionschromeOptions=getDefaultChromeOptions();chromeOptions.setPageLoadStrategy(PageLoadStrategy.NORMAL);WebDriverdriver=newChromeDriver(chromeOptions);try{// Navigate to Urldriver.get("https://selenium.dev");}finally{driver.quit();}}@TestpublicvoidsetPageLoadStrategyEager(){ChromeOptionschromeOptions=getDefaultChromeOptions();chromeOptions.setPageLoadStrategy(PageLoadStrategy.EAGER);WebDriverdriver=newChromeDriver(chromeOptions);try{// Navigate to Urldriver.get("https://selenium.dev");}finally{driver.quit();}}@TestpublicvoidsetPageLoadStrategyNone(){ChromeOptionschromeOptions=getDefaultChromeOptions();chromeOptions.setPageLoadStrategy(PageLoadStrategy.NONE);WebDriverdriver=newChromeDriver(chromeOptions);try{// Navigate to Urldriver.get("https://selenium.dev");}finally{driver.quit();}}@TestpublicvoidsetAcceptInsecureCerts(){ChromeOptionschromeOptions=getDefaultChromeOptions();chromeOptions.setAcceptInsecureCerts(true);WebDriverdriver=newChromeDriver(chromeOptions);try{// Navigate to Urldriver.get("https://selenium.dev");}finally{driver.quit();}}@TestpublicvoidgetBrowserName(){ChromeOptionschromeOptions=getDefaultChromeOptions();Stringname=chromeOptions.getBrowserName();Assertions.assertFalse(name.isEmpty(),"Browser name should not be empty");}@TestpublicvoidsetBrowserVersion(){ChromeOptionschromeOptions=getDefaultChromeOptions();Stringversion="latest";chromeOptions.setBrowserVersion(version);Assertions.assertEquals(version,chromeOptions.getBrowserVersion());}@TestpublicvoidsetPlatformName(){ChromeOptionschromeOptions=getDefaultChromeOptions();Stringplatform="OS X 10.6";chromeOptions.setPlatformName(platform);Assertions.assertEquals(platform,chromeOptions.getPlatformName().toString());}@TestpublicvoidsetScriptTimeout(){ChromeOptionschromeOptions=getDefaultChromeOptions();Durationduration=Duration.of(5,ChronoUnit.SECONDS);chromeOptions.setScriptTimeout(duration);WebDriverdriver=newChromeDriver(chromeOptions);try{Durationtimeout=driver.manage().timeouts().getScriptTimeout();Assertions.assertEquals(timeout,duration,"The script timeout should be set to 5 seconds.");}finally{driver.quit();}}@TestpublicvoidsetPageLoadTimeout(){ChromeOptionschromeOptions=getDefaultChromeOptions();Durationduration=Duration.of(5,ChronoUnit.SECONDS);chromeOptions.setPageLoadTimeout(duration);WebDriverdriver=newChromeDriver(chromeOptions);try{Durationtimeout=driver.manage().timeouts().getPageLoadTimeout();Assertions.assertEquals(timeout,duration,"The page load timeout should be set to 5 seconds.");}finally{driver.quit();}}@TestpublicvoidsetImplicitWaitTimeout(){ChromeOptionschromeOptions=getDefaultChromeOptions();Durationduration=Duration.of(5,ChronoUnit.SECONDS);chromeOptions.setImplicitWaitTimeout(duration);WebDriverdriver=newChromeDriver(chromeOptions);try{Durationtimeout=driver.manage().timeouts().getImplicitWaitTimeout();Assertions.assertEquals(timeout,duration,"The implicit wait timeout should be set to 5 seconds.");}finally{driver.quit();}}@TestpublicvoidsetUnhandledPromptBehaviour(){ChromeOptionschromeOptions=getDefaultChromeOptions();chromeOptions.setUnhandledPromptBehaviour(UnexpectedAlertBehaviour.DISMISS_AND_NOTIFY);//verify the capability object is not nullObjectcapabilityObject=chromeOptions.getCapability(CapabilityType.UNHANDLED_PROMPT_BEHAVIOUR);Assertions.assertNotNull(capabilityObject,"Capability UNHANDLED_PROMPT_BEHAVIOUR should not be null.");Assertions.assertEquals(capabilityObject.toString(),UnexpectedAlertBehaviour.DISMISS_AND_NOTIFY.toString());}@TestpublicvoidsetWindowRect(){ChromeOptionschromeOptions=getDefaultChromeOptions();chromeOptions.setCapability(CapabilityType.SET_WINDOW_RECT,true);//verify the capability object is not nullObjectcapabilityObject=chromeOptions.getCapability(CapabilityType.SET_WINDOW_RECT);Assertions.assertNotNull(capabilityObject,"Capability SET_WINDOW_RECT should not be null.");Booleancapability=(Boolean)capabilityObject;Assertions.assertTrue(capability,"The capability SET_WINDOW_RECT should be set to true.");}@TestpublicvoidsetStrictFileInteractability(){ChromeOptionschromeOptions=getDefaultChromeOptions();chromeOptions.setCapability(CapabilityType.STRICT_FILE_INTERACTABILITY,true);//verify the capability object is not nullObjectcapabilityObject=chromeOptions.getCapability(CapabilityType.STRICT_FILE_INTERACTABILITY);Assertions.assertNotNull(capabilityObject,"Capability STRICT_FILE_INTERACTABILITY should not be null.");Booleancapability=(Boolean)capabilityObject;Assertions.assertTrue(capability,"The capability STRICT_FILE_INTERACTABILITY should be set to true.");}}
fromseleniumimportwebdriverfromselenium.webdriver.common.proxyimportProxyfromselenium.webdriver.common.proxyimportProxyTypedeftest_page_load_strategy_normal():options=get_default_chrome_options()options.page_load_strategy='normal'driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_page_load_strategy_eager():options=get_default_chrome_options()options.page_load_strategy='eager'driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_page_load_strategy_none():options=get_default_chrome_options()options.page_load_strategy='none'driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_timeouts_script():options=get_default_chrome_options()options.timeouts={'script':5000}driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_timeouts_page_load():options=get_default_chrome_options()options.timeouts={'pageLoad':5000}driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_timeouts_implicit_wait():options=get_default_chrome_options()options.timeouts={'implicit':5000}driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_unhandled_prompt():options=get_default_chrome_options()options.unhandled_prompt_behavior='accept'driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_set_window_rect():options=webdriver.FirefoxOptions()options.set_window_rect=True# Full support in Firefoxdriver=webdriver.Firefox(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_strict_file_interactability():options=get_default_chrome_options()options.strict_file_interactability=Truedriver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_proxy():options=get_default_chrome_options()options.proxy=Proxy({'proxyType':ProxyType.MANUAL,'httpProxy':'http.proxy:1234'})driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_set_browser_name():options=get_default_chrome_options()assertoptions.capabilities['browserName']=='chrome'driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_set_browser_version():options=get_default_chrome_options()options.browser_version='stable'assertoptions.capabilities['browserVersion']=='stable'driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_platform_name():options=get_default_chrome_options()options.platform_name='any'driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_accept_insecure_certs():options=get_default_chrome_options()options.accept_insecure_certs=Truedriver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()defget_default_chrome_options():options=webdriver.ChromeOptions()options.add_argument("--no-sandbox")returnoptions
usingSystem;usingSystem.Diagnostics;usingSystem.IO;usingSystem.Net;usingSystem.Net.Http;usingSystem.Net.Sockets;usingSystem.Runtime.InteropServices;usingSystem.Threading.Tasks;usingMicrosoft.VisualStudio.TestTools.UnitTesting;usingOpenQA.Selenium;usingOpenQA.Selenium.Chrome;namespaceSeleniumDocs{publicclassBaseTest{protectedIWebDriverdriver;protectedUriGridUrl;privateProcess_webserverProcess;privateconststringServerJarName="selenium-server-4.31.0.jar";privatestaticreadonlystringBaseDirectory=AppContext.BaseDirectory;privateconststringRelativePathToGrid="../../../../../";privatereadonlystring_examplesDirectory=Path.GetFullPath(Path.Combine(BaseDirectory,RelativePathToGrid)); [TestCleanup]publicvoidCleanup(){driver?.Quit();if(_webserverProcess!=null){StopServer();}}protectedvoidStartDriver(stringbrowserVersion="stable"){ChromeOptionsoptions=newChromeOptions{BrowserVersion=browserVersion};driver=newChromeDriver(options);}protectedasyncTaskStartServer(){if(_webserverProcess==null||_webserverProcess.HasExited){_webserverProcess=newProcess();_webserverProcess.StartInfo.FileName=RuntimeInformation.IsOSPlatform(OSPlatform.Windows)?"java.exe":"java";stringport=GetFreeTcpPort().ToString();GridUrl=newUri("http://localhost:"+port+"/wd/hub");_webserverProcess.StartInfo.Arguments=" -jar "+ServerJarName+" standalone --port "+port+" --selenium-manager true --enable-managed-downloads true";_webserverProcess.StartInfo.WorkingDirectory=_examplesDirectory;_webserverProcess.Start();awaitEnsureGridIsRunningAsync();}}privatevoidStopServer(){if(_webserverProcess!=null&&!_webserverProcess.HasExited){_webserverProcess.Kill();_webserverProcess.Dispose();_webserverProcess=null;}}privatestaticintGetFreeTcpPort(){TcpListenerl=newTcpListener(IPAddress.Loopback,0);l.Start();intport=((IPEndPoint)l.LocalEndpoint).Port;l.Stop();returnport;}privateasyncTaskEnsureGridIsRunningAsync(){DateTimetimeout=DateTime.Now.Add(TimeSpan.FromSeconds(30));boolisRunning=false;HttpClientclient=newHttpClient();while(!isRunning&&DateTime.Now<timeout){try{HttpResponseMessageresponse=awaitclient.GetAsync(GridUrl+"/status");if(response.IsSuccessStatusCode){isRunning=true;}else{awaitTask.Delay(500);}}catch(HttpRequestException){awaitTask.Delay(500);}}if(!isRunning){thrownewTimeoutException("Could not confirm the remote selenium server is running within 30 seconds");}}}}
# frozen_string_literal: truerequire'spec_helper'RSpec.describe'Chrome'dodescribe'Driver Options'dolet(:chrome_location){driver_finder&&ENV.fetch('CHROME_BIN',nil)}let(:url){'https://www.selenium.dev/selenium/web/'}it'page load strategy normal'dooptions=Selenium::WebDriver::Options.chromeoptions.page_load_strategy=:normaldriver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'page load strategy eager'dooptions=Selenium::WebDriver::Options.chromeoptions.page_load_strategy=:eagerdriver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'page load strategy none'dooptions=Selenium::WebDriver::Options.chromeoptions.page_load_strategy=:nonedriver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets remote capabilities',skip:'this is example code that will not execute'dooptions=Selenium::WebDriver::Options.firefoxoptions.platform_name='Windows 10'options.browser_version='latest'cloud_options={}cloud_options[:build]=my_test_buildcloud_options[:name]=my_test_nameoptions.add_option('cloud:options',cloud_options)driver=Selenium::WebDriver.for:remote,capabilities:optionsdriver.get(url)driver.quitendit'accepts untrusted certificates'dooptions=Selenium::WebDriver::Options.chromeoptions.accept_insecure_certs=truedriver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets unhandled prompt behavior'dooptions=Selenium::WebDriver::Options.chromeoptions.unhandled_prompt_behavior=:acceptdriver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets window rect'dooptions=Selenium::WebDriver::Options.firefoxoptions.set_window_rect=truedriver=Selenium::WebDriver.for:firefox,options:optionsdriver.get(url)driver.quitendit'sets strict file interactability'dooptions=Selenium::WebDriver::Options.chromeoptions.strict_file_interactability=truedriver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets the proxy'dooptions=Selenium::WebDriver::Options.chromeoptions.proxy=Selenium::WebDriver::Proxy.new(http:'myproxy.com:8080')driver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets the implicit timeout'dooptions=Selenium::WebDriver::Options.chromeoptions.timeouts={implicit:1}driver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets the page load timeout'dooptions=Selenium::WebDriver::Options.chromeoptions.timeouts={page_load:400_000}driver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets the script timeout'dooptions=Selenium::WebDriver::Options.chromeoptions.timeouts={script:40_000}driver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets capabilities in the pre-selenium 4 way',skip:'this is example code that will not execute'docaps=Selenium::WebDriver::Remote::Capabilities.firefoxcaps[:platform]='Windows 10'caps[:version]='92'caps[:build]=my_test_buildcaps[:name]=my_test_namedriver=Selenium::WebDriver.for:remote,url:cloud_url,desired_capabilities:capsdriver.get(url)driver.quitendendend
The primary unique argument for starting a remote driver includes information about where to execute the code.
Read the details in the Remote Driver Section
fromseleniumimportwebdriverfromselenium.webdriver.common.proxyimportProxyfromselenium.webdriver.common.proxyimportProxyTypedeftest_page_load_strategy_normal():options=get_default_chrome_options()options.page_load_strategy='normal'driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_page_load_strategy_eager():options=get_default_chrome_options()options.page_load_strategy='eager'driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_page_load_strategy_none():options=get_default_chrome_options()options.page_load_strategy='none'driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_timeouts_script():options=get_default_chrome_options()options.timeouts={'script':5000}driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_timeouts_page_load():options=get_default_chrome_options()options.timeouts={'pageLoad':5000}driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_timeouts_implicit_wait():options=get_default_chrome_options()options.timeouts={'implicit':5000}driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_unhandled_prompt():options=get_default_chrome_options()options.unhandled_prompt_behavior='accept'driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_set_window_rect():options=webdriver.FirefoxOptions()options.set_window_rect=True# Full support in Firefoxdriver=webdriver.Firefox(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_strict_file_interactability():options=get_default_chrome_options()options.strict_file_interactability=Truedriver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_proxy():options=get_default_chrome_options()options.proxy=Proxy({'proxyType':ProxyType.MANUAL,'httpProxy':'http.proxy:1234'})driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_set_browser_name():options=get_default_chrome_options()assertoptions.capabilities['browserName']=='chrome'driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_set_browser_version():options=get_default_chrome_options()options.browser_version='stable'assertoptions.capabilities['browserVersion']=='stable'driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_platform_name():options=get_default_chrome_options()options.platform_name='any'driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_accept_insecure_certs():options=get_default_chrome_options()options.accept_insecure_certs=Truedriver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()defget_default_chrome_options():options=webdriver.ChromeOptions()options.add_argument("--no-sandbox")returnoptions
# frozen_string_literal: truerequire'spec_helper'RSpec.describe'Chrome'dodescribe'Driver Options'dolet(:chrome_location){driver_finder&&ENV.fetch('CHROME_BIN',nil)}let(:url){'https://www.selenium.dev/selenium/web/'}it'page load strategy normal'dooptions=Selenium::WebDriver::Options.chromeoptions.page_load_strategy=:normaldriver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'page load strategy eager'dooptions=Selenium::WebDriver::Options.chromeoptions.page_load_strategy=:eagerdriver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'page load strategy none'dooptions=Selenium::WebDriver::Options.chromeoptions.page_load_strategy=:nonedriver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets remote capabilities',skip:'this is example code that will not execute'dooptions=Selenium::WebDriver::Options.firefoxoptions.platform_name='Windows 10'options.browser_version='latest'cloud_options={}cloud_options[:build]=my_test_buildcloud_options[:name]=my_test_nameoptions.add_option('cloud:options',cloud_options)driver=Selenium::WebDriver.for:remote,capabilities:optionsdriver.get(url)driver.quitendit'accepts untrusted certificates'dooptions=Selenium::WebDriver::Options.chromeoptions.accept_insecure_certs=truedriver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets unhandled prompt behavior'dooptions=Selenium::WebDriver::Options.chromeoptions.unhandled_prompt_behavior=:acceptdriver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets window rect'dooptions=Selenium::WebDriver::Options.firefoxoptions.set_window_rect=truedriver=Selenium::WebDriver.for:firefox,options:optionsdriver.get(url)driver.quitendit'sets strict file interactability'dooptions=Selenium::WebDriver::Options.chromeoptions.strict_file_interactability=truedriver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets the proxy'dooptions=Selenium::WebDriver::Options.chromeoptions.proxy=Selenium::WebDriver::Proxy.new(http:'myproxy.com:8080')driver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets the implicit timeout'dooptions=Selenium::WebDriver::Options.chromeoptions.timeouts={implicit:1}driver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets the page load timeout'dooptions=Selenium::WebDriver::Options.chromeoptions.timeouts={page_load:400_000}driver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets the script timeout'dooptions=Selenium::WebDriver::Options.chromeoptions.timeouts={script:40_000}driver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets capabilities in the pre-selenium 4 way',skip:'this is example code that will not execute'docaps=Selenium::WebDriver::Remote::Capabilities.firefoxcaps[:platform]='Windows 10'caps[:version]='92'caps[:build]=my_test_buildcaps[:name]=my_test_namedriver=Selenium::WebDriver.for:remote,url:cloud_url,desired_capabilities:capsdriver.get(url)driver.quitendendend
In Selenium 3, capabilities were defined in a session by using Desired Capabilities classes.
As of Selenium 4, you must use the browser options classes.
For remote driver sessions, a browser options instance is required as it determines which browser will be used.
These options are described in the w3c specification for Capabilities.
Each browser has custom options that may be defined in addition to the ones defined in the specification.
browserName
Browser name is set by default when using an Options class instance.
packagedev.selenium.drivers;importdev.selenium.BaseTest;importjava.time.Duration;importjava.time.temporal.ChronoUnit;importorg.junit.jupiter.api.Test;importorg.junit.jupiter.api.Assertions;importorg.openqa.selenium.PageLoadStrategy;importorg.openqa.selenium.UnexpectedAlertBehaviour;importorg.openqa.selenium.WebDriver;importorg.openqa.selenium.chrome.ChromeOptions;importorg.openqa.selenium.remote.CapabilityType;importorg.openqa.selenium.chrome.ChromeDriver;publicclassOptionsTestextendsBaseTest{@TestpublicvoidsetPageLoadStrategyNormal(){ChromeOptionschromeOptions=getDefaultChromeOptions();chromeOptions.setPageLoadStrategy(PageLoadStrategy.NORMAL);WebDriverdriver=newChromeDriver(chromeOptions);try{// Navigate to Urldriver.get("https://selenium.dev");}finally{driver.quit();}}@TestpublicvoidsetPageLoadStrategyEager(){ChromeOptionschromeOptions=getDefaultChromeOptions();chromeOptions.setPageLoadStrategy(PageLoadStrategy.EAGER);WebDriverdriver=newChromeDriver(chromeOptions);try{// Navigate to Urldriver.get("https://selenium.dev");}finally{driver.quit();}}@TestpublicvoidsetPageLoadStrategyNone(){ChromeOptionschromeOptions=getDefaultChromeOptions();chromeOptions.setPageLoadStrategy(PageLoadStrategy.NONE);WebDriverdriver=newChromeDriver(chromeOptions);try{// Navigate to Urldriver.get("https://selenium.dev");}finally{driver.quit();}}@TestpublicvoidsetAcceptInsecureCerts(){ChromeOptionschromeOptions=getDefaultChromeOptions();chromeOptions.setAcceptInsecureCerts(true);WebDriverdriver=newChromeDriver(chromeOptions);try{// Navigate to Urldriver.get("https://selenium.dev");}finally{driver.quit();}}@TestpublicvoidgetBrowserName(){ChromeOptionschromeOptions=getDefaultChromeOptions();Stringname=chromeOptions.getBrowserName();Assertions.assertFalse(name.isEmpty(),"Browser name should not be empty");}@TestpublicvoidsetBrowserVersion(){ChromeOptionschromeOptions=getDefaultChromeOptions();Stringversion="latest";chromeOptions.setBrowserVersion(version);Assertions.assertEquals(version,chromeOptions.getBrowserVersion());}@TestpublicvoidsetPlatformName(){ChromeOptionschromeOptions=getDefaultChromeOptions();Stringplatform="OS X 10.6";chromeOptions.setPlatformName(platform);Assertions.assertEquals(platform,chromeOptions.getPlatformName().toString());}@TestpublicvoidsetScriptTimeout(){ChromeOptionschromeOptions=getDefaultChromeOptions();Durationduration=Duration.of(5,ChronoUnit.SECONDS);chromeOptions.setScriptTimeout(duration);WebDriverdriver=newChromeDriver(chromeOptions);try{Durationtimeout=driver.manage().timeouts().getScriptTimeout();Assertions.assertEquals(timeout,duration,"The script timeout should be set to 5 seconds.");}finally{driver.quit();}}@TestpublicvoidsetPageLoadTimeout(){ChromeOptionschromeOptions=getDefaultChromeOptions();Durationduration=Duration.of(5,ChronoUnit.SECONDS);chromeOptions.setPageLoadTimeout(duration);WebDriverdriver=newChromeDriver(chromeOptions);try{Durationtimeout=driver.manage().timeouts().getPageLoadTimeout();Assertions.assertEquals(timeout,duration,"The page load timeout should be set to 5 seconds.");}finally{driver.quit();}}@TestpublicvoidsetImplicitWaitTimeout(){ChromeOptionschromeOptions=getDefaultChromeOptions();Durationduration=Duration.of(5,ChronoUnit.SECONDS);chromeOptions.setImplicitWaitTimeout(duration);WebDriverdriver=newChromeDriver(chromeOptions);try{Durationtimeout=driver.manage().timeouts().getImplicitWaitTimeout();Assertions.assertEquals(timeout,duration,"The implicit wait timeout should be set to 5 seconds.");}finally{driver.quit();}}@TestpublicvoidsetUnhandledPromptBehaviour(){ChromeOptionschromeOptions=getDefaultChromeOptions();chromeOptions.setUnhandledPromptBehaviour(UnexpectedAlertBehaviour.DISMISS_AND_NOTIFY);//verify the capability object is not nullObjectcapabilityObject=chromeOptions.getCapability(CapabilityType.UNHANDLED_PROMPT_BEHAVIOUR);Assertions.assertNotNull(capabilityObject,"Capability UNHANDLED_PROMPT_BEHAVIOUR should not be null.");Assertions.assertEquals(capabilityObject.toString(),UnexpectedAlertBehaviour.DISMISS_AND_NOTIFY.toString());}@TestpublicvoidsetWindowRect(){ChromeOptionschromeOptions=getDefaultChromeOptions();chromeOptions.setCapability(CapabilityType.SET_WINDOW_RECT,true);//verify the capability object is not nullObjectcapabilityObject=chromeOptions.getCapability(CapabilityType.SET_WINDOW_RECT);Assertions.assertNotNull(capabilityObject,"Capability SET_WINDOW_RECT should not be null.");Booleancapability=(Boolean)capabilityObject;Assertions.assertTrue(capability,"The capability SET_WINDOW_RECT should be set to true.");}@TestpublicvoidsetStrictFileInteractability(){ChromeOptionschromeOptions=getDefaultChromeOptions();chromeOptions.setCapability(CapabilityType.STRICT_FILE_INTERACTABILITY,true);//verify the capability object is not nullObjectcapabilityObject=chromeOptions.getCapability(CapabilityType.STRICT_FILE_INTERACTABILITY);Assertions.assertNotNull(capabilityObject,"Capability STRICT_FILE_INTERACTABILITY should not be null.");Booleancapability=(Boolean)capabilityObject;Assertions.assertTrue(capability,"The capability STRICT_FILE_INTERACTABILITY should be set to true.");}}
fromseleniumimportwebdriverfromselenium.webdriver.common.proxyimportProxyfromselenium.webdriver.common.proxyimportProxyTypedeftest_page_load_strategy_normal():options=get_default_chrome_options()options.page_load_strategy='normal'driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_page_load_strategy_eager():options=get_default_chrome_options()options.page_load_strategy='eager'driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_page_load_strategy_none():options=get_default_chrome_options()options.page_load_strategy='none'driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_timeouts_script():options=get_default_chrome_options()options.timeouts={'script':5000}driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_timeouts_page_load():options=get_default_chrome_options()options.timeouts={'pageLoad':5000}driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_timeouts_implicit_wait():options=get_default_chrome_options()options.timeouts={'implicit':5000}driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_unhandled_prompt():options=get_default_chrome_options()options.unhandled_prompt_behavior='accept'driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_set_window_rect():options=webdriver.FirefoxOptions()options.set_window_rect=True# Full support in Firefoxdriver=webdriver.Firefox(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_strict_file_interactability():options=get_default_chrome_options()options.strict_file_interactability=Truedriver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_proxy():options=get_default_chrome_options()options.proxy=Proxy({'proxyType':ProxyType.MANUAL,'httpProxy':'http.proxy:1234'})driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_set_browser_name():options=get_default_chrome_options()assertoptions.capabilities['browserName']=='chrome'driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_set_browser_version():options=get_default_chrome_options()options.browser_version='stable'assertoptions.capabilities['browserVersion']=='stable'driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_platform_name():options=get_default_chrome_options()options.platform_name='any'driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_accept_insecure_certs():options=get_default_chrome_options()options.accept_insecure_certs=Truedriver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()defget_default_chrome_options():options=webdriver.ChromeOptions()options.add_argument("--no-sandbox")returnoptions
# frozen_string_literal: truerequire'spec_helper'RSpec.describe'Chrome'dodescribe'Driver Options'dolet(:chrome_location){driver_finder&&ENV.fetch('CHROME_BIN',nil)}let(:url){'https://www.selenium.dev/selenium/web/'}it'page load strategy normal'dooptions=Selenium::WebDriver::Options.chromeoptions.page_load_strategy=:normaldriver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'page load strategy eager'dooptions=Selenium::WebDriver::Options.chromeoptions.page_load_strategy=:eagerdriver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'page load strategy none'dooptions=Selenium::WebDriver::Options.chromeoptions.page_load_strategy=:nonedriver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets remote capabilities',skip:'this is example code that will not execute'dooptions=Selenium::WebDriver::Options.firefoxoptions.platform_name='Windows 10'options.browser_version='latest'cloud_options={}cloud_options[:build]=my_test_buildcloud_options[:name]=my_test_nameoptions.add_option('cloud:options',cloud_options)driver=Selenium::WebDriver.for:remote,capabilities:optionsdriver.get(url)driver.quitendit'accepts untrusted certificates'dooptions=Selenium::WebDriver::Options.chromeoptions.accept_insecure_certs=truedriver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets unhandled prompt behavior'dooptions=Selenium::WebDriver::Options.chromeoptions.unhandled_prompt_behavior=:acceptdriver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets window rect'dooptions=Selenium::WebDriver::Options.firefoxoptions.set_window_rect=truedriver=Selenium::WebDriver.for:firefox,options:optionsdriver.get(url)driver.quitendit'sets strict file interactability'dooptions=Selenium::WebDriver::Options.chromeoptions.strict_file_interactability=truedriver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets the proxy'dooptions=Selenium::WebDriver::Options.chromeoptions.proxy=Selenium::WebDriver::Proxy.new(http:'myproxy.com:8080')driver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets the implicit timeout'dooptions=Selenium::WebDriver::Options.chromeoptions.timeouts={implicit:1}driver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets the page load timeout'dooptions=Selenium::WebDriver::Options.chromeoptions.timeouts={page_load:400_000}driver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets the script timeout'dooptions=Selenium::WebDriver::Options.chromeoptions.timeouts={script:40_000}driver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets capabilities in the pre-selenium 4 way',skip:'this is example code that will not execute'docaps=Selenium::WebDriver::Remote::Capabilities.firefoxcaps[:platform]='Windows 10'caps[:version]='92'caps[:build]=my_test_buildcaps[:name]=my_test_namedriver=Selenium::WebDriver.for:remote,url:cloud_url,desired_capabilities:capsdriver.get(url)driver.quitendendend
This capability is optional, this is used to set the available browser version at remote end.
In recent versions of Selenium, if the version is not found on the system,
it will be automatically downloaded by Selenium Manager
packagedev.selenium.drivers;importdev.selenium.BaseTest;importjava.time.Duration;importjava.time.temporal.ChronoUnit;importorg.junit.jupiter.api.Test;importorg.junit.jupiter.api.Assertions;importorg.openqa.selenium.PageLoadStrategy;importorg.openqa.selenium.UnexpectedAlertBehaviour;importorg.openqa.selenium.WebDriver;importorg.openqa.selenium.chrome.ChromeOptions;importorg.openqa.selenium.remote.CapabilityType;importorg.openqa.selenium.chrome.ChromeDriver;publicclassOptionsTestextendsBaseTest{@TestpublicvoidsetPageLoadStrategyNormal(){ChromeOptionschromeOptions=getDefaultChromeOptions();chromeOptions.setPageLoadStrategy(PageLoadStrategy.NORMAL);WebDriverdriver=newChromeDriver(chromeOptions);try{// Navigate to Urldriver.get("https://selenium.dev");}finally{driver.quit();}}@TestpublicvoidsetPageLoadStrategyEager(){ChromeOptionschromeOptions=getDefaultChromeOptions();chromeOptions.setPageLoadStrategy(PageLoadStrategy.EAGER);WebDriverdriver=newChromeDriver(chromeOptions);try{// Navigate to Urldriver.get("https://selenium.dev");}finally{driver.quit();}}@TestpublicvoidsetPageLoadStrategyNone(){ChromeOptionschromeOptions=getDefaultChromeOptions();chromeOptions.setPageLoadStrategy(PageLoadStrategy.NONE);WebDriverdriver=newChromeDriver(chromeOptions);try{// Navigate to Urldriver.get("https://selenium.dev");}finally{driver.quit();}}@TestpublicvoidsetAcceptInsecureCerts(){ChromeOptionschromeOptions=getDefaultChromeOptions();chromeOptions.setAcceptInsecureCerts(true);WebDriverdriver=newChromeDriver(chromeOptions);try{// Navigate to Urldriver.get("https://selenium.dev");}finally{driver.quit();}}@TestpublicvoidgetBrowserName(){ChromeOptionschromeOptions=getDefaultChromeOptions();Stringname=chromeOptions.getBrowserName();Assertions.assertFalse(name.isEmpty(),"Browser name should not be empty");}@TestpublicvoidsetBrowserVersion(){ChromeOptionschromeOptions=getDefaultChromeOptions();Stringversion="latest";chromeOptions.setBrowserVersion(version);Assertions.assertEquals(version,chromeOptions.getBrowserVersion());}@TestpublicvoidsetPlatformName(){ChromeOptionschromeOptions=getDefaultChromeOptions();Stringplatform="OS X 10.6";chromeOptions.setPlatformName(platform);Assertions.assertEquals(platform,chromeOptions.getPlatformName().toString());}@TestpublicvoidsetScriptTimeout(){ChromeOptionschromeOptions=getDefaultChromeOptions();Durationduration=Duration.of(5,ChronoUnit.SECONDS);chromeOptions.setScriptTimeout(duration);WebDriverdriver=newChromeDriver(chromeOptions);try{Durationtimeout=driver.manage().timeouts().getScriptTimeout();Assertions.assertEquals(timeout,duration,"The script timeout should be set to 5 seconds.");}finally{driver.quit();}}@TestpublicvoidsetPageLoadTimeout(){ChromeOptionschromeOptions=getDefaultChromeOptions();Durationduration=Duration.of(5,ChronoUnit.SECONDS);chromeOptions.setPageLoadTimeout(duration);WebDriverdriver=newChromeDriver(chromeOptions);try{Durationtimeout=driver.manage().timeouts().getPageLoadTimeout();Assertions.assertEquals(timeout,duration,"The page load timeout should be set to 5 seconds.");}finally{driver.quit();}}@TestpublicvoidsetImplicitWaitTimeout(){ChromeOptionschromeOptions=getDefaultChromeOptions();Durationduration=Duration.of(5,ChronoUnit.SECONDS);chromeOptions.setImplicitWaitTimeout(duration);WebDriverdriver=newChromeDriver(chromeOptions);try{Durationtimeout=driver.manage().timeouts().getImplicitWaitTimeout();Assertions.assertEquals(timeout,duration,"The implicit wait timeout should be set to 5 seconds.");}finally{driver.quit();}}@TestpublicvoidsetUnhandledPromptBehaviour(){ChromeOptionschromeOptions=getDefaultChromeOptions();chromeOptions.setUnhandledPromptBehaviour(UnexpectedAlertBehaviour.DISMISS_AND_NOTIFY);//verify the capability object is not nullObjectcapabilityObject=chromeOptions.getCapability(CapabilityType.UNHANDLED_PROMPT_BEHAVIOUR);Assertions.assertNotNull(capabilityObject,"Capability UNHANDLED_PROMPT_BEHAVIOUR should not be null.");Assertions.assertEquals(capabilityObject.toString(),UnexpectedAlertBehaviour.DISMISS_AND_NOTIFY.toString());}@TestpublicvoidsetWindowRect(){ChromeOptionschromeOptions=getDefaultChromeOptions();chromeOptions.setCapability(CapabilityType.SET_WINDOW_RECT,true);//verify the capability object is not nullObjectcapabilityObject=chromeOptions.getCapability(CapabilityType.SET_WINDOW_RECT);Assertions.assertNotNull(capabilityObject,"Capability SET_WINDOW_RECT should not be null.");Booleancapability=(Boolean)capabilityObject;Assertions.assertTrue(capability,"The capability SET_WINDOW_RECT should be set to true.");}@TestpublicvoidsetStrictFileInteractability(){ChromeOptionschromeOptions=getDefaultChromeOptions();chromeOptions.setCapability(CapabilityType.STRICT_FILE_INTERACTABILITY,true);//verify the capability object is not nullObjectcapabilityObject=chromeOptions.getCapability(CapabilityType.STRICT_FILE_INTERACTABILITY);Assertions.assertNotNull(capabilityObject,"Capability STRICT_FILE_INTERACTABILITY should not be null.");Booleancapability=(Boolean)capabilityObject;Assertions.assertTrue(capability,"The capability STRICT_FILE_INTERACTABILITY should be set to true.");}}
fromseleniumimportwebdriverfromselenium.webdriver.common.proxyimportProxyfromselenium.webdriver.common.proxyimportProxyTypedeftest_page_load_strategy_normal():options=get_default_chrome_options()options.page_load_strategy='normal'driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_page_load_strategy_eager():options=get_default_chrome_options()options.page_load_strategy='eager'driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_page_load_strategy_none():options=get_default_chrome_options()options.page_load_strategy='none'driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_timeouts_script():options=get_default_chrome_options()options.timeouts={'script':5000}driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_timeouts_page_load():options=get_default_chrome_options()options.timeouts={'pageLoad':5000}driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_timeouts_implicit_wait():options=get_default_chrome_options()options.timeouts={'implicit':5000}driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_unhandled_prompt():options=get_default_chrome_options()options.unhandled_prompt_behavior='accept'driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_set_window_rect():options=webdriver.FirefoxOptions()options.set_window_rect=True# Full support in Firefoxdriver=webdriver.Firefox(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_strict_file_interactability():options=get_default_chrome_options()options.strict_file_interactability=Truedriver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_proxy():options=get_default_chrome_options()options.proxy=Proxy({'proxyType':ProxyType.MANUAL,'httpProxy':'http.proxy:1234'})driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_set_browser_name():options=get_default_chrome_options()assertoptions.capabilities['browserName']=='chrome'driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_set_browser_version():options=get_default_chrome_options()options.browser_version='stable'assertoptions.capabilities['browserVersion']=='stable'driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_platform_name():options=get_default_chrome_options()options.platform_name='any'driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_accept_insecure_certs():options=get_default_chrome_options()options.accept_insecure_certs=Truedriver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()defget_default_chrome_options():options=webdriver.ChromeOptions()options.add_argument("--no-sandbox")returnoptions
# frozen_string_literal: truerequire'spec_helper'RSpec.describe'Chrome'dodescribe'Driver Options'dolet(:chrome_location){driver_finder&&ENV.fetch('CHROME_BIN',nil)}let(:url){'https://www.selenium.dev/selenium/web/'}it'page load strategy normal'dooptions=Selenium::WebDriver::Options.chromeoptions.page_load_strategy=:normaldriver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'page load strategy eager'dooptions=Selenium::WebDriver::Options.chromeoptions.page_load_strategy=:eagerdriver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'page load strategy none'dooptions=Selenium::WebDriver::Options.chromeoptions.page_load_strategy=:nonedriver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets remote capabilities',skip:'this is example code that will not execute'dooptions=Selenium::WebDriver::Options.firefoxoptions.platform_name='Windows 10'options.browser_version='latest'cloud_options={}cloud_options[:build]=my_test_buildcloud_options[:name]=my_test_nameoptions.add_option('cloud:options',cloud_options)driver=Selenium::WebDriver.for:remote,capabilities:optionsdriver.get(url)driver.quitendit'accepts untrusted certificates'dooptions=Selenium::WebDriver::Options.chromeoptions.accept_insecure_certs=truedriver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets unhandled prompt behavior'dooptions=Selenium::WebDriver::Options.chromeoptions.unhandled_prompt_behavior=:acceptdriver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets window rect'dooptions=Selenium::WebDriver::Options.firefoxoptions.set_window_rect=truedriver=Selenium::WebDriver.for:firefox,options:optionsdriver.get(url)driver.quitendit'sets strict file interactability'dooptions=Selenium::WebDriver::Options.chromeoptions.strict_file_interactability=truedriver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets the proxy'dooptions=Selenium::WebDriver::Options.chromeoptions.proxy=Selenium::WebDriver::Proxy.new(http:'myproxy.com:8080')driver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets the implicit timeout'dooptions=Selenium::WebDriver::Options.chromeoptions.timeouts={implicit:1}driver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets the page load timeout'dooptions=Selenium::WebDriver::Options.chromeoptions.timeouts={page_load:400_000}driver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets the script timeout'dooptions=Selenium::WebDriver::Options.chromeoptions.timeouts={script:40_000}driver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets capabilities in the pre-selenium 4 way',skip:'this is example code that will not execute'docaps=Selenium::WebDriver::Remote::Capabilities.firefoxcaps[:platform]='Windows 10'caps[:version]='92'caps[:build]=my_test_buildcaps[:name]=my_test_namedriver=Selenium::WebDriver.for:remote,url:cloud_url,desired_capabilities:capsdriver.get(url)driver.quitendendend
Three types of page load strategies are available.
The page load strategy queries the
document.readyState
as described in the table below:
Strategy
Ready State
Notes
normal
complete
Used by default, waits for all resources to download
eager
interactive
DOM access is ready, but other resources like images may still be loading
none
Any
Does not block WebDriver at all
The document.readyState property of a document describes the loading state of the current document.
When navigating to a new page via URL, by default, WebDriver will hold off on completing a navigation
method (e.g., driver.navigate().get()) until the document ready state is complete. This does not
necessarily mean that the page has finished loading, especially for sites like Single Page Applications
that use JavaScript to dynamically load content after the Ready State returns complete. Note also
that this behavior does not apply to navigation that is a result of clicking an element or submitting a form.
If a page takes a long time to load as a result of downloading assets (e.g., images, css, js)
that aren’t important to the automation, you can change from the default parameter of normal to
eager or none to speed up the session. This value applies to the entire session, so make sure
that your waiting strategy is sufficient to minimize
flakiness.
normal (default)
WebDriver waits until the load
event fire is returned.
packagedev.selenium.drivers;importdev.selenium.BaseTest;importjava.time.Duration;importjava.time.temporal.ChronoUnit;importorg.junit.jupiter.api.Test;importorg.junit.jupiter.api.Assertions;importorg.openqa.selenium.PageLoadStrategy;importorg.openqa.selenium.UnexpectedAlertBehaviour;importorg.openqa.selenium.WebDriver;importorg.openqa.selenium.chrome.ChromeOptions;importorg.openqa.selenium.remote.CapabilityType;importorg.openqa.selenium.chrome.ChromeDriver;publicclassOptionsTestextendsBaseTest{@TestpublicvoidsetPageLoadStrategyNormal(){ChromeOptionschromeOptions=getDefaultChromeOptions();chromeOptions.setPageLoadStrategy(PageLoadStrategy.NORMAL);WebDriverdriver=newChromeDriver(chromeOptions);try{// Navigate to Urldriver.get("https://selenium.dev");}finally{driver.quit();}}@TestpublicvoidsetPageLoadStrategyEager(){ChromeOptionschromeOptions=getDefaultChromeOptions();chromeOptions.setPageLoadStrategy(PageLoadStrategy.EAGER);WebDriverdriver=newChromeDriver(chromeOptions);try{// Navigate to Urldriver.get("https://selenium.dev");}finally{driver.quit();}}@TestpublicvoidsetPageLoadStrategyNone(){ChromeOptionschromeOptions=getDefaultChromeOptions();chromeOptions.setPageLoadStrategy(PageLoadStrategy.NONE);WebDriverdriver=newChromeDriver(chromeOptions);try{// Navigate to Urldriver.get("https://selenium.dev");}finally{driver.quit();}}@TestpublicvoidsetAcceptInsecureCerts(){ChromeOptionschromeOptions=getDefaultChromeOptions();chromeOptions.setAcceptInsecureCerts(true);WebDriverdriver=newChromeDriver(chromeOptions);try{// Navigate to Urldriver.get("https://selenium.dev");}finally{driver.quit();}}@TestpublicvoidgetBrowserName(){ChromeOptionschromeOptions=getDefaultChromeOptions();Stringname=chromeOptions.getBrowserName();Assertions.assertFalse(name.isEmpty(),"Browser name should not be empty");}@TestpublicvoidsetBrowserVersion(){ChromeOptionschromeOptions=getDefaultChromeOptions();Stringversion="latest";chromeOptions.setBrowserVersion(version);Assertions.assertEquals(version,chromeOptions.getBrowserVersion());}@TestpublicvoidsetPlatformName(){ChromeOptionschromeOptions=getDefaultChromeOptions();Stringplatform="OS X 10.6";chromeOptions.setPlatformName(platform);Assertions.assertEquals(platform,chromeOptions.getPlatformName().toString());}@TestpublicvoidsetScriptTimeout(){ChromeOptionschromeOptions=getDefaultChromeOptions();Durationduration=Duration.of(5,ChronoUnit.SECONDS);chromeOptions.setScriptTimeout(duration);WebDriverdriver=newChromeDriver(chromeOptions);try{Durationtimeout=driver.manage().timeouts().getScriptTimeout();Assertions.assertEquals(timeout,duration,"The script timeout should be set to 5 seconds.");}finally{driver.quit();}}@TestpublicvoidsetPageLoadTimeout(){ChromeOptionschromeOptions=getDefaultChromeOptions();Durationduration=Duration.of(5,ChronoUnit.SECONDS);chromeOptions.setPageLoadTimeout(duration);WebDriverdriver=newChromeDriver(chromeOptions);try{Durationtimeout=driver.manage().timeouts().getPageLoadTimeout();Assertions.assertEquals(timeout,duration,"The page load timeout should be set to 5 seconds.");}finally{driver.quit();}}@TestpublicvoidsetImplicitWaitTimeout(){ChromeOptionschromeOptions=getDefaultChromeOptions();Durationduration=Duration.of(5,ChronoUnit.SECONDS);chromeOptions.setImplicitWaitTimeout(duration);WebDriverdriver=newChromeDriver(chromeOptions);try{Durationtimeout=driver.manage().timeouts().getImplicitWaitTimeout();Assertions.assertEquals(timeout,duration,"The implicit wait timeout should be set to 5 seconds.");}finally{driver.quit();}}@TestpublicvoidsetUnhandledPromptBehaviour(){ChromeOptionschromeOptions=getDefaultChromeOptions();chromeOptions.setUnhandledPromptBehaviour(UnexpectedAlertBehaviour.DISMISS_AND_NOTIFY);//verify the capability object is not nullObjectcapabilityObject=chromeOptions.getCapability(CapabilityType.UNHANDLED_PROMPT_BEHAVIOUR);Assertions.assertNotNull(capabilityObject,"Capability UNHANDLED_PROMPT_BEHAVIOUR should not be null.");Assertions.assertEquals(capabilityObject.toString(),UnexpectedAlertBehaviour.DISMISS_AND_NOTIFY.toString());}@TestpublicvoidsetWindowRect(){ChromeOptionschromeOptions=getDefaultChromeOptions();chromeOptions.setCapability(CapabilityType.SET_WINDOW_RECT,true);//verify the capability object is not nullObjectcapabilityObject=chromeOptions.getCapability(CapabilityType.SET_WINDOW_RECT);Assertions.assertNotNull(capabilityObject,"Capability SET_WINDOW_RECT should not be null.");Booleancapability=(Boolean)capabilityObject;Assertions.assertTrue(capability,"The capability SET_WINDOW_RECT should be set to true.");}@TestpublicvoidsetStrictFileInteractability(){ChromeOptionschromeOptions=getDefaultChromeOptions();chromeOptions.setCapability(CapabilityType.STRICT_FILE_INTERACTABILITY,true);//verify the capability object is not nullObjectcapabilityObject=chromeOptions.getCapability(CapabilityType.STRICT_FILE_INTERACTABILITY);Assertions.assertNotNull(capabilityObject,"Capability STRICT_FILE_INTERACTABILITY should not be null.");Booleancapability=(Boolean)capabilityObject;Assertions.assertTrue(capability,"The capability STRICT_FILE_INTERACTABILITY should be set to true.");}}
fromseleniumimportwebdriverfromselenium.webdriver.common.proxyimportProxyfromselenium.webdriver.common.proxyimportProxyTypedeftest_page_load_strategy_normal():options=get_default_chrome_options()options.page_load_strategy='normal'driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_page_load_strategy_eager():options=get_default_chrome_options()options.page_load_strategy='eager'driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_page_load_strategy_none():options=get_default_chrome_options()options.page_load_strategy='none'driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_timeouts_script():options=get_default_chrome_options()options.timeouts={'script':5000}driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_timeouts_page_load():options=get_default_chrome_options()options.timeouts={'pageLoad':5000}driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_timeouts_implicit_wait():options=get_default_chrome_options()options.timeouts={'implicit':5000}driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_unhandled_prompt():options=get_default_chrome_options()options.unhandled_prompt_behavior='accept'driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_set_window_rect():options=webdriver.FirefoxOptions()options.set_window_rect=True# Full support in Firefoxdriver=webdriver.Firefox(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_strict_file_interactability():options=get_default_chrome_options()options.strict_file_interactability=Truedriver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_proxy():options=get_default_chrome_options()options.proxy=Proxy({'proxyType':ProxyType.MANUAL,'httpProxy':'http.proxy:1234'})driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_set_browser_name():options=get_default_chrome_options()assertoptions.capabilities['browserName']=='chrome'driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_set_browser_version():options=get_default_chrome_options()options.browser_version='stable'assertoptions.capabilities['browserVersion']=='stable'driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_platform_name():options=get_default_chrome_options()options.platform_name='any'driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_accept_insecure_certs():options=get_default_chrome_options()options.accept_insecure_certs=Truedriver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()defget_default_chrome_options():options=webdriver.ChromeOptions()options.add_argument("--no-sandbox")returnoptions
# frozen_string_literal: truerequire'spec_helper'RSpec.describe'Chrome'dodescribe'Driver Options'dolet(:chrome_location){driver_finder&&ENV.fetch('CHROME_BIN',nil)}let(:url){'https://www.selenium.dev/selenium/web/'}it'page load strategy normal'dooptions=Selenium::WebDriver::Options.chromeoptions.page_load_strategy=:normaldriver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'page load strategy eager'dooptions=Selenium::WebDriver::Options.chromeoptions.page_load_strategy=:eagerdriver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'page load strategy none'dooptions=Selenium::WebDriver::Options.chromeoptions.page_load_strategy=:nonedriver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets remote capabilities',skip:'this is example code that will not execute'dooptions=Selenium::WebDriver::Options.firefoxoptions.platform_name='Windows 10'options.browser_version='latest'cloud_options={}cloud_options[:build]=my_test_buildcloud_options[:name]=my_test_nameoptions.add_option('cloud:options',cloud_options)driver=Selenium::WebDriver.for:remote,capabilities:optionsdriver.get(url)driver.quitendit'accepts untrusted certificates'dooptions=Selenium::WebDriver::Options.chromeoptions.accept_insecure_certs=truedriver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets unhandled prompt behavior'dooptions=Selenium::WebDriver::Options.chromeoptions.unhandled_prompt_behavior=:acceptdriver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets window rect'dooptions=Selenium::WebDriver::Options.firefoxoptions.set_window_rect=truedriver=Selenium::WebDriver.for:firefox,options:optionsdriver.get(url)driver.quitendit'sets strict file interactability'dooptions=Selenium::WebDriver::Options.chromeoptions.strict_file_interactability=truedriver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets the proxy'dooptions=Selenium::WebDriver::Options.chromeoptions.proxy=Selenium::WebDriver::Proxy.new(http:'myproxy.com:8080')driver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets the implicit timeout'dooptions=Selenium::WebDriver::Options.chromeoptions.timeouts={implicit:1}driver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets the page load timeout'dooptions=Selenium::WebDriver::Options.chromeoptions.timeouts={page_load:400_000}driver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets the script timeout'dooptions=Selenium::WebDriver::Options.chromeoptions.timeouts={script:40_000}driver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets capabilities in the pre-selenium 4 way',skip:'this is example code that will not execute'docaps=Selenium::WebDriver::Remote::Capabilities.firefoxcaps[:platform]='Windows 10'caps[:version]='92'caps[:build]=my_test_buildcaps[:name]=my_test_namedriver=Selenium::WebDriver.for:remote,url:cloud_url,desired_capabilities:capsdriver.get(url)driver.quitendendend
constChrome=require('selenium-webdriver/chrome');const{Browser,Builder}=require("selenium-webdriver");constoptions=newChrome.Options()describe('Page loading strategies',function(){it('Navigate using eager page loading strategy',asyncfunction(){letdriver=newBuilder().forBrowser(Browser.CHROME).setChromeOptions(options.setPageLoadStrategy('eager')).build();awaitdriver.get('https://www.selenium.dev/selenium/web/blank.html');awaitdriver.quit();});it('Navigate using none page loading strategy',asyncfunction(){letdriver=newBuilder().forBrowser(Browser.CHROME).setChromeOptions(options.setPageLoadStrategy('none')).build();awaitdriver.get('https://www.selenium.dev/selenium/web/blank.html');awaitdriver.quit();});it('Navigate using normal page loading strategy',asyncfunction(){letdriver=newBuilder().forBrowser(Browser.CHROME).setChromeOptions(options.setPageLoadStrategy('normal')).build();awaitdriver.get('https://www.selenium.dev/selenium/web/blank.html');awaitdriver.quit();});it('Should be able to accept certs',asyncfunction(){letdriver=newBuilder().forBrowser(Browser.CHROME).setChromeOptions(options.setAcceptInsecureCerts(true)).build();awaitdriver.get('https://www.selenium.dev/selenium/web/blank.html');awaitdriver.quit();});});
packagedev.selenium.drivers;importdev.selenium.BaseTest;importjava.time.Duration;importjava.time.temporal.ChronoUnit;importorg.junit.jupiter.api.Test;importorg.junit.jupiter.api.Assertions;importorg.openqa.selenium.PageLoadStrategy;importorg.openqa.selenium.UnexpectedAlertBehaviour;importorg.openqa.selenium.WebDriver;importorg.openqa.selenium.chrome.ChromeOptions;importorg.openqa.selenium.remote.CapabilityType;importorg.openqa.selenium.chrome.ChromeDriver;publicclassOptionsTestextendsBaseTest{@TestpublicvoidsetPageLoadStrategyNormal(){ChromeOptionschromeOptions=getDefaultChromeOptions();chromeOptions.setPageLoadStrategy(PageLoadStrategy.NORMAL);WebDriverdriver=newChromeDriver(chromeOptions);try{// Navigate to Urldriver.get("https://selenium.dev");}finally{driver.quit();}}@TestpublicvoidsetPageLoadStrategyEager(){ChromeOptionschromeOptions=getDefaultChromeOptions();chromeOptions.setPageLoadStrategy(PageLoadStrategy.EAGER);WebDriverdriver=newChromeDriver(chromeOptions);try{// Navigate to Urldriver.get("https://selenium.dev");}finally{driver.quit();}}@TestpublicvoidsetPageLoadStrategyNone(){ChromeOptionschromeOptions=getDefaultChromeOptions();chromeOptions.setPageLoadStrategy(PageLoadStrategy.NONE);WebDriverdriver=newChromeDriver(chromeOptions);try{// Navigate to Urldriver.get("https://selenium.dev");}finally{driver.quit();}}@TestpublicvoidsetAcceptInsecureCerts(){ChromeOptionschromeOptions=getDefaultChromeOptions();chromeOptions.setAcceptInsecureCerts(true);WebDriverdriver=newChromeDriver(chromeOptions);try{// Navigate to Urldriver.get("https://selenium.dev");}finally{driver.quit();}}@TestpublicvoidgetBrowserName(){ChromeOptionschromeOptions=getDefaultChromeOptions();Stringname=chromeOptions.getBrowserName();Assertions.assertFalse(name.isEmpty(),"Browser name should not be empty");}@TestpublicvoidsetBrowserVersion(){ChromeOptionschromeOptions=getDefaultChromeOptions();Stringversion="latest";chromeOptions.setBrowserVersion(version);Assertions.assertEquals(version,chromeOptions.getBrowserVersion());}@TestpublicvoidsetPlatformName(){ChromeOptionschromeOptions=getDefaultChromeOptions();Stringplatform="OS X 10.6";chromeOptions.setPlatformName(platform);Assertions.assertEquals(platform,chromeOptions.getPlatformName().toString());}@TestpublicvoidsetScriptTimeout(){ChromeOptionschromeOptions=getDefaultChromeOptions();Durationduration=Duration.of(5,ChronoUnit.SECONDS);chromeOptions.setScriptTimeout(duration);WebDriverdriver=newChromeDriver(chromeOptions);try{Durationtimeout=driver.manage().timeouts().getScriptTimeout();Assertions.assertEquals(timeout,duration,"The script timeout should be set to 5 seconds.");}finally{driver.quit();}}@TestpublicvoidsetPageLoadTimeout(){ChromeOptionschromeOptions=getDefaultChromeOptions();Durationduration=Duration.of(5,ChronoUnit.SECONDS);chromeOptions.setPageLoadTimeout(duration);WebDriverdriver=newChromeDriver(chromeOptions);try{Durationtimeout=driver.manage().timeouts().getPageLoadTimeout();Assertions.assertEquals(timeout,duration,"The page load timeout should be set to 5 seconds.");}finally{driver.quit();}}@TestpublicvoidsetImplicitWaitTimeout(){ChromeOptionschromeOptions=getDefaultChromeOptions();Durationduration=Duration.of(5,ChronoUnit.SECONDS);chromeOptions.setImplicitWaitTimeout(duration);WebDriverdriver=newChromeDriver(chromeOptions);try{Durationtimeout=driver.manage().timeouts().getImplicitWaitTimeout();Assertions.assertEquals(timeout,duration,"The implicit wait timeout should be set to 5 seconds.");}finally{driver.quit();}}@TestpublicvoidsetUnhandledPromptBehaviour(){ChromeOptionschromeOptions=getDefaultChromeOptions();chromeOptions.setUnhandledPromptBehaviour(UnexpectedAlertBehaviour.DISMISS_AND_NOTIFY);//verify the capability object is not nullObjectcapabilityObject=chromeOptions.getCapability(CapabilityType.UNHANDLED_PROMPT_BEHAVIOUR);Assertions.assertNotNull(capabilityObject,"Capability UNHANDLED_PROMPT_BEHAVIOUR should not be null.");Assertions.assertEquals(capabilityObject.toString(),UnexpectedAlertBehaviour.DISMISS_AND_NOTIFY.toString());}@TestpublicvoidsetWindowRect(){ChromeOptionschromeOptions=getDefaultChromeOptions();chromeOptions.setCapability(CapabilityType.SET_WINDOW_RECT,true);//verify the capability object is not nullObjectcapabilityObject=chromeOptions.getCapability(CapabilityType.SET_WINDOW_RECT);Assertions.assertNotNull(capabilityObject,"Capability SET_WINDOW_RECT should not be null.");Booleancapability=(Boolean)capabilityObject;Assertions.assertTrue(capability,"The capability SET_WINDOW_RECT should be set to true.");}@TestpublicvoidsetStrictFileInteractability(){ChromeOptionschromeOptions=getDefaultChromeOptions();chromeOptions.setCapability(CapabilityType.STRICT_FILE_INTERACTABILITY,true);//verify the capability object is not nullObjectcapabilityObject=chromeOptions.getCapability(CapabilityType.STRICT_FILE_INTERACTABILITY);Assertions.assertNotNull(capabilityObject,"Capability STRICT_FILE_INTERACTABILITY should not be null.");Booleancapability=(Boolean)capabilityObject;Assertions.assertTrue(capability,"The capability STRICT_FILE_INTERACTABILITY should be set to true.");}}
fromseleniumimportwebdriverfromselenium.webdriver.common.proxyimportProxyfromselenium.webdriver.common.proxyimportProxyTypedeftest_page_load_strategy_normal():options=get_default_chrome_options()options.page_load_strategy='normal'driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_page_load_strategy_eager():options=get_default_chrome_options()options.page_load_strategy='eager'driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_page_load_strategy_none():options=get_default_chrome_options()options.page_load_strategy='none'driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_timeouts_script():options=get_default_chrome_options()options.timeouts={'script':5000}driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_timeouts_page_load():options=get_default_chrome_options()options.timeouts={'pageLoad':5000}driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_timeouts_implicit_wait():options=get_default_chrome_options()options.timeouts={'implicit':5000}driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_unhandled_prompt():options=get_default_chrome_options()options.unhandled_prompt_behavior='accept'driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_set_window_rect():options=webdriver.FirefoxOptions()options.set_window_rect=True# Full support in Firefoxdriver=webdriver.Firefox(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_strict_file_interactability():options=get_default_chrome_options()options.strict_file_interactability=Truedriver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_proxy():options=get_default_chrome_options()options.proxy=Proxy({'proxyType':ProxyType.MANUAL,'httpProxy':'http.proxy:1234'})driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_set_browser_name():options=get_default_chrome_options()assertoptions.capabilities['browserName']=='chrome'driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_set_browser_version():options=get_default_chrome_options()options.browser_version='stable'assertoptions.capabilities['browserVersion']=='stable'driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_platform_name():options=get_default_chrome_options()options.platform_name='any'driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_accept_insecure_certs():options=get_default_chrome_options()options.accept_insecure_certs=Truedriver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()defget_default_chrome_options():options=webdriver.ChromeOptions()options.add_argument("--no-sandbox")returnoptions
# frozen_string_literal: truerequire'spec_helper'RSpec.describe'Chrome'dodescribe'Driver Options'dolet(:chrome_location){driver_finder&&ENV.fetch('CHROME_BIN',nil)}let(:url){'https://www.selenium.dev/selenium/web/'}it'page load strategy normal'dooptions=Selenium::WebDriver::Options.chromeoptions.page_load_strategy=:normaldriver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'page load strategy eager'dooptions=Selenium::WebDriver::Options.chromeoptions.page_load_strategy=:eagerdriver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'page load strategy none'dooptions=Selenium::WebDriver::Options.chromeoptions.page_load_strategy=:nonedriver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets remote capabilities',skip:'this is example code that will not execute'dooptions=Selenium::WebDriver::Options.firefoxoptions.platform_name='Windows 10'options.browser_version='latest'cloud_options={}cloud_options[:build]=my_test_buildcloud_options[:name]=my_test_nameoptions.add_option('cloud:options',cloud_options)driver=Selenium::WebDriver.for:remote,capabilities:optionsdriver.get(url)driver.quitendit'accepts untrusted certificates'dooptions=Selenium::WebDriver::Options.chromeoptions.accept_insecure_certs=truedriver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets unhandled prompt behavior'dooptions=Selenium::WebDriver::Options.chromeoptions.unhandled_prompt_behavior=:acceptdriver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets window rect'dooptions=Selenium::WebDriver::Options.firefoxoptions.set_window_rect=truedriver=Selenium::WebDriver.for:firefox,options:optionsdriver.get(url)driver.quitendit'sets strict file interactability'dooptions=Selenium::WebDriver::Options.chromeoptions.strict_file_interactability=truedriver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets the proxy'dooptions=Selenium::WebDriver::Options.chromeoptions.proxy=Selenium::WebDriver::Proxy.new(http:'myproxy.com:8080')driver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets the implicit timeout'dooptions=Selenium::WebDriver::Options.chromeoptions.timeouts={implicit:1}driver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets the page load timeout'dooptions=Selenium::WebDriver::Options.chromeoptions.timeouts={page_load:400_000}driver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets the script timeout'dooptions=Selenium::WebDriver::Options.chromeoptions.timeouts={script:40_000}driver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets capabilities in the pre-selenium 4 way',skip:'this is example code that will not execute'docaps=Selenium::WebDriver::Remote::Capabilities.firefoxcaps[:platform]='Windows 10'caps[:version]='92'caps[:build]=my_test_buildcaps[:name]=my_test_namedriver=Selenium::WebDriver.for:remote,url:cloud_url,desired_capabilities:capsdriver.get(url)driver.quitendendend
constChrome=require('selenium-webdriver/chrome');const{Browser,Builder}=require("selenium-webdriver");constoptions=newChrome.Options()describe('Page loading strategies',function(){it('Navigate using eager page loading strategy',asyncfunction(){letdriver=newBuilder().forBrowser(Browser.CHROME).setChromeOptions(options.setPageLoadStrategy('eager')).build();awaitdriver.get('https://www.selenium.dev/selenium/web/blank.html');awaitdriver.quit();});it('Navigate using none page loading strategy',asyncfunction(){letdriver=newBuilder().forBrowser(Browser.CHROME).setChromeOptions(options.setPageLoadStrategy('none')).build();awaitdriver.get('https://www.selenium.dev/selenium/web/blank.html');awaitdriver.quit();});it('Navigate using normal page loading strategy',asyncfunction(){letdriver=newBuilder().forBrowser(Browser.CHROME).setChromeOptions(options.setPageLoadStrategy('normal')).build();awaitdriver.get('https://www.selenium.dev/selenium/web/blank.html');awaitdriver.quit();});it('Should be able to accept certs',asyncfunction(){letdriver=newBuilder().forBrowser(Browser.CHROME).setChromeOptions(options.setAcceptInsecureCerts(true)).build();awaitdriver.get('https://www.selenium.dev/selenium/web/blank.html');awaitdriver.quit();});});
packagedev.selenium.drivers;importdev.selenium.BaseTest;importjava.time.Duration;importjava.time.temporal.ChronoUnit;importorg.junit.jupiter.api.Test;importorg.junit.jupiter.api.Assertions;importorg.openqa.selenium.PageLoadStrategy;importorg.openqa.selenium.UnexpectedAlertBehaviour;importorg.openqa.selenium.WebDriver;importorg.openqa.selenium.chrome.ChromeOptions;importorg.openqa.selenium.remote.CapabilityType;importorg.openqa.selenium.chrome.ChromeDriver;publicclassOptionsTestextendsBaseTest{@TestpublicvoidsetPageLoadStrategyNormal(){ChromeOptionschromeOptions=getDefaultChromeOptions();chromeOptions.setPageLoadStrategy(PageLoadStrategy.NORMAL);WebDriverdriver=newChromeDriver(chromeOptions);try{// Navigate to Urldriver.get("https://selenium.dev");}finally{driver.quit();}}@TestpublicvoidsetPageLoadStrategyEager(){ChromeOptionschromeOptions=getDefaultChromeOptions();chromeOptions.setPageLoadStrategy(PageLoadStrategy.EAGER);WebDriverdriver=newChromeDriver(chromeOptions);try{// Navigate to Urldriver.get("https://selenium.dev");}finally{driver.quit();}}@TestpublicvoidsetPageLoadStrategyNone(){ChromeOptionschromeOptions=getDefaultChromeOptions();chromeOptions.setPageLoadStrategy(PageLoadStrategy.NONE);WebDriverdriver=newChromeDriver(chromeOptions);try{// Navigate to Urldriver.get("https://selenium.dev");}finally{driver.quit();}}@TestpublicvoidsetAcceptInsecureCerts(){ChromeOptionschromeOptions=getDefaultChromeOptions();chromeOptions.setAcceptInsecureCerts(true);WebDriverdriver=newChromeDriver(chromeOptions);try{// Navigate to Urldriver.get("https://selenium.dev");}finally{driver.quit();}}@TestpublicvoidgetBrowserName(){ChromeOptionschromeOptions=getDefaultChromeOptions();Stringname=chromeOptions.getBrowserName();Assertions.assertFalse(name.isEmpty(),"Browser name should not be empty");}@TestpublicvoidsetBrowserVersion(){ChromeOptionschromeOptions=getDefaultChromeOptions();Stringversion="latest";chromeOptions.setBrowserVersion(version);Assertions.assertEquals(version,chromeOptions.getBrowserVersion());}@TestpublicvoidsetPlatformName(){ChromeOptionschromeOptions=getDefaultChromeOptions();Stringplatform="OS X 10.6";chromeOptions.setPlatformName(platform);Assertions.assertEquals(platform,chromeOptions.getPlatformName().toString());}@TestpublicvoidsetScriptTimeout(){ChromeOptionschromeOptions=getDefaultChromeOptions();Durationduration=Duration.of(5,ChronoUnit.SECONDS);chromeOptions.setScriptTimeout(duration);WebDriverdriver=newChromeDriver(chromeOptions);try{Durationtimeout=driver.manage().timeouts().getScriptTimeout();Assertions.assertEquals(timeout,duration,"The script timeout should be set to 5 seconds.");}finally{driver.quit();}}@TestpublicvoidsetPageLoadTimeout(){ChromeOptionschromeOptions=getDefaultChromeOptions();Durationduration=Duration.of(5,ChronoUnit.SECONDS);chromeOptions.setPageLoadTimeout(duration);WebDriverdriver=newChromeDriver(chromeOptions);try{Durationtimeout=driver.manage().timeouts().getPageLoadTimeout();Assertions.assertEquals(timeout,duration,"The page load timeout should be set to 5 seconds.");}finally{driver.quit();}}@TestpublicvoidsetImplicitWaitTimeout(){ChromeOptionschromeOptions=getDefaultChromeOptions();Durationduration=Duration.of(5,ChronoUnit.SECONDS);chromeOptions.setImplicitWaitTimeout(duration);WebDriverdriver=newChromeDriver(chromeOptions);try{Durationtimeout=driver.manage().timeouts().getImplicitWaitTimeout();Assertions.assertEquals(timeout,duration,"The implicit wait timeout should be set to 5 seconds.");}finally{driver.quit();}}@TestpublicvoidsetUnhandledPromptBehaviour(){ChromeOptionschromeOptions=getDefaultChromeOptions();chromeOptions.setUnhandledPromptBehaviour(UnexpectedAlertBehaviour.DISMISS_AND_NOTIFY);//verify the capability object is not nullObjectcapabilityObject=chromeOptions.getCapability(CapabilityType.UNHANDLED_PROMPT_BEHAVIOUR);Assertions.assertNotNull(capabilityObject,"Capability UNHANDLED_PROMPT_BEHAVIOUR should not be null.");Assertions.assertEquals(capabilityObject.toString(),UnexpectedAlertBehaviour.DISMISS_AND_NOTIFY.toString());}@TestpublicvoidsetWindowRect(){ChromeOptionschromeOptions=getDefaultChromeOptions();chromeOptions.setCapability(CapabilityType.SET_WINDOW_RECT,true);//verify the capability object is not nullObjectcapabilityObject=chromeOptions.getCapability(CapabilityType.SET_WINDOW_RECT);Assertions.assertNotNull(capabilityObject,"Capability SET_WINDOW_RECT should not be null.");Booleancapability=(Boolean)capabilityObject;Assertions.assertTrue(capability,"The capability SET_WINDOW_RECT should be set to true.");}@TestpublicvoidsetStrictFileInteractability(){ChromeOptionschromeOptions=getDefaultChromeOptions();chromeOptions.setCapability(CapabilityType.STRICT_FILE_INTERACTABILITY,true);//verify the capability object is not nullObjectcapabilityObject=chromeOptions.getCapability(CapabilityType.STRICT_FILE_INTERACTABILITY);Assertions.assertNotNull(capabilityObject,"Capability STRICT_FILE_INTERACTABILITY should not be null.");Booleancapability=(Boolean)capabilityObject;Assertions.assertTrue(capability,"The capability STRICT_FILE_INTERACTABILITY should be set to true.");}}
fromseleniumimportwebdriverfromselenium.webdriver.common.proxyimportProxyfromselenium.webdriver.common.proxyimportProxyTypedeftest_page_load_strategy_normal():options=get_default_chrome_options()options.page_load_strategy='normal'driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_page_load_strategy_eager():options=get_default_chrome_options()options.page_load_strategy='eager'driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_page_load_strategy_none():options=get_default_chrome_options()options.page_load_strategy='none'driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_timeouts_script():options=get_default_chrome_options()options.timeouts={'script':5000}driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_timeouts_page_load():options=get_default_chrome_options()options.timeouts={'pageLoad':5000}driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_timeouts_implicit_wait():options=get_default_chrome_options()options.timeouts={'implicit':5000}driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_unhandled_prompt():options=get_default_chrome_options()options.unhandled_prompt_behavior='accept'driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_set_window_rect():options=webdriver.FirefoxOptions()options.set_window_rect=True# Full support in Firefoxdriver=webdriver.Firefox(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_strict_file_interactability():options=get_default_chrome_options()options.strict_file_interactability=Truedriver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_proxy():options=get_default_chrome_options()options.proxy=Proxy({'proxyType':ProxyType.MANUAL,'httpProxy':'http.proxy:1234'})driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_set_browser_name():options=get_default_chrome_options()assertoptions.capabilities['browserName']=='chrome'driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_set_browser_version():options=get_default_chrome_options()options.browser_version='stable'assertoptions.capabilities['browserVersion']=='stable'driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_platform_name():options=get_default_chrome_options()options.platform_name='any'driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_accept_insecure_certs():options=get_default_chrome_options()options.accept_insecure_certs=Truedriver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()defget_default_chrome_options():options=webdriver.ChromeOptions()options.add_argument("--no-sandbox")returnoptions
# frozen_string_literal: truerequire'spec_helper'RSpec.describe'Chrome'dodescribe'Driver Options'dolet(:chrome_location){driver_finder&&ENV.fetch('CHROME_BIN',nil)}let(:url){'https://www.selenium.dev/selenium/web/'}it'page load strategy normal'dooptions=Selenium::WebDriver::Options.chromeoptions.page_load_strategy=:normaldriver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'page load strategy eager'dooptions=Selenium::WebDriver::Options.chromeoptions.page_load_strategy=:eagerdriver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'page load strategy none'dooptions=Selenium::WebDriver::Options.chromeoptions.page_load_strategy=:nonedriver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets remote capabilities',skip:'this is example code that will not execute'dooptions=Selenium::WebDriver::Options.firefoxoptions.platform_name='Windows 10'options.browser_version='latest'cloud_options={}cloud_options[:build]=my_test_buildcloud_options[:name]=my_test_nameoptions.add_option('cloud:options',cloud_options)driver=Selenium::WebDriver.for:remote,capabilities:optionsdriver.get(url)driver.quitendit'accepts untrusted certificates'dooptions=Selenium::WebDriver::Options.chromeoptions.accept_insecure_certs=truedriver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets unhandled prompt behavior'dooptions=Selenium::WebDriver::Options.chromeoptions.unhandled_prompt_behavior=:acceptdriver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets window rect'dooptions=Selenium::WebDriver::Options.firefoxoptions.set_window_rect=truedriver=Selenium::WebDriver.for:firefox,options:optionsdriver.get(url)driver.quitendit'sets strict file interactability'dooptions=Selenium::WebDriver::Options.chromeoptions.strict_file_interactability=truedriver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets the proxy'dooptions=Selenium::WebDriver::Options.chromeoptions.proxy=Selenium::WebDriver::Proxy.new(http:'myproxy.com:8080')driver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets the implicit timeout'dooptions=Selenium::WebDriver::Options.chromeoptions.timeouts={implicit:1}driver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets the page load timeout'dooptions=Selenium::WebDriver::Options.chromeoptions.timeouts={page_load:400_000}driver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets the script timeout'dooptions=Selenium::WebDriver::Options.chromeoptions.timeouts={script:40_000}driver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets capabilities in the pre-selenium 4 way',skip:'this is example code that will not execute'docaps=Selenium::WebDriver::Remote::Capabilities.firefoxcaps[:platform]='Windows 10'caps[:version]='92'caps[:build]=my_test_buildcaps[:name]=my_test_namedriver=Selenium::WebDriver.for:remote,url:cloud_url,desired_capabilities:capsdriver.get(url)driver.quitendendend
constChrome=require('selenium-webdriver/chrome');const{Browser,Builder}=require("selenium-webdriver");constoptions=newChrome.Options()describe('Page loading strategies',function(){it('Navigate using eager page loading strategy',asyncfunction(){letdriver=newBuilder().forBrowser(Browser.CHROME).setChromeOptions(options.setPageLoadStrategy('eager')).build();awaitdriver.get('https://www.selenium.dev/selenium/web/blank.html');awaitdriver.quit();});it('Navigate using none page loading strategy',asyncfunction(){letdriver=newBuilder().forBrowser(Browser.CHROME).setChromeOptions(options.setPageLoadStrategy('none')).build();awaitdriver.get('https://www.selenium.dev/selenium/web/blank.html');awaitdriver.quit();});it('Navigate using normal page loading strategy',asyncfunction(){letdriver=newBuilder().forBrowser(Browser.CHROME).setChromeOptions(options.setPageLoadStrategy('normal')).build();awaitdriver.get('https://www.selenium.dev/selenium/web/blank.html');awaitdriver.quit();});it('Should be able to accept certs',asyncfunction(){letdriver=newBuilder().forBrowser(Browser.CHROME).setChromeOptions(options.setAcceptInsecureCerts(true)).build();awaitdriver.get('https://www.selenium.dev/selenium/web/blank.html');awaitdriver.quit();});});
This identifies the operating system at the remote-end,
fetching the platformName returns the OS name.
In cloud-based providers,
setting platformName sets the OS at the remote-end.
ChromeOptionschromeOptions=getDefaultChromeOptions();Stringplatform="OS X 10.6";chromeOptions.setPlatformName(platform);
Show full example
packagedev.selenium.drivers;importdev.selenium.BaseTest;importjava.time.Duration;importjava.time.temporal.ChronoUnit;importorg.junit.jupiter.api.Test;importorg.junit.jupiter.api.Assertions;importorg.openqa.selenium.PageLoadStrategy;importorg.openqa.selenium.UnexpectedAlertBehaviour;importorg.openqa.selenium.WebDriver;importorg.openqa.selenium.chrome.ChromeOptions;importorg.openqa.selenium.remote.CapabilityType;importorg.openqa.selenium.chrome.ChromeDriver;publicclassOptionsTestextendsBaseTest{@TestpublicvoidsetPageLoadStrategyNormal(){ChromeOptionschromeOptions=getDefaultChromeOptions();chromeOptions.setPageLoadStrategy(PageLoadStrategy.NORMAL);WebDriverdriver=newChromeDriver(chromeOptions);try{// Navigate to Urldriver.get("https://selenium.dev");}finally{driver.quit();}}@TestpublicvoidsetPageLoadStrategyEager(){ChromeOptionschromeOptions=getDefaultChromeOptions();chromeOptions.setPageLoadStrategy(PageLoadStrategy.EAGER);WebDriverdriver=newChromeDriver(chromeOptions);try{// Navigate to Urldriver.get("https://selenium.dev");}finally{driver.quit();}}@TestpublicvoidsetPageLoadStrategyNone(){ChromeOptionschromeOptions=getDefaultChromeOptions();chromeOptions.setPageLoadStrategy(PageLoadStrategy.NONE);WebDriverdriver=newChromeDriver(chromeOptions);try{// Navigate to Urldriver.get("https://selenium.dev");}finally{driver.quit();}}@TestpublicvoidsetAcceptInsecureCerts(){ChromeOptionschromeOptions=getDefaultChromeOptions();chromeOptions.setAcceptInsecureCerts(true);WebDriverdriver=newChromeDriver(chromeOptions);try{// Navigate to Urldriver.get("https://selenium.dev");}finally{driver.quit();}}@TestpublicvoidgetBrowserName(){ChromeOptionschromeOptions=getDefaultChromeOptions();Stringname=chromeOptions.getBrowserName();Assertions.assertFalse(name.isEmpty(),"Browser name should not be empty");}@TestpublicvoidsetBrowserVersion(){ChromeOptionschromeOptions=getDefaultChromeOptions();Stringversion="latest";chromeOptions.setBrowserVersion(version);Assertions.assertEquals(version,chromeOptions.getBrowserVersion());}@TestpublicvoidsetPlatformName(){ChromeOptionschromeOptions=getDefaultChromeOptions();Stringplatform="OS X 10.6";chromeOptions.setPlatformName(platform);Assertions.assertEquals(platform,chromeOptions.getPlatformName().toString());}@TestpublicvoidsetScriptTimeout(){ChromeOptionschromeOptions=getDefaultChromeOptions();Durationduration=Duration.of(5,ChronoUnit.SECONDS);chromeOptions.setScriptTimeout(duration);WebDriverdriver=newChromeDriver(chromeOptions);try{Durationtimeout=driver.manage().timeouts().getScriptTimeout();Assertions.assertEquals(timeout,duration,"The script timeout should be set to 5 seconds.");}finally{driver.quit();}}@TestpublicvoidsetPageLoadTimeout(){ChromeOptionschromeOptions=getDefaultChromeOptions();Durationduration=Duration.of(5,ChronoUnit.SECONDS);chromeOptions.setPageLoadTimeout(duration);WebDriverdriver=newChromeDriver(chromeOptions);try{Durationtimeout=driver.manage().timeouts().getPageLoadTimeout();Assertions.assertEquals(timeout,duration,"The page load timeout should be set to 5 seconds.");}finally{driver.quit();}}@TestpublicvoidsetImplicitWaitTimeout(){ChromeOptionschromeOptions=getDefaultChromeOptions();Durationduration=Duration.of(5,ChronoUnit.SECONDS);chromeOptions.setImplicitWaitTimeout(duration);WebDriverdriver=newChromeDriver(chromeOptions);try{Durationtimeout=driver.manage().timeouts().getImplicitWaitTimeout();Assertions.assertEquals(timeout,duration,"The implicit wait timeout should be set to 5 seconds.");}finally{driver.quit();}}@TestpublicvoidsetUnhandledPromptBehaviour(){ChromeOptionschromeOptions=getDefaultChromeOptions();chromeOptions.setUnhandledPromptBehaviour(UnexpectedAlertBehaviour.DISMISS_AND_NOTIFY);//verify the capability object is not nullObjectcapabilityObject=chromeOptions.getCapability(CapabilityType.UNHANDLED_PROMPT_BEHAVIOUR);Assertions.assertNotNull(capabilityObject,"Capability UNHANDLED_PROMPT_BEHAVIOUR should not be null.");Assertions.assertEquals(capabilityObject.toString(),UnexpectedAlertBehaviour.DISMISS_AND_NOTIFY.toString());}@TestpublicvoidsetWindowRect(){ChromeOptionschromeOptions=getDefaultChromeOptions();chromeOptions.setCapability(CapabilityType.SET_WINDOW_RECT,true);//verify the capability object is not nullObjectcapabilityObject=chromeOptions.getCapability(CapabilityType.SET_WINDOW_RECT);Assertions.assertNotNull(capabilityObject,"Capability SET_WINDOW_RECT should not be null.");Booleancapability=(Boolean)capabilityObject;Assertions.assertTrue(capability,"The capability SET_WINDOW_RECT should be set to true.");}@TestpublicvoidsetStrictFileInteractability(){ChromeOptionschromeOptions=getDefaultChromeOptions();chromeOptions.setCapability(CapabilityType.STRICT_FILE_INTERACTABILITY,true);//verify the capability object is not nullObjectcapabilityObject=chromeOptions.getCapability(CapabilityType.STRICT_FILE_INTERACTABILITY);Assertions.assertNotNull(capabilityObject,"Capability STRICT_FILE_INTERACTABILITY should not be null.");Booleancapability=(Boolean)capabilityObject;Assertions.assertTrue(capability,"The capability STRICT_FILE_INTERACTABILITY should be set to true.");}}
fromseleniumimportwebdriverfromselenium.webdriver.common.proxyimportProxyfromselenium.webdriver.common.proxyimportProxyTypedeftest_page_load_strategy_normal():options=get_default_chrome_options()options.page_load_strategy='normal'driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_page_load_strategy_eager():options=get_default_chrome_options()options.page_load_strategy='eager'driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_page_load_strategy_none():options=get_default_chrome_options()options.page_load_strategy='none'driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_timeouts_script():options=get_default_chrome_options()options.timeouts={'script':5000}driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_timeouts_page_load():options=get_default_chrome_options()options.timeouts={'pageLoad':5000}driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_timeouts_implicit_wait():options=get_default_chrome_options()options.timeouts={'implicit':5000}driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_unhandled_prompt():options=get_default_chrome_options()options.unhandled_prompt_behavior='accept'driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_set_window_rect():options=webdriver.FirefoxOptions()options.set_window_rect=True# Full support in Firefoxdriver=webdriver.Firefox(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_strict_file_interactability():options=get_default_chrome_options()options.strict_file_interactability=Truedriver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_proxy():options=get_default_chrome_options()options.proxy=Proxy({'proxyType':ProxyType.MANUAL,'httpProxy':'http.proxy:1234'})driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_set_browser_name():options=get_default_chrome_options()assertoptions.capabilities['browserName']=='chrome'driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_set_browser_version():options=get_default_chrome_options()options.browser_version='stable'assertoptions.capabilities['browserVersion']=='stable'driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_platform_name():options=get_default_chrome_options()options.platform_name='any'driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_accept_insecure_certs():options=get_default_chrome_options()options.accept_insecure_certs=Truedriver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()defget_default_chrome_options():options=webdriver.ChromeOptions()options.add_argument("--no-sandbox")returnoptions
# frozen_string_literal: truerequire'spec_helper'RSpec.describe'Chrome'dodescribe'Driver Options'dolet(:chrome_location){driver_finder&&ENV.fetch('CHROME_BIN',nil)}let(:url){'https://www.selenium.dev/selenium/web/'}it'page load strategy normal'dooptions=Selenium::WebDriver::Options.chromeoptions.page_load_strategy=:normaldriver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'page load strategy eager'dooptions=Selenium::WebDriver::Options.chromeoptions.page_load_strategy=:eagerdriver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'page load strategy none'dooptions=Selenium::WebDriver::Options.chromeoptions.page_load_strategy=:nonedriver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets remote capabilities',skip:'this is example code that will not execute'dooptions=Selenium::WebDriver::Options.firefoxoptions.platform_name='Windows 10'options.browser_version='latest'cloud_options={}cloud_options[:build]=my_test_buildcloud_options[:name]=my_test_nameoptions.add_option('cloud:options',cloud_options)driver=Selenium::WebDriver.for:remote,capabilities:optionsdriver.get(url)driver.quitendit'accepts untrusted certificates'dooptions=Selenium::WebDriver::Options.chromeoptions.accept_insecure_certs=truedriver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets unhandled prompt behavior'dooptions=Selenium::WebDriver::Options.chromeoptions.unhandled_prompt_behavior=:acceptdriver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets window rect'dooptions=Selenium::WebDriver::Options.firefoxoptions.set_window_rect=truedriver=Selenium::WebDriver.for:firefox,options:optionsdriver.get(url)driver.quitendit'sets strict file interactability'dooptions=Selenium::WebDriver::Options.chromeoptions.strict_file_interactability=truedriver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets the proxy'dooptions=Selenium::WebDriver::Options.chromeoptions.proxy=Selenium::WebDriver::Proxy.new(http:'myproxy.com:8080')driver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets the implicit timeout'dooptions=Selenium::WebDriver::Options.chromeoptions.timeouts={implicit:1}driver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets the page load timeout'dooptions=Selenium::WebDriver::Options.chromeoptions.timeouts={page_load:400_000}driver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets the script timeout'dooptions=Selenium::WebDriver::Options.chromeoptions.timeouts={script:40_000}driver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets capabilities in the pre-selenium 4 way',skip:'this is example code that will not execute'docaps=Selenium::WebDriver::Remote::Capabilities.firefoxcaps[:platform]='Windows 10'caps[:version]='92'caps[:build]=my_test_buildcaps[:name]=my_test_namedriver=Selenium::WebDriver.for:remote,url:cloud_url,desired_capabilities:capsdriver.get(url)driver.quitendendend
This capability checks whether an expired (or)
invalid TLS Certificate is used while navigating
during a session.
If the capability is set to false, an
insecure certificate error
will be returned as navigation encounters any domain
certificate problems. If set to true, invalid certificate will be
trusted by the browser.
All self-signed certificates will be trusted by this capability by default.
Once set, acceptInsecureCerts capability will have an
effect for the entire session.
packagedev.selenium.drivers;importdev.selenium.BaseTest;importjava.time.Duration;importjava.time.temporal.ChronoUnit;importorg.junit.jupiter.api.Test;importorg.junit.jupiter.api.Assertions;importorg.openqa.selenium.PageLoadStrategy;importorg.openqa.selenium.UnexpectedAlertBehaviour;importorg.openqa.selenium.WebDriver;importorg.openqa.selenium.chrome.ChromeOptions;importorg.openqa.selenium.remote.CapabilityType;importorg.openqa.selenium.chrome.ChromeDriver;publicclassOptionsTestextendsBaseTest{@TestpublicvoidsetPageLoadStrategyNormal(){ChromeOptionschromeOptions=getDefaultChromeOptions();chromeOptions.setPageLoadStrategy(PageLoadStrategy.NORMAL);WebDriverdriver=newChromeDriver(chromeOptions);try{// Navigate to Urldriver.get("https://selenium.dev");}finally{driver.quit();}}@TestpublicvoidsetPageLoadStrategyEager(){ChromeOptionschromeOptions=getDefaultChromeOptions();chromeOptions.setPageLoadStrategy(PageLoadStrategy.EAGER);WebDriverdriver=newChromeDriver(chromeOptions);try{// Navigate to Urldriver.get("https://selenium.dev");}finally{driver.quit();}}@TestpublicvoidsetPageLoadStrategyNone(){ChromeOptionschromeOptions=getDefaultChromeOptions();chromeOptions.setPageLoadStrategy(PageLoadStrategy.NONE);WebDriverdriver=newChromeDriver(chromeOptions);try{// Navigate to Urldriver.get("https://selenium.dev");}finally{driver.quit();}}@TestpublicvoidsetAcceptInsecureCerts(){ChromeOptionschromeOptions=getDefaultChromeOptions();chromeOptions.setAcceptInsecureCerts(true);WebDriverdriver=newChromeDriver(chromeOptions);try{// Navigate to Urldriver.get("https://selenium.dev");}finally{driver.quit();}}@TestpublicvoidgetBrowserName(){ChromeOptionschromeOptions=getDefaultChromeOptions();Stringname=chromeOptions.getBrowserName();Assertions.assertFalse(name.isEmpty(),"Browser name should not be empty");}@TestpublicvoidsetBrowserVersion(){ChromeOptionschromeOptions=getDefaultChromeOptions();Stringversion="latest";chromeOptions.setBrowserVersion(version);Assertions.assertEquals(version,chromeOptions.getBrowserVersion());}@TestpublicvoidsetPlatformName(){ChromeOptionschromeOptions=getDefaultChromeOptions();Stringplatform="OS X 10.6";chromeOptions.setPlatformName(platform);Assertions.assertEquals(platform,chromeOptions.getPlatformName().toString());}@TestpublicvoidsetScriptTimeout(){ChromeOptionschromeOptions=getDefaultChromeOptions();Durationduration=Duration.of(5,ChronoUnit.SECONDS);chromeOptions.setScriptTimeout(duration);WebDriverdriver=newChromeDriver(chromeOptions);try{Durationtimeout=driver.manage().timeouts().getScriptTimeout();Assertions.assertEquals(timeout,duration,"The script timeout should be set to 5 seconds.");}finally{driver.quit();}}@TestpublicvoidsetPageLoadTimeout(){ChromeOptionschromeOptions=getDefaultChromeOptions();Durationduration=Duration.of(5,ChronoUnit.SECONDS);chromeOptions.setPageLoadTimeout(duration);WebDriverdriver=newChromeDriver(chromeOptions);try{Durationtimeout=driver.manage().timeouts().getPageLoadTimeout();Assertions.assertEquals(timeout,duration,"The page load timeout should be set to 5 seconds.");}finally{driver.quit();}}@TestpublicvoidsetImplicitWaitTimeout(){ChromeOptionschromeOptions=getDefaultChromeOptions();Durationduration=Duration.of(5,ChronoUnit.SECONDS);chromeOptions.setImplicitWaitTimeout(duration);WebDriverdriver=newChromeDriver(chromeOptions);try{Durationtimeout=driver.manage().timeouts().getImplicitWaitTimeout();Assertions.assertEquals(timeout,duration,"The implicit wait timeout should be set to 5 seconds.");}finally{driver.quit();}}@TestpublicvoidsetUnhandledPromptBehaviour(){ChromeOptionschromeOptions=getDefaultChromeOptions();chromeOptions.setUnhandledPromptBehaviour(UnexpectedAlertBehaviour.DISMISS_AND_NOTIFY);//verify the capability object is not nullObjectcapabilityObject=chromeOptions.getCapability(CapabilityType.UNHANDLED_PROMPT_BEHAVIOUR);Assertions.assertNotNull(capabilityObject,"Capability UNHANDLED_PROMPT_BEHAVIOUR should not be null.");Assertions.assertEquals(capabilityObject.toString(),UnexpectedAlertBehaviour.DISMISS_AND_NOTIFY.toString());}@TestpublicvoidsetWindowRect(){ChromeOptionschromeOptions=getDefaultChromeOptions();chromeOptions.setCapability(CapabilityType.SET_WINDOW_RECT,true);//verify the capability object is not nullObjectcapabilityObject=chromeOptions.getCapability(CapabilityType.SET_WINDOW_RECT);Assertions.assertNotNull(capabilityObject,"Capability SET_WINDOW_RECT should not be null.");Booleancapability=(Boolean)capabilityObject;Assertions.assertTrue(capability,"The capability SET_WINDOW_RECT should be set to true.");}@TestpublicvoidsetStrictFileInteractability(){ChromeOptionschromeOptions=getDefaultChromeOptions();chromeOptions.setCapability(CapabilityType.STRICT_FILE_INTERACTABILITY,true);//verify the capability object is not nullObjectcapabilityObject=chromeOptions.getCapability(CapabilityType.STRICT_FILE_INTERACTABILITY);Assertions.assertNotNull(capabilityObject,"Capability STRICT_FILE_INTERACTABILITY should not be null.");Booleancapability=(Boolean)capabilityObject;Assertions.assertTrue(capability,"The capability STRICT_FILE_INTERACTABILITY should be set to true.");}}
fromseleniumimportwebdriverfromselenium.webdriver.common.proxyimportProxyfromselenium.webdriver.common.proxyimportProxyTypedeftest_page_load_strategy_normal():options=get_default_chrome_options()options.page_load_strategy='normal'driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_page_load_strategy_eager():options=get_default_chrome_options()options.page_load_strategy='eager'driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_page_load_strategy_none():options=get_default_chrome_options()options.page_load_strategy='none'driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_timeouts_script():options=get_default_chrome_options()options.timeouts={'script':5000}driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_timeouts_page_load():options=get_default_chrome_options()options.timeouts={'pageLoad':5000}driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_timeouts_implicit_wait():options=get_default_chrome_options()options.timeouts={'implicit':5000}driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_unhandled_prompt():options=get_default_chrome_options()options.unhandled_prompt_behavior='accept'driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_set_window_rect():options=webdriver.FirefoxOptions()options.set_window_rect=True# Full support in Firefoxdriver=webdriver.Firefox(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_strict_file_interactability():options=get_default_chrome_options()options.strict_file_interactability=Truedriver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_proxy():options=get_default_chrome_options()options.proxy=Proxy({'proxyType':ProxyType.MANUAL,'httpProxy':'http.proxy:1234'})driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_set_browser_name():options=get_default_chrome_options()assertoptions.capabilities['browserName']=='chrome'driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_set_browser_version():options=get_default_chrome_options()options.browser_version='stable'assertoptions.capabilities['browserVersion']=='stable'driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_platform_name():options=get_default_chrome_options()options.platform_name='any'driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_accept_insecure_certs():options=get_default_chrome_options()options.accept_insecure_certs=Truedriver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()defget_default_chrome_options():options=webdriver.ChromeOptions()options.add_argument("--no-sandbox")returnoptions
# frozen_string_literal: truerequire'spec_helper'RSpec.describe'Chrome'dodescribe'Driver Options'dolet(:chrome_location){driver_finder&&ENV.fetch('CHROME_BIN',nil)}let(:url){'https://www.selenium.dev/selenium/web/'}it'page load strategy normal'dooptions=Selenium::WebDriver::Options.chromeoptions.page_load_strategy=:normaldriver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'page load strategy eager'dooptions=Selenium::WebDriver::Options.chromeoptions.page_load_strategy=:eagerdriver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'page load strategy none'dooptions=Selenium::WebDriver::Options.chromeoptions.page_load_strategy=:nonedriver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets remote capabilities',skip:'this is example code that will not execute'dooptions=Selenium::WebDriver::Options.firefoxoptions.platform_name='Windows 10'options.browser_version='latest'cloud_options={}cloud_options[:build]=my_test_buildcloud_options[:name]=my_test_nameoptions.add_option('cloud:options',cloud_options)driver=Selenium::WebDriver.for:remote,capabilities:optionsdriver.get(url)driver.quitendit'accepts untrusted certificates'dooptions=Selenium::WebDriver::Options.chromeoptions.accept_insecure_certs=truedriver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets unhandled prompt behavior'dooptions=Selenium::WebDriver::Options.chromeoptions.unhandled_prompt_behavior=:acceptdriver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets window rect'dooptions=Selenium::WebDriver::Options.firefoxoptions.set_window_rect=truedriver=Selenium::WebDriver.for:firefox,options:optionsdriver.get(url)driver.quitendit'sets strict file interactability'dooptions=Selenium::WebDriver::Options.chromeoptions.strict_file_interactability=truedriver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets the proxy'dooptions=Selenium::WebDriver::Options.chromeoptions.proxy=Selenium::WebDriver::Proxy.new(http:'myproxy.com:8080')driver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets the implicit timeout'dooptions=Selenium::WebDriver::Options.chromeoptions.timeouts={implicit:1}driver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets the page load timeout'dooptions=Selenium::WebDriver::Options.chromeoptions.timeouts={page_load:400_000}driver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets the script timeout'dooptions=Selenium::WebDriver::Options.chromeoptions.timeouts={script:40_000}driver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets capabilities in the pre-selenium 4 way',skip:'this is example code that will not execute'docaps=Selenium::WebDriver::Remote::Capabilities.firefoxcaps[:platform]='Windows 10'caps[:version]='92'caps[:build]=my_test_buildcaps[:name]=my_test_namedriver=Selenium::WebDriver.for:remote,url:cloud_url,desired_capabilities:capsdriver.get(url)driver.quitendendend
constChrome=require('selenium-webdriver/chrome');const{Browser,Builder}=require("selenium-webdriver");constoptions=newChrome.Options()describe('Page loading strategies',function(){it('Navigate using eager page loading strategy',asyncfunction(){letdriver=newBuilder().forBrowser(Browser.CHROME).setChromeOptions(options.setPageLoadStrategy('eager')).build();awaitdriver.get('https://www.selenium.dev/selenium/web/blank.html');awaitdriver.quit();});it('Navigate using none page loading strategy',asyncfunction(){letdriver=newBuilder().forBrowser(Browser.CHROME).setChromeOptions(options.setPageLoadStrategy('none')).build();awaitdriver.get('https://www.selenium.dev/selenium/web/blank.html');awaitdriver.quit();});it('Navigate using normal page loading strategy',asyncfunction(){letdriver=newBuilder().forBrowser(Browser.CHROME).setChromeOptions(options.setPageLoadStrategy('normal')).build();awaitdriver.get('https://www.selenium.dev/selenium/web/blank.html');awaitdriver.quit();});it('Should be able to accept certs',asyncfunction(){letdriver=newBuilder().forBrowser(Browser.CHROME).setChromeOptions(options.setAcceptInsecureCerts(true)).build();awaitdriver.get('https://www.selenium.dev/selenium/web/blank.html');awaitdriver.quit();});});
A WebDriver session is imposed with a certain session timeout
interval, during which the user can control the behaviour
of executing scripts or retrieving information from the browser.
Each session timeout is configured with
combination of different timeouts as described below:
Script Timeout
Specifies when to interrupt an executing script in
a current browsing context. The default timeout 30,000
is imposed when a new session is created by WebDriver.
packagedev.selenium.drivers;importdev.selenium.BaseTest;importjava.time.Duration;importjava.time.temporal.ChronoUnit;importorg.junit.jupiter.api.Test;importorg.junit.jupiter.api.Assertions;importorg.openqa.selenium.PageLoadStrategy;importorg.openqa.selenium.UnexpectedAlertBehaviour;importorg.openqa.selenium.WebDriver;importorg.openqa.selenium.chrome.ChromeOptions;importorg.openqa.selenium.remote.CapabilityType;importorg.openqa.selenium.chrome.ChromeDriver;publicclassOptionsTestextendsBaseTest{@TestpublicvoidsetPageLoadStrategyNormal(){ChromeOptionschromeOptions=getDefaultChromeOptions();chromeOptions.setPageLoadStrategy(PageLoadStrategy.NORMAL);WebDriverdriver=newChromeDriver(chromeOptions);try{// Navigate to Urldriver.get("https://selenium.dev");}finally{driver.quit();}}@TestpublicvoidsetPageLoadStrategyEager(){ChromeOptionschromeOptions=getDefaultChromeOptions();chromeOptions.setPageLoadStrategy(PageLoadStrategy.EAGER);WebDriverdriver=newChromeDriver(chromeOptions);try{// Navigate to Urldriver.get("https://selenium.dev");}finally{driver.quit();}}@TestpublicvoidsetPageLoadStrategyNone(){ChromeOptionschromeOptions=getDefaultChromeOptions();chromeOptions.setPageLoadStrategy(PageLoadStrategy.NONE);WebDriverdriver=newChromeDriver(chromeOptions);try{// Navigate to Urldriver.get("https://selenium.dev");}finally{driver.quit();}}@TestpublicvoidsetAcceptInsecureCerts(){ChromeOptionschromeOptions=getDefaultChromeOptions();chromeOptions.setAcceptInsecureCerts(true);WebDriverdriver=newChromeDriver(chromeOptions);try{// Navigate to Urldriver.get("https://selenium.dev");}finally{driver.quit();}}@TestpublicvoidgetBrowserName(){ChromeOptionschromeOptions=getDefaultChromeOptions();Stringname=chromeOptions.getBrowserName();Assertions.assertFalse(name.isEmpty(),"Browser name should not be empty");}@TestpublicvoidsetBrowserVersion(){ChromeOptionschromeOptions=getDefaultChromeOptions();Stringversion="latest";chromeOptions.setBrowserVersion(version);Assertions.assertEquals(version,chromeOptions.getBrowserVersion());}@TestpublicvoidsetPlatformName(){ChromeOptionschromeOptions=getDefaultChromeOptions();Stringplatform="OS X 10.6";chromeOptions.setPlatformName(platform);Assertions.assertEquals(platform,chromeOptions.getPlatformName().toString());}@TestpublicvoidsetScriptTimeout(){ChromeOptionschromeOptions=getDefaultChromeOptions();Durationduration=Duration.of(5,ChronoUnit.SECONDS);chromeOptions.setScriptTimeout(duration);WebDriverdriver=newChromeDriver(chromeOptions);try{Durationtimeout=driver.manage().timeouts().getScriptTimeout();Assertions.assertEquals(timeout,duration,"The script timeout should be set to 5 seconds.");}finally{driver.quit();}}@TestpublicvoidsetPageLoadTimeout(){ChromeOptionschromeOptions=getDefaultChromeOptions();Durationduration=Duration.of(5,ChronoUnit.SECONDS);chromeOptions.setPageLoadTimeout(duration);WebDriverdriver=newChromeDriver(chromeOptions);try{Durationtimeout=driver.manage().timeouts().getPageLoadTimeout();Assertions.assertEquals(timeout,duration,"The page load timeout should be set to 5 seconds.");}finally{driver.quit();}}@TestpublicvoidsetImplicitWaitTimeout(){ChromeOptionschromeOptions=getDefaultChromeOptions();Durationduration=Duration.of(5,ChronoUnit.SECONDS);chromeOptions.setImplicitWaitTimeout(duration);WebDriverdriver=newChromeDriver(chromeOptions);try{Durationtimeout=driver.manage().timeouts().getImplicitWaitTimeout();Assertions.assertEquals(timeout,duration,"The implicit wait timeout should be set to 5 seconds.");}finally{driver.quit();}}@TestpublicvoidsetUnhandledPromptBehaviour(){ChromeOptionschromeOptions=getDefaultChromeOptions();chromeOptions.setUnhandledPromptBehaviour(UnexpectedAlertBehaviour.DISMISS_AND_NOTIFY);//verify the capability object is not nullObjectcapabilityObject=chromeOptions.getCapability(CapabilityType.UNHANDLED_PROMPT_BEHAVIOUR);Assertions.assertNotNull(capabilityObject,"Capability UNHANDLED_PROMPT_BEHAVIOUR should not be null.");Assertions.assertEquals(capabilityObject.toString(),UnexpectedAlertBehaviour.DISMISS_AND_NOTIFY.toString());}@TestpublicvoidsetWindowRect(){ChromeOptionschromeOptions=getDefaultChromeOptions();chromeOptions.setCapability(CapabilityType.SET_WINDOW_RECT,true);//verify the capability object is not nullObjectcapabilityObject=chromeOptions.getCapability(CapabilityType.SET_WINDOW_RECT);Assertions.assertNotNull(capabilityObject,"Capability SET_WINDOW_RECT should not be null.");Booleancapability=(Boolean)capabilityObject;Assertions.assertTrue(capability,"The capability SET_WINDOW_RECT should be set to true.");}@TestpublicvoidsetStrictFileInteractability(){ChromeOptionschromeOptions=getDefaultChromeOptions();chromeOptions.setCapability(CapabilityType.STRICT_FILE_INTERACTABILITY,true);//verify the capability object is not nullObjectcapabilityObject=chromeOptions.getCapability(CapabilityType.STRICT_FILE_INTERACTABILITY);Assertions.assertNotNull(capabilityObject,"Capability STRICT_FILE_INTERACTABILITY should not be null.");Booleancapability=(Boolean)capabilityObject;Assertions.assertTrue(capability,"The capability STRICT_FILE_INTERACTABILITY should be set to true.");}}
fromseleniumimportwebdriverfromselenium.webdriver.common.proxyimportProxyfromselenium.webdriver.common.proxyimportProxyTypedeftest_page_load_strategy_normal():options=get_default_chrome_options()options.page_load_strategy='normal'driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_page_load_strategy_eager():options=get_default_chrome_options()options.page_load_strategy='eager'driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_page_load_strategy_none():options=get_default_chrome_options()options.page_load_strategy='none'driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_timeouts_script():options=get_default_chrome_options()options.timeouts={'script':5000}driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_timeouts_page_load():options=get_default_chrome_options()options.timeouts={'pageLoad':5000}driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_timeouts_implicit_wait():options=get_default_chrome_options()options.timeouts={'implicit':5000}driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_unhandled_prompt():options=get_default_chrome_options()options.unhandled_prompt_behavior='accept'driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_set_window_rect():options=webdriver.FirefoxOptions()options.set_window_rect=True# Full support in Firefoxdriver=webdriver.Firefox(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_strict_file_interactability():options=get_default_chrome_options()options.strict_file_interactability=Truedriver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_proxy():options=get_default_chrome_options()options.proxy=Proxy({'proxyType':ProxyType.MANUAL,'httpProxy':'http.proxy:1234'})driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_set_browser_name():options=get_default_chrome_options()assertoptions.capabilities['browserName']=='chrome'driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_set_browser_version():options=get_default_chrome_options()options.browser_version='stable'assertoptions.capabilities['browserVersion']=='stable'driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_platform_name():options=get_default_chrome_options()options.platform_name='any'driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_accept_insecure_certs():options=get_default_chrome_options()options.accept_insecure_certs=Truedriver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()defget_default_chrome_options():options=webdriver.ChromeOptions()options.add_argument("--no-sandbox")returnoptions
# frozen_string_literal: truerequire'spec_helper'RSpec.describe'Chrome'dodescribe'Driver Options'dolet(:chrome_location){driver_finder&&ENV.fetch('CHROME_BIN',nil)}let(:url){'https://www.selenium.dev/selenium/web/'}it'page load strategy normal'dooptions=Selenium::WebDriver::Options.chromeoptions.page_load_strategy=:normaldriver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'page load strategy eager'dooptions=Selenium::WebDriver::Options.chromeoptions.page_load_strategy=:eagerdriver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'page load strategy none'dooptions=Selenium::WebDriver::Options.chromeoptions.page_load_strategy=:nonedriver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets remote capabilities',skip:'this is example code that will not execute'dooptions=Selenium::WebDriver::Options.firefoxoptions.platform_name='Windows 10'options.browser_version='latest'cloud_options={}cloud_options[:build]=my_test_buildcloud_options[:name]=my_test_nameoptions.add_option('cloud:options',cloud_options)driver=Selenium::WebDriver.for:remote,capabilities:optionsdriver.get(url)driver.quitendit'accepts untrusted certificates'dooptions=Selenium::WebDriver::Options.chromeoptions.accept_insecure_certs=truedriver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets unhandled prompt behavior'dooptions=Selenium::WebDriver::Options.chromeoptions.unhandled_prompt_behavior=:acceptdriver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets window rect'dooptions=Selenium::WebDriver::Options.firefoxoptions.set_window_rect=truedriver=Selenium::WebDriver.for:firefox,options:optionsdriver.get(url)driver.quitendit'sets strict file interactability'dooptions=Selenium::WebDriver::Options.chromeoptions.strict_file_interactability=truedriver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets the proxy'dooptions=Selenium::WebDriver::Options.chromeoptions.proxy=Selenium::WebDriver::Proxy.new(http:'myproxy.com:8080')driver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets the implicit timeout'dooptions=Selenium::WebDriver::Options.chromeoptions.timeouts={implicit:1}driver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets the page load timeout'dooptions=Selenium::WebDriver::Options.chromeoptions.timeouts={page_load:400_000}driver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets the script timeout'dooptions=Selenium::WebDriver::Options.chromeoptions.timeouts={script:40_000}driver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets capabilities in the pre-selenium 4 way',skip:'this is example code that will not execute'docaps=Selenium::WebDriver::Remote::Capabilities.firefoxcaps[:platform]='Windows 10'caps[:version]='92'caps[:build]=my_test_buildcaps[:name]=my_test_namedriver=Selenium::WebDriver.for:remote,url:cloud_url,desired_capabilities:capsdriver.get(url)driver.quitendendend
Specifies the time interval in which web page
needs to be loaded in a current browsing context.
The default timeout 300,000 is imposed when a
new session is created by WebDriver. If page load limits
a given/default time frame, the script will be stopped by
TimeoutException.
packagedev.selenium.drivers;importdev.selenium.BaseTest;importjava.time.Duration;importjava.time.temporal.ChronoUnit;importorg.junit.jupiter.api.Test;importorg.junit.jupiter.api.Assertions;importorg.openqa.selenium.PageLoadStrategy;importorg.openqa.selenium.UnexpectedAlertBehaviour;importorg.openqa.selenium.WebDriver;importorg.openqa.selenium.chrome.ChromeOptions;importorg.openqa.selenium.remote.CapabilityType;importorg.openqa.selenium.chrome.ChromeDriver;publicclassOptionsTestextendsBaseTest{@TestpublicvoidsetPageLoadStrategyNormal(){ChromeOptionschromeOptions=getDefaultChromeOptions();chromeOptions.setPageLoadStrategy(PageLoadStrategy.NORMAL);WebDriverdriver=newChromeDriver(chromeOptions);try{// Navigate to Urldriver.get("https://selenium.dev");}finally{driver.quit();}}@TestpublicvoidsetPageLoadStrategyEager(){ChromeOptionschromeOptions=getDefaultChromeOptions();chromeOptions.setPageLoadStrategy(PageLoadStrategy.EAGER);WebDriverdriver=newChromeDriver(chromeOptions);try{// Navigate to Urldriver.get("https://selenium.dev");}finally{driver.quit();}}@TestpublicvoidsetPageLoadStrategyNone(){ChromeOptionschromeOptions=getDefaultChromeOptions();chromeOptions.setPageLoadStrategy(PageLoadStrategy.NONE);WebDriverdriver=newChromeDriver(chromeOptions);try{// Navigate to Urldriver.get("https://selenium.dev");}finally{driver.quit();}}@TestpublicvoidsetAcceptInsecureCerts(){ChromeOptionschromeOptions=getDefaultChromeOptions();chromeOptions.setAcceptInsecureCerts(true);WebDriverdriver=newChromeDriver(chromeOptions);try{// Navigate to Urldriver.get("https://selenium.dev");}finally{driver.quit();}}@TestpublicvoidgetBrowserName(){ChromeOptionschromeOptions=getDefaultChromeOptions();Stringname=chromeOptions.getBrowserName();Assertions.assertFalse(name.isEmpty(),"Browser name should not be empty");}@TestpublicvoidsetBrowserVersion(){ChromeOptionschromeOptions=getDefaultChromeOptions();Stringversion="latest";chromeOptions.setBrowserVersion(version);Assertions.assertEquals(version,chromeOptions.getBrowserVersion());}@TestpublicvoidsetPlatformName(){ChromeOptionschromeOptions=getDefaultChromeOptions();Stringplatform="OS X 10.6";chromeOptions.setPlatformName(platform);Assertions.assertEquals(platform,chromeOptions.getPlatformName().toString());}@TestpublicvoidsetScriptTimeout(){ChromeOptionschromeOptions=getDefaultChromeOptions();Durationduration=Duration.of(5,ChronoUnit.SECONDS);chromeOptions.setScriptTimeout(duration);WebDriverdriver=newChromeDriver(chromeOptions);try{Durationtimeout=driver.manage().timeouts().getScriptTimeout();Assertions.assertEquals(timeout,duration,"The script timeout should be set to 5 seconds.");}finally{driver.quit();}}@TestpublicvoidsetPageLoadTimeout(){ChromeOptionschromeOptions=getDefaultChromeOptions();Durationduration=Duration.of(5,ChronoUnit.SECONDS);chromeOptions.setPageLoadTimeout(duration);WebDriverdriver=newChromeDriver(chromeOptions);try{Durationtimeout=driver.manage().timeouts().getPageLoadTimeout();Assertions.assertEquals(timeout,duration,"The page load timeout should be set to 5 seconds.");}finally{driver.quit();}}@TestpublicvoidsetImplicitWaitTimeout(){ChromeOptionschromeOptions=getDefaultChromeOptions();Durationduration=Duration.of(5,ChronoUnit.SECONDS);chromeOptions.setImplicitWaitTimeout(duration);WebDriverdriver=newChromeDriver(chromeOptions);try{Durationtimeout=driver.manage().timeouts().getImplicitWaitTimeout();Assertions.assertEquals(timeout,duration,"The implicit wait timeout should be set to 5 seconds.");}finally{driver.quit();}}@TestpublicvoidsetUnhandledPromptBehaviour(){ChromeOptionschromeOptions=getDefaultChromeOptions();chromeOptions.setUnhandledPromptBehaviour(UnexpectedAlertBehaviour.DISMISS_AND_NOTIFY);//verify the capability object is not nullObjectcapabilityObject=chromeOptions.getCapability(CapabilityType.UNHANDLED_PROMPT_BEHAVIOUR);Assertions.assertNotNull(capabilityObject,"Capability UNHANDLED_PROMPT_BEHAVIOUR should not be null.");Assertions.assertEquals(capabilityObject.toString(),UnexpectedAlertBehaviour.DISMISS_AND_NOTIFY.toString());}@TestpublicvoidsetWindowRect(){ChromeOptionschromeOptions=getDefaultChromeOptions();chromeOptions.setCapability(CapabilityType.SET_WINDOW_RECT,true);//verify the capability object is not nullObjectcapabilityObject=chromeOptions.getCapability(CapabilityType.SET_WINDOW_RECT);Assertions.assertNotNull(capabilityObject,"Capability SET_WINDOW_RECT should not be null.");Booleancapability=(Boolean)capabilityObject;Assertions.assertTrue(capability,"The capability SET_WINDOW_RECT should be set to true.");}@TestpublicvoidsetStrictFileInteractability(){ChromeOptionschromeOptions=getDefaultChromeOptions();chromeOptions.setCapability(CapabilityType.STRICT_FILE_INTERACTABILITY,true);//verify the capability object is not nullObjectcapabilityObject=chromeOptions.getCapability(CapabilityType.STRICT_FILE_INTERACTABILITY);Assertions.assertNotNull(capabilityObject,"Capability STRICT_FILE_INTERACTABILITY should not be null.");Booleancapability=(Boolean)capabilityObject;Assertions.assertTrue(capability,"The capability STRICT_FILE_INTERACTABILITY should be set to true.");}}
fromseleniumimportwebdriverfromselenium.webdriver.common.proxyimportProxyfromselenium.webdriver.common.proxyimportProxyTypedeftest_page_load_strategy_normal():options=get_default_chrome_options()options.page_load_strategy='normal'driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_page_load_strategy_eager():options=get_default_chrome_options()options.page_load_strategy='eager'driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_page_load_strategy_none():options=get_default_chrome_options()options.page_load_strategy='none'driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_timeouts_script():options=get_default_chrome_options()options.timeouts={'script':5000}driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_timeouts_page_load():options=get_default_chrome_options()options.timeouts={'pageLoad':5000}driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_timeouts_implicit_wait():options=get_default_chrome_options()options.timeouts={'implicit':5000}driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_unhandled_prompt():options=get_default_chrome_options()options.unhandled_prompt_behavior='accept'driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_set_window_rect():options=webdriver.FirefoxOptions()options.set_window_rect=True# Full support in Firefoxdriver=webdriver.Firefox(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_strict_file_interactability():options=get_default_chrome_options()options.strict_file_interactability=Truedriver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_proxy():options=get_default_chrome_options()options.proxy=Proxy({'proxyType':ProxyType.MANUAL,'httpProxy':'http.proxy:1234'})driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_set_browser_name():options=get_default_chrome_options()assertoptions.capabilities['browserName']=='chrome'driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_set_browser_version():options=get_default_chrome_options()options.browser_version='stable'assertoptions.capabilities['browserVersion']=='stable'driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_platform_name():options=get_default_chrome_options()options.platform_name='any'driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_accept_insecure_certs():options=get_default_chrome_options()options.accept_insecure_certs=Truedriver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()defget_default_chrome_options():options=webdriver.ChromeOptions()options.add_argument("--no-sandbox")returnoptions
# frozen_string_literal: truerequire'spec_helper'RSpec.describe'Chrome'dodescribe'Driver Options'dolet(:chrome_location){driver_finder&&ENV.fetch('CHROME_BIN',nil)}let(:url){'https://www.selenium.dev/selenium/web/'}it'page load strategy normal'dooptions=Selenium::WebDriver::Options.chromeoptions.page_load_strategy=:normaldriver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'page load strategy eager'dooptions=Selenium::WebDriver::Options.chromeoptions.page_load_strategy=:eagerdriver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'page load strategy none'dooptions=Selenium::WebDriver::Options.chromeoptions.page_load_strategy=:nonedriver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets remote capabilities',skip:'this is example code that will not execute'dooptions=Selenium::WebDriver::Options.firefoxoptions.platform_name='Windows 10'options.browser_version='latest'cloud_options={}cloud_options[:build]=my_test_buildcloud_options[:name]=my_test_nameoptions.add_option('cloud:options',cloud_options)driver=Selenium::WebDriver.for:remote,capabilities:optionsdriver.get(url)driver.quitendit'accepts untrusted certificates'dooptions=Selenium::WebDriver::Options.chromeoptions.accept_insecure_certs=truedriver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets unhandled prompt behavior'dooptions=Selenium::WebDriver::Options.chromeoptions.unhandled_prompt_behavior=:acceptdriver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets window rect'dooptions=Selenium::WebDriver::Options.firefoxoptions.set_window_rect=truedriver=Selenium::WebDriver.for:firefox,options:optionsdriver.get(url)driver.quitendit'sets strict file interactability'dooptions=Selenium::WebDriver::Options.chromeoptions.strict_file_interactability=truedriver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets the proxy'dooptions=Selenium::WebDriver::Options.chromeoptions.proxy=Selenium::WebDriver::Proxy.new(http:'myproxy.com:8080')driver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets the implicit timeout'dooptions=Selenium::WebDriver::Options.chromeoptions.timeouts={implicit:1}driver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets the page load timeout'dooptions=Selenium::WebDriver::Options.chromeoptions.timeouts={page_load:400_000}driver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets the script timeout'dooptions=Selenium::WebDriver::Options.chromeoptions.timeouts={script:40_000}driver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets capabilities in the pre-selenium 4 way',skip:'this is example code that will not execute'docaps=Selenium::WebDriver::Remote::Capabilities.firefoxcaps[:platform]='Windows 10'caps[:version]='92'caps[:build]=my_test_buildcaps[:name]=my_test_namedriver=Selenium::WebDriver.for:remote,url:cloud_url,desired_capabilities:capsdriver.get(url)driver.quitendendend
This specifies the time to wait for the
implicit element location strategy when
locating elements. The default timeout 0
is imposed when a new session is created by WebDriver.
packagedev.selenium.drivers;importdev.selenium.BaseTest;importjava.time.Duration;importjava.time.temporal.ChronoUnit;importorg.junit.jupiter.api.Test;importorg.junit.jupiter.api.Assertions;importorg.openqa.selenium.PageLoadStrategy;importorg.openqa.selenium.UnexpectedAlertBehaviour;importorg.openqa.selenium.WebDriver;importorg.openqa.selenium.chrome.ChromeOptions;importorg.openqa.selenium.remote.CapabilityType;importorg.openqa.selenium.chrome.ChromeDriver;publicclassOptionsTestextendsBaseTest{@TestpublicvoidsetPageLoadStrategyNormal(){ChromeOptionschromeOptions=getDefaultChromeOptions();chromeOptions.setPageLoadStrategy(PageLoadStrategy.NORMAL);WebDriverdriver=newChromeDriver(chromeOptions);try{// Navigate to Urldriver.get("https://selenium.dev");}finally{driver.quit();}}@TestpublicvoidsetPageLoadStrategyEager(){ChromeOptionschromeOptions=getDefaultChromeOptions();chromeOptions.setPageLoadStrategy(PageLoadStrategy.EAGER);WebDriverdriver=newChromeDriver(chromeOptions);try{// Navigate to Urldriver.get("https://selenium.dev");}finally{driver.quit();}}@TestpublicvoidsetPageLoadStrategyNone(){ChromeOptionschromeOptions=getDefaultChromeOptions();chromeOptions.setPageLoadStrategy(PageLoadStrategy.NONE);WebDriverdriver=newChromeDriver(chromeOptions);try{// Navigate to Urldriver.get("https://selenium.dev");}finally{driver.quit();}}@TestpublicvoidsetAcceptInsecureCerts(){ChromeOptionschromeOptions=getDefaultChromeOptions();chromeOptions.setAcceptInsecureCerts(true);WebDriverdriver=newChromeDriver(chromeOptions);try{// Navigate to Urldriver.get("https://selenium.dev");}finally{driver.quit();}}@TestpublicvoidgetBrowserName(){ChromeOptionschromeOptions=getDefaultChromeOptions();Stringname=chromeOptions.getBrowserName();Assertions.assertFalse(name.isEmpty(),"Browser name should not be empty");}@TestpublicvoidsetBrowserVersion(){ChromeOptionschromeOptions=getDefaultChromeOptions();Stringversion="latest";chromeOptions.setBrowserVersion(version);Assertions.assertEquals(version,chromeOptions.getBrowserVersion());}@TestpublicvoidsetPlatformName(){ChromeOptionschromeOptions=getDefaultChromeOptions();Stringplatform="OS X 10.6";chromeOptions.setPlatformName(platform);Assertions.assertEquals(platform,chromeOptions.getPlatformName().toString());}@TestpublicvoidsetScriptTimeout(){ChromeOptionschromeOptions=getDefaultChromeOptions();Durationduration=Duration.of(5,ChronoUnit.SECONDS);chromeOptions.setScriptTimeout(duration);WebDriverdriver=newChromeDriver(chromeOptions);try{Durationtimeout=driver.manage().timeouts().getScriptTimeout();Assertions.assertEquals(timeout,duration,"The script timeout should be set to 5 seconds.");}finally{driver.quit();}}@TestpublicvoidsetPageLoadTimeout(){ChromeOptionschromeOptions=getDefaultChromeOptions();Durationduration=Duration.of(5,ChronoUnit.SECONDS);chromeOptions.setPageLoadTimeout(duration);WebDriverdriver=newChromeDriver(chromeOptions);try{Durationtimeout=driver.manage().timeouts().getPageLoadTimeout();Assertions.assertEquals(timeout,duration,"The page load timeout should be set to 5 seconds.");}finally{driver.quit();}}@TestpublicvoidsetImplicitWaitTimeout(){ChromeOptionschromeOptions=getDefaultChromeOptions();Durationduration=Duration.of(5,ChronoUnit.SECONDS);chromeOptions.setImplicitWaitTimeout(duration);WebDriverdriver=newChromeDriver(chromeOptions);try{Durationtimeout=driver.manage().timeouts().getImplicitWaitTimeout();Assertions.assertEquals(timeout,duration,"The implicit wait timeout should be set to 5 seconds.");}finally{driver.quit();}}@TestpublicvoidsetUnhandledPromptBehaviour(){ChromeOptionschromeOptions=getDefaultChromeOptions();chromeOptions.setUnhandledPromptBehaviour(UnexpectedAlertBehaviour.DISMISS_AND_NOTIFY);//verify the capability object is not nullObjectcapabilityObject=chromeOptions.getCapability(CapabilityType.UNHANDLED_PROMPT_BEHAVIOUR);Assertions.assertNotNull(capabilityObject,"Capability UNHANDLED_PROMPT_BEHAVIOUR should not be null.");Assertions.assertEquals(capabilityObject.toString(),UnexpectedAlertBehaviour.DISMISS_AND_NOTIFY.toString());}@TestpublicvoidsetWindowRect(){ChromeOptionschromeOptions=getDefaultChromeOptions();chromeOptions.setCapability(CapabilityType.SET_WINDOW_RECT,true);//verify the capability object is not nullObjectcapabilityObject=chromeOptions.getCapability(CapabilityType.SET_WINDOW_RECT);Assertions.assertNotNull(capabilityObject,"Capability SET_WINDOW_RECT should not be null.");Booleancapability=(Boolean)capabilityObject;Assertions.assertTrue(capability,"The capability SET_WINDOW_RECT should be set to true.");}@TestpublicvoidsetStrictFileInteractability(){ChromeOptionschromeOptions=getDefaultChromeOptions();chromeOptions.setCapability(CapabilityType.STRICT_FILE_INTERACTABILITY,true);//verify the capability object is not nullObjectcapabilityObject=chromeOptions.getCapability(CapabilityType.STRICT_FILE_INTERACTABILITY);Assertions.assertNotNull(capabilityObject,"Capability STRICT_FILE_INTERACTABILITY should not be null.");Booleancapability=(Boolean)capabilityObject;Assertions.assertTrue(capability,"The capability STRICT_FILE_INTERACTABILITY should be set to true.");}}
fromseleniumimportwebdriverfromselenium.webdriver.common.proxyimportProxyfromselenium.webdriver.common.proxyimportProxyTypedeftest_page_load_strategy_normal():options=get_default_chrome_options()options.page_load_strategy='normal'driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_page_load_strategy_eager():options=get_default_chrome_options()options.page_load_strategy='eager'driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_page_load_strategy_none():options=get_default_chrome_options()options.page_load_strategy='none'driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_timeouts_script():options=get_default_chrome_options()options.timeouts={'script':5000}driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_timeouts_page_load():options=get_default_chrome_options()options.timeouts={'pageLoad':5000}driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_timeouts_implicit_wait():options=get_default_chrome_options()options.timeouts={'implicit':5000}driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_unhandled_prompt():options=get_default_chrome_options()options.unhandled_prompt_behavior='accept'driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_set_window_rect():options=webdriver.FirefoxOptions()options.set_window_rect=True# Full support in Firefoxdriver=webdriver.Firefox(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_strict_file_interactability():options=get_default_chrome_options()options.strict_file_interactability=Truedriver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_proxy():options=get_default_chrome_options()options.proxy=Proxy({'proxyType':ProxyType.MANUAL,'httpProxy':'http.proxy:1234'})driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_set_browser_name():options=get_default_chrome_options()assertoptions.capabilities['browserName']=='chrome'driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_set_browser_version():options=get_default_chrome_options()options.browser_version='stable'assertoptions.capabilities['browserVersion']=='stable'driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_platform_name():options=get_default_chrome_options()options.platform_name='any'driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_accept_insecure_certs():options=get_default_chrome_options()options.accept_insecure_certs=Truedriver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()defget_default_chrome_options():options=webdriver.ChromeOptions()options.add_argument("--no-sandbox")returnoptions
# frozen_string_literal: truerequire'spec_helper'RSpec.describe'Chrome'dodescribe'Driver Options'dolet(:chrome_location){driver_finder&&ENV.fetch('CHROME_BIN',nil)}let(:url){'https://www.selenium.dev/selenium/web/'}it'page load strategy normal'dooptions=Selenium::WebDriver::Options.chromeoptions.page_load_strategy=:normaldriver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'page load strategy eager'dooptions=Selenium::WebDriver::Options.chromeoptions.page_load_strategy=:eagerdriver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'page load strategy none'dooptions=Selenium::WebDriver::Options.chromeoptions.page_load_strategy=:nonedriver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets remote capabilities',skip:'this is example code that will not execute'dooptions=Selenium::WebDriver::Options.firefoxoptions.platform_name='Windows 10'options.browser_version='latest'cloud_options={}cloud_options[:build]=my_test_buildcloud_options[:name]=my_test_nameoptions.add_option('cloud:options',cloud_options)driver=Selenium::WebDriver.for:remote,capabilities:optionsdriver.get(url)driver.quitendit'accepts untrusted certificates'dooptions=Selenium::WebDriver::Options.chromeoptions.accept_insecure_certs=truedriver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets unhandled prompt behavior'dooptions=Selenium::WebDriver::Options.chromeoptions.unhandled_prompt_behavior=:acceptdriver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets window rect'dooptions=Selenium::WebDriver::Options.firefoxoptions.set_window_rect=truedriver=Selenium::WebDriver.for:firefox,options:optionsdriver.get(url)driver.quitendit'sets strict file interactability'dooptions=Selenium::WebDriver::Options.chromeoptions.strict_file_interactability=truedriver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets the proxy'dooptions=Selenium::WebDriver::Options.chromeoptions.proxy=Selenium::WebDriver::Proxy.new(http:'myproxy.com:8080')driver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets the implicit timeout'dooptions=Selenium::WebDriver::Options.chromeoptions.timeouts={implicit:1}driver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets the page load timeout'dooptions=Selenium::WebDriver::Options.chromeoptions.timeouts={page_load:400_000}driver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets the script timeout'dooptions=Selenium::WebDriver::Options.chromeoptions.timeouts={script:40_000}driver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets capabilities in the pre-selenium 4 way',skip:'this is example code that will not execute'docaps=Selenium::WebDriver::Remote::Capabilities.firefoxcaps[:platform]='Windows 10'caps[:version]='92'caps[:build]=my_test_buildcaps[:name]=my_test_namedriver=Selenium::WebDriver.for:remote,url:cloud_url,desired_capabilities:capsdriver.get(url)driver.quitendendend
Specifies the state of current session’s user prompt handler.
Defaults to dismiss and notify state
User Prompt Handler
This defines what action must take when a
user prompt encounters at the remote-end. This is defined by
unhandledPromptBehavior capability and has the following states:
packagedev.selenium.drivers;importdev.selenium.BaseTest;importjava.time.Duration;importjava.time.temporal.ChronoUnit;importorg.junit.jupiter.api.Test;importorg.junit.jupiter.api.Assertions;importorg.openqa.selenium.PageLoadStrategy;importorg.openqa.selenium.UnexpectedAlertBehaviour;importorg.openqa.selenium.WebDriver;importorg.openqa.selenium.chrome.ChromeOptions;importorg.openqa.selenium.remote.CapabilityType;importorg.openqa.selenium.chrome.ChromeDriver;publicclassOptionsTestextendsBaseTest{@TestpublicvoidsetPageLoadStrategyNormal(){ChromeOptionschromeOptions=getDefaultChromeOptions();chromeOptions.setPageLoadStrategy(PageLoadStrategy.NORMAL);WebDriverdriver=newChromeDriver(chromeOptions);try{// Navigate to Urldriver.get("https://selenium.dev");}finally{driver.quit();}}@TestpublicvoidsetPageLoadStrategyEager(){ChromeOptionschromeOptions=getDefaultChromeOptions();chromeOptions.setPageLoadStrategy(PageLoadStrategy.EAGER);WebDriverdriver=newChromeDriver(chromeOptions);try{// Navigate to Urldriver.get("https://selenium.dev");}finally{driver.quit();}}@TestpublicvoidsetPageLoadStrategyNone(){ChromeOptionschromeOptions=getDefaultChromeOptions();chromeOptions.setPageLoadStrategy(PageLoadStrategy.NONE);WebDriverdriver=newChromeDriver(chromeOptions);try{// Navigate to Urldriver.get("https://selenium.dev");}finally{driver.quit();}}@TestpublicvoidsetAcceptInsecureCerts(){ChromeOptionschromeOptions=getDefaultChromeOptions();chromeOptions.setAcceptInsecureCerts(true);WebDriverdriver=newChromeDriver(chromeOptions);try{// Navigate to Urldriver.get("https://selenium.dev");}finally{driver.quit();}}@TestpublicvoidgetBrowserName(){ChromeOptionschromeOptions=getDefaultChromeOptions();Stringname=chromeOptions.getBrowserName();Assertions.assertFalse(name.isEmpty(),"Browser name should not be empty");}@TestpublicvoidsetBrowserVersion(){ChromeOptionschromeOptions=getDefaultChromeOptions();Stringversion="latest";chromeOptions.setBrowserVersion(version);Assertions.assertEquals(version,chromeOptions.getBrowserVersion());}@TestpublicvoidsetPlatformName(){ChromeOptionschromeOptions=getDefaultChromeOptions();Stringplatform="OS X 10.6";chromeOptions.setPlatformName(platform);Assertions.assertEquals(platform,chromeOptions.getPlatformName().toString());}@TestpublicvoidsetScriptTimeout(){ChromeOptionschromeOptions=getDefaultChromeOptions();Durationduration=Duration.of(5,ChronoUnit.SECONDS);chromeOptions.setScriptTimeout(duration);WebDriverdriver=newChromeDriver(chromeOptions);try{Durationtimeout=driver.manage().timeouts().getScriptTimeout();Assertions.assertEquals(timeout,duration,"The script timeout should be set to 5 seconds.");}finally{driver.quit();}}@TestpublicvoidsetPageLoadTimeout(){ChromeOptionschromeOptions=getDefaultChromeOptions();Durationduration=Duration.of(5,ChronoUnit.SECONDS);chromeOptions.setPageLoadTimeout(duration);WebDriverdriver=newChromeDriver(chromeOptions);try{Durationtimeout=driver.manage().timeouts().getPageLoadTimeout();Assertions.assertEquals(timeout,duration,"The page load timeout should be set to 5 seconds.");}finally{driver.quit();}}@TestpublicvoidsetImplicitWaitTimeout(){ChromeOptionschromeOptions=getDefaultChromeOptions();Durationduration=Duration.of(5,ChronoUnit.SECONDS);chromeOptions.setImplicitWaitTimeout(duration);WebDriverdriver=newChromeDriver(chromeOptions);try{Durationtimeout=driver.manage().timeouts().getImplicitWaitTimeout();Assertions.assertEquals(timeout,duration,"The implicit wait timeout should be set to 5 seconds.");}finally{driver.quit();}}@TestpublicvoidsetUnhandledPromptBehaviour(){ChromeOptionschromeOptions=getDefaultChromeOptions();chromeOptions.setUnhandledPromptBehaviour(UnexpectedAlertBehaviour.DISMISS_AND_NOTIFY);//verify the capability object is not nullObjectcapabilityObject=chromeOptions.getCapability(CapabilityType.UNHANDLED_PROMPT_BEHAVIOUR);Assertions.assertNotNull(capabilityObject,"Capability UNHANDLED_PROMPT_BEHAVIOUR should not be null.");Assertions.assertEquals(capabilityObject.toString(),UnexpectedAlertBehaviour.DISMISS_AND_NOTIFY.toString());}@TestpublicvoidsetWindowRect(){ChromeOptionschromeOptions=getDefaultChromeOptions();chromeOptions.setCapability(CapabilityType.SET_WINDOW_RECT,true);//verify the capability object is not nullObjectcapabilityObject=chromeOptions.getCapability(CapabilityType.SET_WINDOW_RECT);Assertions.assertNotNull(capabilityObject,"Capability SET_WINDOW_RECT should not be null.");Booleancapability=(Boolean)capabilityObject;Assertions.assertTrue(capability,"The capability SET_WINDOW_RECT should be set to true.");}@TestpublicvoidsetStrictFileInteractability(){ChromeOptionschromeOptions=getDefaultChromeOptions();chromeOptions.setCapability(CapabilityType.STRICT_FILE_INTERACTABILITY,true);//verify the capability object is not nullObjectcapabilityObject=chromeOptions.getCapability(CapabilityType.STRICT_FILE_INTERACTABILITY);Assertions.assertNotNull(capabilityObject,"Capability STRICT_FILE_INTERACTABILITY should not be null.");Booleancapability=(Boolean)capabilityObject;Assertions.assertTrue(capability,"The capability STRICT_FILE_INTERACTABILITY should be set to true.");}}
fromseleniumimportwebdriverfromselenium.webdriver.common.proxyimportProxyfromselenium.webdriver.common.proxyimportProxyTypedeftest_page_load_strategy_normal():options=get_default_chrome_options()options.page_load_strategy='normal'driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_page_load_strategy_eager():options=get_default_chrome_options()options.page_load_strategy='eager'driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_page_load_strategy_none():options=get_default_chrome_options()options.page_load_strategy='none'driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_timeouts_script():options=get_default_chrome_options()options.timeouts={'script':5000}driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_timeouts_page_load():options=get_default_chrome_options()options.timeouts={'pageLoad':5000}driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_timeouts_implicit_wait():options=get_default_chrome_options()options.timeouts={'implicit':5000}driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_unhandled_prompt():options=get_default_chrome_options()options.unhandled_prompt_behavior='accept'driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_set_window_rect():options=webdriver.FirefoxOptions()options.set_window_rect=True# Full support in Firefoxdriver=webdriver.Firefox(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_strict_file_interactability():options=get_default_chrome_options()options.strict_file_interactability=Truedriver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_proxy():options=get_default_chrome_options()options.proxy=Proxy({'proxyType':ProxyType.MANUAL,'httpProxy':'http.proxy:1234'})driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_set_browser_name():options=get_default_chrome_options()assertoptions.capabilities['browserName']=='chrome'driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_set_browser_version():options=get_default_chrome_options()options.browser_version='stable'assertoptions.capabilities['browserVersion']=='stable'driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_platform_name():options=get_default_chrome_options()options.platform_name='any'driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_accept_insecure_certs():options=get_default_chrome_options()options.accept_insecure_certs=Truedriver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()defget_default_chrome_options():options=webdriver.ChromeOptions()options.add_argument("--no-sandbox")returnoptions
# frozen_string_literal: truerequire'spec_helper'RSpec.describe'Chrome'dodescribe'Driver Options'dolet(:chrome_location){driver_finder&&ENV.fetch('CHROME_BIN',nil)}let(:url){'https://www.selenium.dev/selenium/web/'}it'page load strategy normal'dooptions=Selenium::WebDriver::Options.chromeoptions.page_load_strategy=:normaldriver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'page load strategy eager'dooptions=Selenium::WebDriver::Options.chromeoptions.page_load_strategy=:eagerdriver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'page load strategy none'dooptions=Selenium::WebDriver::Options.chromeoptions.page_load_strategy=:nonedriver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets remote capabilities',skip:'this is example code that will not execute'dooptions=Selenium::WebDriver::Options.firefoxoptions.platform_name='Windows 10'options.browser_version='latest'cloud_options={}cloud_options[:build]=my_test_buildcloud_options[:name]=my_test_nameoptions.add_option('cloud:options',cloud_options)driver=Selenium::WebDriver.for:remote,capabilities:optionsdriver.get(url)driver.quitendit'accepts untrusted certificates'dooptions=Selenium::WebDriver::Options.chromeoptions.accept_insecure_certs=truedriver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets unhandled prompt behavior'dooptions=Selenium::WebDriver::Options.chromeoptions.unhandled_prompt_behavior=:acceptdriver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets window rect'dooptions=Selenium::WebDriver::Options.firefoxoptions.set_window_rect=truedriver=Selenium::WebDriver.for:firefox,options:optionsdriver.get(url)driver.quitendit'sets strict file interactability'dooptions=Selenium::WebDriver::Options.chromeoptions.strict_file_interactability=truedriver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets the proxy'dooptions=Selenium::WebDriver::Options.chromeoptions.proxy=Selenium::WebDriver::Proxy.new(http:'myproxy.com:8080')driver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets the implicit timeout'dooptions=Selenium::WebDriver::Options.chromeoptions.timeouts={implicit:1}driver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets the page load timeout'dooptions=Selenium::WebDriver::Options.chromeoptions.timeouts={page_load:400_000}driver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets the script timeout'dooptions=Selenium::WebDriver::Options.chromeoptions.timeouts={script:40_000}driver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets capabilities in the pre-selenium 4 way',skip:'this is example code that will not execute'docaps=Selenium::WebDriver::Remote::Capabilities.firefoxcaps[:platform]='Windows 10'caps[:version]='92'caps[:build]=my_test_buildcaps[:name]=my_test_namedriver=Selenium::WebDriver.for:remote,url:cloud_url,desired_capabilities:capsdriver.get(url)driver.quitendendend
packagedev.selenium.drivers;importdev.selenium.BaseTest;importjava.time.Duration;importjava.time.temporal.ChronoUnit;importorg.junit.jupiter.api.Test;importorg.junit.jupiter.api.Assertions;importorg.openqa.selenium.PageLoadStrategy;importorg.openqa.selenium.UnexpectedAlertBehaviour;importorg.openqa.selenium.WebDriver;importorg.openqa.selenium.chrome.ChromeOptions;importorg.openqa.selenium.remote.CapabilityType;importorg.openqa.selenium.chrome.ChromeDriver;publicclassOptionsTestextendsBaseTest{@TestpublicvoidsetPageLoadStrategyNormal(){ChromeOptionschromeOptions=getDefaultChromeOptions();chromeOptions.setPageLoadStrategy(PageLoadStrategy.NORMAL);WebDriverdriver=newChromeDriver(chromeOptions);try{// Navigate to Urldriver.get("https://selenium.dev");}finally{driver.quit();}}@TestpublicvoidsetPageLoadStrategyEager(){ChromeOptionschromeOptions=getDefaultChromeOptions();chromeOptions.setPageLoadStrategy(PageLoadStrategy.EAGER);WebDriverdriver=newChromeDriver(chromeOptions);try{// Navigate to Urldriver.get("https://selenium.dev");}finally{driver.quit();}}@TestpublicvoidsetPageLoadStrategyNone(){ChromeOptionschromeOptions=getDefaultChromeOptions();chromeOptions.setPageLoadStrategy(PageLoadStrategy.NONE);WebDriverdriver=newChromeDriver(chromeOptions);try{// Navigate to Urldriver.get("https://selenium.dev");}finally{driver.quit();}}@TestpublicvoidsetAcceptInsecureCerts(){ChromeOptionschromeOptions=getDefaultChromeOptions();chromeOptions.setAcceptInsecureCerts(true);WebDriverdriver=newChromeDriver(chromeOptions);try{// Navigate to Urldriver.get("https://selenium.dev");}finally{driver.quit();}}@TestpublicvoidgetBrowserName(){ChromeOptionschromeOptions=getDefaultChromeOptions();Stringname=chromeOptions.getBrowserName();Assertions.assertFalse(name.isEmpty(),"Browser name should not be empty");}@TestpublicvoidsetBrowserVersion(){ChromeOptionschromeOptions=getDefaultChromeOptions();Stringversion="latest";chromeOptions.setBrowserVersion(version);Assertions.assertEquals(version,chromeOptions.getBrowserVersion());}@TestpublicvoidsetPlatformName(){ChromeOptionschromeOptions=getDefaultChromeOptions();Stringplatform="OS X 10.6";chromeOptions.setPlatformName(platform);Assertions.assertEquals(platform,chromeOptions.getPlatformName().toString());}@TestpublicvoidsetScriptTimeout(){ChromeOptionschromeOptions=getDefaultChromeOptions();Durationduration=Duration.of(5,ChronoUnit.SECONDS);chromeOptions.setScriptTimeout(duration);WebDriverdriver=newChromeDriver(chromeOptions);try{Durationtimeout=driver.manage().timeouts().getScriptTimeout();Assertions.assertEquals(timeout,duration,"The script timeout should be set to 5 seconds.");}finally{driver.quit();}}@TestpublicvoidsetPageLoadTimeout(){ChromeOptionschromeOptions=getDefaultChromeOptions();Durationduration=Duration.of(5,ChronoUnit.SECONDS);chromeOptions.setPageLoadTimeout(duration);WebDriverdriver=newChromeDriver(chromeOptions);try{Durationtimeout=driver.manage().timeouts().getPageLoadTimeout();Assertions.assertEquals(timeout,duration,"The page load timeout should be set to 5 seconds.");}finally{driver.quit();}}@TestpublicvoidsetImplicitWaitTimeout(){ChromeOptionschromeOptions=getDefaultChromeOptions();Durationduration=Duration.of(5,ChronoUnit.SECONDS);chromeOptions.setImplicitWaitTimeout(duration);WebDriverdriver=newChromeDriver(chromeOptions);try{Durationtimeout=driver.manage().timeouts().getImplicitWaitTimeout();Assertions.assertEquals(timeout,duration,"The implicit wait timeout should be set to 5 seconds.");}finally{driver.quit();}}@TestpublicvoidsetUnhandledPromptBehaviour(){ChromeOptionschromeOptions=getDefaultChromeOptions();chromeOptions.setUnhandledPromptBehaviour(UnexpectedAlertBehaviour.DISMISS_AND_NOTIFY);//verify the capability object is not nullObjectcapabilityObject=chromeOptions.getCapability(CapabilityType.UNHANDLED_PROMPT_BEHAVIOUR);Assertions.assertNotNull(capabilityObject,"Capability UNHANDLED_PROMPT_BEHAVIOUR should not be null.");Assertions.assertEquals(capabilityObject.toString(),UnexpectedAlertBehaviour.DISMISS_AND_NOTIFY.toString());}@TestpublicvoidsetWindowRect(){ChromeOptionschromeOptions=getDefaultChromeOptions();chromeOptions.setCapability(CapabilityType.SET_WINDOW_RECT,true);//verify the capability object is not nullObjectcapabilityObject=chromeOptions.getCapability(CapabilityType.SET_WINDOW_RECT);Assertions.assertNotNull(capabilityObject,"Capability SET_WINDOW_RECT should not be null.");Booleancapability=(Boolean)capabilityObject;Assertions.assertTrue(capability,"The capability SET_WINDOW_RECT should be set to true.");}@TestpublicvoidsetStrictFileInteractability(){ChromeOptionschromeOptions=getDefaultChromeOptions();chromeOptions.setCapability(CapabilityType.STRICT_FILE_INTERACTABILITY,true);//verify the capability object is not nullObjectcapabilityObject=chromeOptions.getCapability(CapabilityType.STRICT_FILE_INTERACTABILITY);Assertions.assertNotNull(capabilityObject,"Capability STRICT_FILE_INTERACTABILITY should not be null.");Booleancapability=(Boolean)capabilityObject;Assertions.assertTrue(capability,"The capability STRICT_FILE_INTERACTABILITY should be set to true.");}}
options=webdriver.FirefoxOptions()options.set_window_rect=True# Full support in Firefoxdriver=webdriver.Firefox(options=options)
Show full example
fromseleniumimportwebdriverfromselenium.webdriver.common.proxyimportProxyfromselenium.webdriver.common.proxyimportProxyTypedeftest_page_load_strategy_normal():options=get_default_chrome_options()options.page_load_strategy='normal'driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_page_load_strategy_eager():options=get_default_chrome_options()options.page_load_strategy='eager'driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_page_load_strategy_none():options=get_default_chrome_options()options.page_load_strategy='none'driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_timeouts_script():options=get_default_chrome_options()options.timeouts={'script':5000}driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_timeouts_page_load():options=get_default_chrome_options()options.timeouts={'pageLoad':5000}driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_timeouts_implicit_wait():options=get_default_chrome_options()options.timeouts={'implicit':5000}driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_unhandled_prompt():options=get_default_chrome_options()options.unhandled_prompt_behavior='accept'driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_set_window_rect():options=webdriver.FirefoxOptions()options.set_window_rect=True# Full support in Firefoxdriver=webdriver.Firefox(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_strict_file_interactability():options=get_default_chrome_options()options.strict_file_interactability=Truedriver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_proxy():options=get_default_chrome_options()options.proxy=Proxy({'proxyType':ProxyType.MANUAL,'httpProxy':'http.proxy:1234'})driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_set_browser_name():options=get_default_chrome_options()assertoptions.capabilities['browserName']=='chrome'driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_set_browser_version():options=get_default_chrome_options()options.browser_version='stable'assertoptions.capabilities['browserVersion']=='stable'driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_platform_name():options=get_default_chrome_options()options.platform_name='any'driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_accept_insecure_certs():options=get_default_chrome_options()options.accept_insecure_certs=Truedriver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()defget_default_chrome_options():options=webdriver.ChromeOptions()options.add_argument("--no-sandbox")returnoptions
# frozen_string_literal: truerequire'spec_helper'RSpec.describe'Chrome'dodescribe'Driver Options'dolet(:chrome_location){driver_finder&&ENV.fetch('CHROME_BIN',nil)}let(:url){'https://www.selenium.dev/selenium/web/'}it'page load strategy normal'dooptions=Selenium::WebDriver::Options.chromeoptions.page_load_strategy=:normaldriver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'page load strategy eager'dooptions=Selenium::WebDriver::Options.chromeoptions.page_load_strategy=:eagerdriver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'page load strategy none'dooptions=Selenium::WebDriver::Options.chromeoptions.page_load_strategy=:nonedriver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets remote capabilities',skip:'this is example code that will not execute'dooptions=Selenium::WebDriver::Options.firefoxoptions.platform_name='Windows 10'options.browser_version='latest'cloud_options={}cloud_options[:build]=my_test_buildcloud_options[:name]=my_test_nameoptions.add_option('cloud:options',cloud_options)driver=Selenium::WebDriver.for:remote,capabilities:optionsdriver.get(url)driver.quitendit'accepts untrusted certificates'dooptions=Selenium::WebDriver::Options.chromeoptions.accept_insecure_certs=truedriver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets unhandled prompt behavior'dooptions=Selenium::WebDriver::Options.chromeoptions.unhandled_prompt_behavior=:acceptdriver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets window rect'dooptions=Selenium::WebDriver::Options.firefoxoptions.set_window_rect=truedriver=Selenium::WebDriver.for:firefox,options:optionsdriver.get(url)driver.quitendit'sets strict file interactability'dooptions=Selenium::WebDriver::Options.chromeoptions.strict_file_interactability=truedriver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets the proxy'dooptions=Selenium::WebDriver::Options.chromeoptions.proxy=Selenium::WebDriver::Proxy.new(http:'myproxy.com:8080')driver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets the implicit timeout'dooptions=Selenium::WebDriver::Options.chromeoptions.timeouts={implicit:1}driver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets the page load timeout'dooptions=Selenium::WebDriver::Options.chromeoptions.timeouts={page_load:400_000}driver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets the script timeout'dooptions=Selenium::WebDriver::Options.chromeoptions.timeouts={script:40_000}driver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets capabilities in the pre-selenium 4 way',skip:'this is example code that will not execute'docaps=Selenium::WebDriver::Remote::Capabilities.firefoxcaps[:platform]='Windows 10'caps[:version]='92'caps[:build]=my_test_buildcaps[:name]=my_test_namedriver=Selenium::WebDriver.for:remote,url:cloud_url,desired_capabilities:capsdriver.get(url)driver.quitendendend
This new capability indicates if strict interactability checks
should be applied to input type=file elements. As strict interactability
checks are off by default, there is a change in behaviour
when using Element Send Keys with hidden file upload controls.
packagedev.selenium.drivers;importdev.selenium.BaseTest;importjava.time.Duration;importjava.time.temporal.ChronoUnit;importorg.junit.jupiter.api.Test;importorg.junit.jupiter.api.Assertions;importorg.openqa.selenium.PageLoadStrategy;importorg.openqa.selenium.UnexpectedAlertBehaviour;importorg.openqa.selenium.WebDriver;importorg.openqa.selenium.chrome.ChromeOptions;importorg.openqa.selenium.remote.CapabilityType;importorg.openqa.selenium.chrome.ChromeDriver;publicclassOptionsTestextendsBaseTest{@TestpublicvoidsetPageLoadStrategyNormal(){ChromeOptionschromeOptions=getDefaultChromeOptions();chromeOptions.setPageLoadStrategy(PageLoadStrategy.NORMAL);WebDriverdriver=newChromeDriver(chromeOptions);try{// Navigate to Urldriver.get("https://selenium.dev");}finally{driver.quit();}}@TestpublicvoidsetPageLoadStrategyEager(){ChromeOptionschromeOptions=getDefaultChromeOptions();chromeOptions.setPageLoadStrategy(PageLoadStrategy.EAGER);WebDriverdriver=newChromeDriver(chromeOptions);try{// Navigate to Urldriver.get("https://selenium.dev");}finally{driver.quit();}}@TestpublicvoidsetPageLoadStrategyNone(){ChromeOptionschromeOptions=getDefaultChromeOptions();chromeOptions.setPageLoadStrategy(PageLoadStrategy.NONE);WebDriverdriver=newChromeDriver(chromeOptions);try{// Navigate to Urldriver.get("https://selenium.dev");}finally{driver.quit();}}@TestpublicvoidsetAcceptInsecureCerts(){ChromeOptionschromeOptions=getDefaultChromeOptions();chromeOptions.setAcceptInsecureCerts(true);WebDriverdriver=newChromeDriver(chromeOptions);try{// Navigate to Urldriver.get("https://selenium.dev");}finally{driver.quit();}}@TestpublicvoidgetBrowserName(){ChromeOptionschromeOptions=getDefaultChromeOptions();Stringname=chromeOptions.getBrowserName();Assertions.assertFalse(name.isEmpty(),"Browser name should not be empty");}@TestpublicvoidsetBrowserVersion(){ChromeOptionschromeOptions=getDefaultChromeOptions();Stringversion="latest";chromeOptions.setBrowserVersion(version);Assertions.assertEquals(version,chromeOptions.getBrowserVersion());}@TestpublicvoidsetPlatformName(){ChromeOptionschromeOptions=getDefaultChromeOptions();Stringplatform="OS X 10.6";chromeOptions.setPlatformName(platform);Assertions.assertEquals(platform,chromeOptions.getPlatformName().toString());}@TestpublicvoidsetScriptTimeout(){ChromeOptionschromeOptions=getDefaultChromeOptions();Durationduration=Duration.of(5,ChronoUnit.SECONDS);chromeOptions.setScriptTimeout(duration);WebDriverdriver=newChromeDriver(chromeOptions);try{Durationtimeout=driver.manage().timeouts().getScriptTimeout();Assertions.assertEquals(timeout,duration,"The script timeout should be set to 5 seconds.");}finally{driver.quit();}}@TestpublicvoidsetPageLoadTimeout(){ChromeOptionschromeOptions=getDefaultChromeOptions();Durationduration=Duration.of(5,ChronoUnit.SECONDS);chromeOptions.setPageLoadTimeout(duration);WebDriverdriver=newChromeDriver(chromeOptions);try{Durationtimeout=driver.manage().timeouts().getPageLoadTimeout();Assertions.assertEquals(timeout,duration,"The page load timeout should be set to 5 seconds.");}finally{driver.quit();}}@TestpublicvoidsetImplicitWaitTimeout(){ChromeOptionschromeOptions=getDefaultChromeOptions();Durationduration=Duration.of(5,ChronoUnit.SECONDS);chromeOptions.setImplicitWaitTimeout(duration);WebDriverdriver=newChromeDriver(chromeOptions);try{Durationtimeout=driver.manage().timeouts().getImplicitWaitTimeout();Assertions.assertEquals(timeout,duration,"The implicit wait timeout should be set to 5 seconds.");}finally{driver.quit();}}@TestpublicvoidsetUnhandledPromptBehaviour(){ChromeOptionschromeOptions=getDefaultChromeOptions();chromeOptions.setUnhandledPromptBehaviour(UnexpectedAlertBehaviour.DISMISS_AND_NOTIFY);//verify the capability object is not nullObjectcapabilityObject=chromeOptions.getCapability(CapabilityType.UNHANDLED_PROMPT_BEHAVIOUR);Assertions.assertNotNull(capabilityObject,"Capability UNHANDLED_PROMPT_BEHAVIOUR should not be null.");Assertions.assertEquals(capabilityObject.toString(),UnexpectedAlertBehaviour.DISMISS_AND_NOTIFY.toString());}@TestpublicvoidsetWindowRect(){ChromeOptionschromeOptions=getDefaultChromeOptions();chromeOptions.setCapability(CapabilityType.SET_WINDOW_RECT,true);//verify the capability object is not nullObjectcapabilityObject=chromeOptions.getCapability(CapabilityType.SET_WINDOW_RECT);Assertions.assertNotNull(capabilityObject,"Capability SET_WINDOW_RECT should not be null.");Booleancapability=(Boolean)capabilityObject;Assertions.assertTrue(capability,"The capability SET_WINDOW_RECT should be set to true.");}@TestpublicvoidsetStrictFileInteractability(){ChromeOptionschromeOptions=getDefaultChromeOptions();chromeOptions.setCapability(CapabilityType.STRICT_FILE_INTERACTABILITY,true);//verify the capability object is not nullObjectcapabilityObject=chromeOptions.getCapability(CapabilityType.STRICT_FILE_INTERACTABILITY);Assertions.assertNotNull(capabilityObject,"Capability STRICT_FILE_INTERACTABILITY should not be null.");Booleancapability=(Boolean)capabilityObject;Assertions.assertTrue(capability,"The capability STRICT_FILE_INTERACTABILITY should be set to true.");}}
fromseleniumimportwebdriverfromselenium.webdriver.common.proxyimportProxyfromselenium.webdriver.common.proxyimportProxyTypedeftest_page_load_strategy_normal():options=get_default_chrome_options()options.page_load_strategy='normal'driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_page_load_strategy_eager():options=get_default_chrome_options()options.page_load_strategy='eager'driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_page_load_strategy_none():options=get_default_chrome_options()options.page_load_strategy='none'driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_timeouts_script():options=get_default_chrome_options()options.timeouts={'script':5000}driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_timeouts_page_load():options=get_default_chrome_options()options.timeouts={'pageLoad':5000}driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_timeouts_implicit_wait():options=get_default_chrome_options()options.timeouts={'implicit':5000}driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_unhandled_prompt():options=get_default_chrome_options()options.unhandled_prompt_behavior='accept'driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_set_window_rect():options=webdriver.FirefoxOptions()options.set_window_rect=True# Full support in Firefoxdriver=webdriver.Firefox(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_strict_file_interactability():options=get_default_chrome_options()options.strict_file_interactability=Truedriver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_proxy():options=get_default_chrome_options()options.proxy=Proxy({'proxyType':ProxyType.MANUAL,'httpProxy':'http.proxy:1234'})driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_set_browser_name():options=get_default_chrome_options()assertoptions.capabilities['browserName']=='chrome'driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_set_browser_version():options=get_default_chrome_options()options.browser_version='stable'assertoptions.capabilities['browserVersion']=='stable'driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_platform_name():options=get_default_chrome_options()options.platform_name='any'driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_accept_insecure_certs():options=get_default_chrome_options()options.accept_insecure_certs=Truedriver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()defget_default_chrome_options():options=webdriver.ChromeOptions()options.add_argument("--no-sandbox")returnoptions
# frozen_string_literal: truerequire'spec_helper'RSpec.describe'Chrome'dodescribe'Driver Options'dolet(:chrome_location){driver_finder&&ENV.fetch('CHROME_BIN',nil)}let(:url){'https://www.selenium.dev/selenium/web/'}it'page load strategy normal'dooptions=Selenium::WebDriver::Options.chromeoptions.page_load_strategy=:normaldriver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'page load strategy eager'dooptions=Selenium::WebDriver::Options.chromeoptions.page_load_strategy=:eagerdriver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'page load strategy none'dooptions=Selenium::WebDriver::Options.chromeoptions.page_load_strategy=:nonedriver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets remote capabilities',skip:'this is example code that will not execute'dooptions=Selenium::WebDriver::Options.firefoxoptions.platform_name='Windows 10'options.browser_version='latest'cloud_options={}cloud_options[:build]=my_test_buildcloud_options[:name]=my_test_nameoptions.add_option('cloud:options',cloud_options)driver=Selenium::WebDriver.for:remote,capabilities:optionsdriver.get(url)driver.quitendit'accepts untrusted certificates'dooptions=Selenium::WebDriver::Options.chromeoptions.accept_insecure_certs=truedriver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets unhandled prompt behavior'dooptions=Selenium::WebDriver::Options.chromeoptions.unhandled_prompt_behavior=:acceptdriver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets window rect'dooptions=Selenium::WebDriver::Options.firefoxoptions.set_window_rect=truedriver=Selenium::WebDriver.for:firefox,options:optionsdriver.get(url)driver.quitendit'sets strict file interactability'dooptions=Selenium::WebDriver::Options.chromeoptions.strict_file_interactability=truedriver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets the proxy'dooptions=Selenium::WebDriver::Options.chromeoptions.proxy=Selenium::WebDriver::Proxy.new(http:'myproxy.com:8080')driver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets the implicit timeout'dooptions=Selenium::WebDriver::Options.chromeoptions.timeouts={implicit:1}driver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets the page load timeout'dooptions=Selenium::WebDriver::Options.chromeoptions.timeouts={page_load:400_000}driver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets the script timeout'dooptions=Selenium::WebDriver::Options.chromeoptions.timeouts={script:40_000}driver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets capabilities in the pre-selenium 4 way',skip:'this is example code that will not execute'docaps=Selenium::WebDriver::Remote::Capabilities.firefoxcaps[:platform]='Windows 10'caps[:version]='92'caps[:build]=my_test_buildcaps[:name]=my_test_namedriver=Selenium::WebDriver.for:remote,url:cloud_url,desired_capabilities:capsdriver.get(url)driver.quitendendend
A proxy server acts as an intermediary for
requests between a client and a server. In simple terms,
the traffic flows through the proxy server
on its way to the address you requested and back.
A proxy server for automation scripts
with Selenium could be helpful for:
Capture network traffic
Mock backend calls made by the website
Access the required website under complex network
topologies or strict corporate restrictions/policies.
If you are in a corporate environment, and a
browser fails to connect to a URL, this is most
likely because the environment needs a proxy to be accessed.
Selenium WebDriver provides a way to proxy settings:
fromseleniumimportwebdriverfromselenium.webdriver.common.proxyimportProxyfromselenium.webdriver.common.proxyimportProxyTypedeftest_page_load_strategy_normal():options=get_default_chrome_options()options.page_load_strategy='normal'driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_page_load_strategy_eager():options=get_default_chrome_options()options.page_load_strategy='eager'driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_page_load_strategy_none():options=get_default_chrome_options()options.page_load_strategy='none'driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_timeouts_script():options=get_default_chrome_options()options.timeouts={'script':5000}driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_timeouts_page_load():options=get_default_chrome_options()options.timeouts={'pageLoad':5000}driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_timeouts_implicit_wait():options=get_default_chrome_options()options.timeouts={'implicit':5000}driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_unhandled_prompt():options=get_default_chrome_options()options.unhandled_prompt_behavior='accept'driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_set_window_rect():options=webdriver.FirefoxOptions()options.set_window_rect=True# Full support in Firefoxdriver=webdriver.Firefox(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_strict_file_interactability():options=get_default_chrome_options()options.strict_file_interactability=Truedriver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_proxy():options=get_default_chrome_options()options.proxy=Proxy({'proxyType':ProxyType.MANUAL,'httpProxy':'http.proxy:1234'})driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_set_browser_name():options=get_default_chrome_options()assertoptions.capabilities['browserName']=='chrome'driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_set_browser_version():options=get_default_chrome_options()options.browser_version='stable'assertoptions.capabilities['browserVersion']=='stable'driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_platform_name():options=get_default_chrome_options()options.platform_name='any'driver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()deftest_accept_insecure_certs():options=get_default_chrome_options()options.accept_insecure_certs=Truedriver=webdriver.Chrome(options=options)driver.get("https://www.selenium.dev/")driver.quit()defget_default_chrome_options():options=webdriver.ChromeOptions()options.add_argument("--no-sandbox")returnoptions
# frozen_string_literal: truerequire'spec_helper'RSpec.describe'Chrome'dodescribe'Driver Options'dolet(:chrome_location){driver_finder&&ENV.fetch('CHROME_BIN',nil)}let(:url){'https://www.selenium.dev/selenium/web/'}it'page load strategy normal'dooptions=Selenium::WebDriver::Options.chromeoptions.page_load_strategy=:normaldriver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'page load strategy eager'dooptions=Selenium::WebDriver::Options.chromeoptions.page_load_strategy=:eagerdriver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'page load strategy none'dooptions=Selenium::WebDriver::Options.chromeoptions.page_load_strategy=:nonedriver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets remote capabilities',skip:'this is example code that will not execute'dooptions=Selenium::WebDriver::Options.firefoxoptions.platform_name='Windows 10'options.browser_version='latest'cloud_options={}cloud_options[:build]=my_test_buildcloud_options[:name]=my_test_nameoptions.add_option('cloud:options',cloud_options)driver=Selenium::WebDriver.for:remote,capabilities:optionsdriver.get(url)driver.quitendit'accepts untrusted certificates'dooptions=Selenium::WebDriver::Options.chromeoptions.accept_insecure_certs=truedriver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets unhandled prompt behavior'dooptions=Selenium::WebDriver::Options.chromeoptions.unhandled_prompt_behavior=:acceptdriver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets window rect'dooptions=Selenium::WebDriver::Options.firefoxoptions.set_window_rect=truedriver=Selenium::WebDriver.for:firefox,options:optionsdriver.get(url)driver.quitendit'sets strict file interactability'dooptions=Selenium::WebDriver::Options.chromeoptions.strict_file_interactability=truedriver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets the proxy'dooptions=Selenium::WebDriver::Options.chromeoptions.proxy=Selenium::WebDriver::Proxy.new(http:'myproxy.com:8080')driver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets the implicit timeout'dooptions=Selenium::WebDriver::Options.chromeoptions.timeouts={implicit:1}driver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets the page load timeout'dooptions=Selenium::WebDriver::Options.chromeoptions.timeouts={page_load:400_000}driver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets the script timeout'dooptions=Selenium::WebDriver::Options.chromeoptions.timeouts={script:40_000}driver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets capabilities in the pre-selenium 4 way',skip:'this is example code that will not execute'docaps=Selenium::WebDriver::Remote::Capabilities.firefoxcaps[:platform]='Windows 10'caps[:version]='92'caps[:build]=my_test_buildcaps[:name]=my_test_namedriver=Selenium::WebDriver.for:remote,url:cloud_url,desired_capabilities:capsdriver.get(url)driver.quitendendend
The Service classes are for managing the starting and stopping of local drivers.
They cannot be used with a Remote WebDriver session.
Service classes allow you to specify information about the driver,
like location and which port to use.
They also let you specify what arguments get passed
to the command line. Most of the useful arguments are related to logging.
Default Service instance
To start a driver with a default service instance:
Note: If you are using Selenium 4.6 or greater, you shouldn’t need to set a driver location.
If you cannot update Selenium or have an advanced use case, here is how to specify the driver location:
Logging functionality varies between browsers. Most browsers allow you to
specify location and level of logs. Take a look at the respective browser page:
Selenium lets you automate browsers on remote computers if
there is a Selenium Grid running on them. The computer that
executes the code is referred to as the client computer, and the computer with the browser and driver is
referred to as the remote computer or sometimes as an end-node.
To direct Selenium tests to the remote computer, you need to use a Remote WebDriver class
and pass the URL including the port of the grid on that machine. Please see the grid documentation
for all the various ways the grid can be configured.
Basic Example
The driver needs to know where to send commands to and which browser to start on the Remote computer. So an address
and an options instance are both required.
packagedev.selenium.drivers;importdev.selenium.BaseTest;importjava.io.File;importjava.io.IOException;importjava.net.URL;importjava.nio.file.Files;importjava.nio.file.Path;importjava.time.Duration;importjava.util.ArrayList;importjava.util.Comparator;importjava.util.List;importjava.util.Map;importorg.junit.jupiter.api.Assertions;importorg.junit.jupiter.api.BeforeEach;importorg.junit.jupiter.api.Test;importorg.openqa.selenium.By;importorg.openqa.selenium.HasDownloads;importorg.openqa.selenium.WebElement;importorg.openqa.selenium.chrome.ChromeOptions;importorg.openqa.selenium.chromium.HasCasting;importorg.openqa.selenium.remote.Augmenter;importorg.openqa.selenium.remote.LocalFileDetector;importorg.openqa.selenium.remote.RemoteWebDriver;importorg.openqa.selenium.remote.http.ClientConfig;importorg.openqa.selenium.support.ui.WebDriverWait;publicclassRemoteWebDriverTestextendsBaseTest{URLgridUrl;@BeforeEachpublicvoidstartGrid(){gridUrl=startStandaloneGrid();}@TestpublicvoidrunRemote(){ChromeOptionsoptions=getDefaultChromeOptions();driver=newRemoteWebDriver(gridUrl,options);}@Testpublicvoiduploads(){ChromeOptionsoptions=getDefaultChromeOptions();driver=newRemoteWebDriver(gridUrl,options);driver.get("https://the-internet.herokuapp.com/upload");FileuploadFile=newFile("src/test/resources/selenium-snapshot.png");((RemoteWebDriver)driver).setFileDetector(newLocalFileDetector());WebElementfileInput=driver.findElement(By.cssSelector("input[type=file]"));fileInput.sendKeys(uploadFile.getAbsolutePath());driver.findElement(By.id("file-submit")).click();WebElementfileName=driver.findElement(By.id("uploaded-files"));Assertions.assertEquals("selenium-snapshot.png",fileName.getText());}@Testpublicvoiddownloads()throwsIOException{ChromeOptionsoptions=getDefaultChromeOptions();options.setEnableDownloads(true);driver=newRemoteWebDriver(gridUrl,options);List<String>fileNames=newArrayList<>();fileNames.add("file_1.txt");fileNames.add("file_2.jpg");driver.get("https://www.selenium.dev/selenium/web/downloads/download.html");driver.findElement(By.id("file-1")).click();driver.findElement(By.id("file-2")).click();newWebDriverWait(driver,Duration.ofSeconds(5)).until(d->((HasDownloads)d).getDownloadableFiles().contains("file_2.jpg"));List<String>files=((HasDownloads)driver).getDownloadableFiles();// Sorting them to avoid differences when comparing the filesfileNames.sort(Comparator.naturalOrder());files.sort(Comparator.naturalOrder());Assertions.assertEquals(fileNames,files);StringdownloadableFile=files.get(0);PathtargetDirectory=Files.createTempDirectory("download");((HasDownloads)driver).downloadFile(downloadableFile,targetDirectory);StringfileContent=String.join("",Files.readAllLines(targetDirectory.resolve(downloadableFile)));Assertions.assertEquals("Hello, World!",fileContent);((HasDownloads)driver).deleteDownloadableFiles();Assertions.assertTrue(((HasDownloads)driver).getDownloadableFiles().isEmpty());}@Testpublicvoidaugment(){ChromeOptionsoptions=getDefaultChromeOptions();driver=newRemoteWebDriver(gridUrl,options);driver=newAugmenter().augment(driver);Assertions.assertTrue(driverinstanceofHasCasting);}@TestpublicvoidremoteWebDriverBuilder(){driver=RemoteWebDriver.builder().address(gridUrl).oneOf(getDefaultChromeOptions()).setCapability("ext:options",Map.of("key","value")).config(ClientConfig.defaultConfig()).build();Assertions.assertTrue(driverinstanceofHasCasting);}}
Uploading a file is more complicated for Remote WebDriver sessions because the file you want to
upload is likely on the computer executing the code, but the driver on the
remote computer is looking for the provided path on its local file system.
The solution is to use a Local File Detector. When one is set, Selenium will bundle
the file, and send it to the remote machine, so the driver can see the reference to it.
Some bindings include a basic local file detector by default, and all of them allow
for a custom file detector.
Java does not include a Local File Detector by default, so you must always add one to do uploads.
packagedev.selenium.drivers;importdev.selenium.BaseTest;importjava.io.File;importjava.io.IOException;importjava.net.URL;importjava.nio.file.Files;importjava.nio.file.Path;importjava.time.Duration;importjava.util.ArrayList;importjava.util.Comparator;importjava.util.List;importjava.util.Map;importorg.junit.jupiter.api.Assertions;importorg.junit.jupiter.api.BeforeEach;importorg.junit.jupiter.api.Test;importorg.openqa.selenium.By;importorg.openqa.selenium.HasDownloads;importorg.openqa.selenium.WebElement;importorg.openqa.selenium.chrome.ChromeOptions;importorg.openqa.selenium.chromium.HasCasting;importorg.openqa.selenium.remote.Augmenter;importorg.openqa.selenium.remote.LocalFileDetector;importorg.openqa.selenium.remote.RemoteWebDriver;importorg.openqa.selenium.remote.http.ClientConfig;importorg.openqa.selenium.support.ui.WebDriverWait;publicclassRemoteWebDriverTestextendsBaseTest{URLgridUrl;@BeforeEachpublicvoidstartGrid(){gridUrl=startStandaloneGrid();}@TestpublicvoidrunRemote(){ChromeOptionsoptions=getDefaultChromeOptions();driver=newRemoteWebDriver(gridUrl,options);}@Testpublicvoiduploads(){ChromeOptionsoptions=getDefaultChromeOptions();driver=newRemoteWebDriver(gridUrl,options);driver.get("https://the-internet.herokuapp.com/upload");FileuploadFile=newFile("src/test/resources/selenium-snapshot.png");((RemoteWebDriver)driver).setFileDetector(newLocalFileDetector());WebElementfileInput=driver.findElement(By.cssSelector("input[type=file]"));fileInput.sendKeys(uploadFile.getAbsolutePath());driver.findElement(By.id("file-submit")).click();WebElementfileName=driver.findElement(By.id("uploaded-files"));Assertions.assertEquals("selenium-snapshot.png",fileName.getText());}@Testpublicvoiddownloads()throwsIOException{ChromeOptionsoptions=getDefaultChromeOptions();options.setEnableDownloads(true);driver=newRemoteWebDriver(gridUrl,options);List<String>fileNames=newArrayList<>();fileNames.add("file_1.txt");fileNames.add("file_2.jpg");driver.get("https://www.selenium.dev/selenium/web/downloads/download.html");driver.findElement(By.id("file-1")).click();driver.findElement(By.id("file-2")).click();newWebDriverWait(driver,Duration.ofSeconds(5)).until(d->((HasDownloads)d).getDownloadableFiles().contains("file_2.jpg"));List<String>files=((HasDownloads)driver).getDownloadableFiles();// Sorting them to avoid differences when comparing the filesfileNames.sort(Comparator.naturalOrder());files.sort(Comparator.naturalOrder());Assertions.assertEquals(fileNames,files);StringdownloadableFile=files.get(0);PathtargetDirectory=Files.createTempDirectory("download");((HasDownloads)driver).downloadFile(downloadableFile,targetDirectory);StringfileContent=String.join("",Files.readAllLines(targetDirectory.resolve(downloadableFile)));Assertions.assertEquals("Hello, World!",fileContent);((HasDownloads)driver).deleteDownloadableFiles();Assertions.assertTrue(((HasDownloads)driver).getDownloadableFiles().isEmpty());}@Testpublicvoidaugment(){ChromeOptionsoptions=getDefaultChromeOptions();driver=newRemoteWebDriver(gridUrl,options);driver=newAugmenter().augment(driver);Assertions.assertTrue(driverinstanceofHasCasting);}@TestpublicvoidremoteWebDriverBuilder(){driver=RemoteWebDriver.builder().address(gridUrl).oneOf(getDefaultChromeOptions()).setCapability("ext:options",Map.of("key","value")).config(ClientConfig.defaultConfig()).build();Assertions.assertTrue(driverinstanceofHasCasting);}}
Chrome, Edge and Firefox each allow you to set the location of the download directory.
When you do this on a remote computer, though, the location is on the remote computer’s local file system.
Selenium allows you to enable downloads to get these files onto the client computer.
Enable Downloads in the Grid
Regardless of the client, when starting the grid in node or standalone mode,
you must add the flag:
--enable-managed-downloads true
Enable Downloads in the Client
The grid uses the se:downloadsEnabled capability to toggle whether to be responsible for managing the browser location.
Each of the bindings have a method in the options class to set this.
packagedev.selenium.drivers;importdev.selenium.BaseTest;importjava.io.File;importjava.io.IOException;importjava.net.URL;importjava.nio.file.Files;importjava.nio.file.Path;importjava.time.Duration;importjava.util.ArrayList;importjava.util.Comparator;importjava.util.List;importjava.util.Map;importorg.junit.jupiter.api.Assertions;importorg.junit.jupiter.api.BeforeEach;importorg.junit.jupiter.api.Test;importorg.openqa.selenium.By;importorg.openqa.selenium.HasDownloads;importorg.openqa.selenium.WebElement;importorg.openqa.selenium.chrome.ChromeOptions;importorg.openqa.selenium.chromium.HasCasting;importorg.openqa.selenium.remote.Augmenter;importorg.openqa.selenium.remote.LocalFileDetector;importorg.openqa.selenium.remote.RemoteWebDriver;importorg.openqa.selenium.remote.http.ClientConfig;importorg.openqa.selenium.support.ui.WebDriverWait;publicclassRemoteWebDriverTestextendsBaseTest{URLgridUrl;@BeforeEachpublicvoidstartGrid(){gridUrl=startStandaloneGrid();}@TestpublicvoidrunRemote(){ChromeOptionsoptions=getDefaultChromeOptions();driver=newRemoteWebDriver(gridUrl,options);}@Testpublicvoiduploads(){ChromeOptionsoptions=getDefaultChromeOptions();driver=newRemoteWebDriver(gridUrl,options);driver.get("https://the-internet.herokuapp.com/upload");FileuploadFile=newFile("src/test/resources/selenium-snapshot.png");((RemoteWebDriver)driver).setFileDetector(newLocalFileDetector());WebElementfileInput=driver.findElement(By.cssSelector("input[type=file]"));fileInput.sendKeys(uploadFile.getAbsolutePath());driver.findElement(By.id("file-submit")).click();WebElementfileName=driver.findElement(By.id("uploaded-files"));Assertions.assertEquals("selenium-snapshot.png",fileName.getText());}@Testpublicvoiddownloads()throwsIOException{ChromeOptionsoptions=getDefaultChromeOptions();options.setEnableDownloads(true);driver=newRemoteWebDriver(gridUrl,options);List<String>fileNames=newArrayList<>();fileNames.add("file_1.txt");fileNames.add("file_2.jpg");driver.get("https://www.selenium.dev/selenium/web/downloads/download.html");driver.findElement(By.id("file-1")).click();driver.findElement(By.id("file-2")).click();newWebDriverWait(driver,Duration.ofSeconds(5)).until(d->((HasDownloads)d).getDownloadableFiles().contains("file_2.jpg"));List<String>files=((HasDownloads)driver).getDownloadableFiles();// Sorting them to avoid differences when comparing the filesfileNames.sort(Comparator.naturalOrder());files.sort(Comparator.naturalOrder());Assertions.assertEquals(fileNames,files);StringdownloadableFile=files.get(0);PathtargetDirectory=Files.createTempDirectory("download");((HasDownloads)driver).downloadFile(downloadableFile,targetDirectory);StringfileContent=String.join("",Files.readAllLines(targetDirectory.resolve(downloadableFile)));Assertions.assertEquals("Hello, World!",fileContent);((HasDownloads)driver).deleteDownloadableFiles();Assertions.assertTrue(((HasDownloads)driver).getDownloadableFiles().isEmpty());}@Testpublicvoidaugment(){ChromeOptionsoptions=getDefaultChromeOptions();driver=newRemoteWebDriver(gridUrl,options);driver=newAugmenter().augment(driver);Assertions.assertTrue(driverinstanceofHasCasting);}@TestpublicvoidremoteWebDriverBuilder(){driver=RemoteWebDriver.builder().address(gridUrl).oneOf(getDefaultChromeOptions()).setCapability("ext:options",Map.of("key","value")).config(ClientConfig.defaultConfig()).build();Assertions.assertTrue(driverinstanceofHasCasting);}}
Be aware that Selenium is not waiting for files to finish downloading,
so the list is an immediate snapshot of what file names are currently in the directory for the given session.
packagedev.selenium.drivers;importdev.selenium.BaseTest;importjava.io.File;importjava.io.IOException;importjava.net.URL;importjava.nio.file.Files;importjava.nio.file.Path;importjava.time.Duration;importjava.util.ArrayList;importjava.util.Comparator;importjava.util.List;importjava.util.Map;importorg.junit.jupiter.api.Assertions;importorg.junit.jupiter.api.BeforeEach;importorg.junit.jupiter.api.Test;importorg.openqa.selenium.By;importorg.openqa.selenium.HasDownloads;importorg.openqa.selenium.WebElement;importorg.openqa.selenium.chrome.ChromeOptions;importorg.openqa.selenium.chromium.HasCasting;importorg.openqa.selenium.remote.Augmenter;importorg.openqa.selenium.remote.LocalFileDetector;importorg.openqa.selenium.remote.RemoteWebDriver;importorg.openqa.selenium.remote.http.ClientConfig;importorg.openqa.selenium.support.ui.WebDriverWait;publicclassRemoteWebDriverTestextendsBaseTest{URLgridUrl;@BeforeEachpublicvoidstartGrid(){gridUrl=startStandaloneGrid();}@TestpublicvoidrunRemote(){ChromeOptionsoptions=getDefaultChromeOptions();driver=newRemoteWebDriver(gridUrl,options);}@Testpublicvoiduploads(){ChromeOptionsoptions=getDefaultChromeOptions();driver=newRemoteWebDriver(gridUrl,options);driver.get("https://the-internet.herokuapp.com/upload");FileuploadFile=newFile("src/test/resources/selenium-snapshot.png");((RemoteWebDriver)driver).setFileDetector(newLocalFileDetector());WebElementfileInput=driver.findElement(By.cssSelector("input[type=file]"));fileInput.sendKeys(uploadFile.getAbsolutePath());driver.findElement(By.id("file-submit")).click();WebElementfileName=driver.findElement(By.id("uploaded-files"));Assertions.assertEquals("selenium-snapshot.png",fileName.getText());}@Testpublicvoiddownloads()throwsIOException{ChromeOptionsoptions=getDefaultChromeOptions();options.setEnableDownloads(true);driver=newRemoteWebDriver(gridUrl,options);List<String>fileNames=newArrayList<>();fileNames.add("file_1.txt");fileNames.add("file_2.jpg");driver.get("https://www.selenium.dev/selenium/web/downloads/download.html");driver.findElement(By.id("file-1")).click();driver.findElement(By.id("file-2")).click();newWebDriverWait(driver,Duration.ofSeconds(5)).until(d->((HasDownloads)d).getDownloadableFiles().contains("file_2.jpg"));List<String>files=((HasDownloads)driver).getDownloadableFiles();// Sorting them to avoid differences when comparing the filesfileNames.sort(Comparator.naturalOrder());files.sort(Comparator.naturalOrder());Assertions.assertEquals(fileNames,files);StringdownloadableFile=files.get(0);PathtargetDirectory=Files.createTempDirectory("download");((HasDownloads)driver).downloadFile(downloadableFile,targetDirectory);StringfileContent=String.join("",Files.readAllLines(targetDirectory.resolve(downloadableFile)));Assertions.assertEquals("Hello, World!",fileContent);((HasDownloads)driver).deleteDownloadableFiles();Assertions.assertTrue(((HasDownloads)driver).getDownloadableFiles().isEmpty());}@Testpublicvoidaugment(){ChromeOptionsoptions=getDefaultChromeOptions();driver=newRemoteWebDriver(gridUrl,options);driver=newAugmenter().augment(driver);Assertions.assertTrue(driverinstanceofHasCasting);}@TestpublicvoidremoteWebDriverBuilder(){driver=RemoteWebDriver.builder().address(gridUrl).oneOf(getDefaultChromeOptions()).setCapability("ext:options",Map.of("key","value")).config(ClientConfig.defaultConfig()).build();Assertions.assertTrue(driverinstanceofHasCasting);}}
packagedev.selenium.drivers;importdev.selenium.BaseTest;importjava.io.File;importjava.io.IOException;importjava.net.URL;importjava.nio.file.Files;importjava.nio.file.Path;importjava.time.Duration;importjava.util.ArrayList;importjava.util.Comparator;importjava.util.List;importjava.util.Map;importorg.junit.jupiter.api.Assertions;importorg.junit.jupiter.api.BeforeEach;importorg.junit.jupiter.api.Test;importorg.openqa.selenium.By;importorg.openqa.selenium.HasDownloads;importorg.openqa.selenium.WebElement;importorg.openqa.selenium.chrome.ChromeOptions;importorg.openqa.selenium.chromium.HasCasting;importorg.openqa.selenium.remote.Augmenter;importorg.openqa.selenium.remote.LocalFileDetector;importorg.openqa.selenium.remote.RemoteWebDriver;importorg.openqa.selenium.remote.http.ClientConfig;importorg.openqa.selenium.support.ui.WebDriverWait;publicclassRemoteWebDriverTestextendsBaseTest{URLgridUrl;@BeforeEachpublicvoidstartGrid(){gridUrl=startStandaloneGrid();}@TestpublicvoidrunRemote(){ChromeOptionsoptions=getDefaultChromeOptions();driver=newRemoteWebDriver(gridUrl,options);}@Testpublicvoiduploads(){ChromeOptionsoptions=getDefaultChromeOptions();driver=newRemoteWebDriver(gridUrl,options);driver.get("https://the-internet.herokuapp.com/upload");FileuploadFile=newFile("src/test/resources/selenium-snapshot.png");((RemoteWebDriver)driver).setFileDetector(newLocalFileDetector());WebElementfileInput=driver.findElement(By.cssSelector("input[type=file]"));fileInput.sendKeys(uploadFile.getAbsolutePath());driver.findElement(By.id("file-submit")).click();WebElementfileName=driver.findElement(By.id("uploaded-files"));Assertions.assertEquals("selenium-snapshot.png",fileName.getText());}@Testpublicvoiddownloads()throwsIOException{ChromeOptionsoptions=getDefaultChromeOptions();options.setEnableDownloads(true);driver=newRemoteWebDriver(gridUrl,options);List<String>fileNames=newArrayList<>();fileNames.add("file_1.txt");fileNames.add("file_2.jpg");driver.get("https://www.selenium.dev/selenium/web/downloads/download.html");driver.findElement(By.id("file-1")).click();driver.findElement(By.id("file-2")).click();newWebDriverWait(driver,Duration.ofSeconds(5)).until(d->((HasDownloads)d).getDownloadableFiles().contains("file_2.jpg"));List<String>files=((HasDownloads)driver).getDownloadableFiles();// Sorting them to avoid differences when comparing the filesfileNames.sort(Comparator.naturalOrder());files.sort(Comparator.naturalOrder());Assertions.assertEquals(fileNames,files);StringdownloadableFile=files.get(0);PathtargetDirectory=Files.createTempDirectory("download");((HasDownloads)driver).downloadFile(downloadableFile,targetDirectory);StringfileContent=String.join("",Files.readAllLines(targetDirectory.resolve(downloadableFile)));Assertions.assertEquals("Hello, World!",fileContent);((HasDownloads)driver).deleteDownloadableFiles();Assertions.assertTrue(((HasDownloads)driver).getDownloadableFiles().isEmpty());}@Testpublicvoidaugment(){ChromeOptionsoptions=getDefaultChromeOptions();driver=newRemoteWebDriver(gridUrl,options);driver=newAugmenter().augment(driver);Assertions.assertTrue(driverinstanceofHasCasting);}@TestpublicvoidremoteWebDriverBuilder(){driver=RemoteWebDriver.builder().address(gridUrl).oneOf(getDefaultChromeOptions()).setCapability("ext:options",Map.of("key","value")).config(ClientConfig.defaultConfig()).build();Assertions.assertTrue(driverinstanceofHasCasting);}}
By default, the download directory is deleted at the end of the applicable session,
but you can also delete all files during the session.
((HasDownloads)driver).deleteDownloadableFiles();
Show full example
packagedev.selenium.drivers;importdev.selenium.BaseTest;importjava.io.File;importjava.io.IOException;importjava.net.URL;importjava.nio.file.Files;importjava.nio.file.Path;importjava.time.Duration;importjava.util.ArrayList;importjava.util.Comparator;importjava.util.List;importjava.util.Map;importorg.junit.jupiter.api.Assertions;importorg.junit.jupiter.api.BeforeEach;importorg.junit.jupiter.api.Test;importorg.openqa.selenium.By;importorg.openqa.selenium.HasDownloads;importorg.openqa.selenium.WebElement;importorg.openqa.selenium.chrome.ChromeOptions;importorg.openqa.selenium.chromium.HasCasting;importorg.openqa.selenium.remote.Augmenter;importorg.openqa.selenium.remote.LocalFileDetector;importorg.openqa.selenium.remote.RemoteWebDriver;importorg.openqa.selenium.remote.http.ClientConfig;importorg.openqa.selenium.support.ui.WebDriverWait;publicclassRemoteWebDriverTestextendsBaseTest{URLgridUrl;@BeforeEachpublicvoidstartGrid(){gridUrl=startStandaloneGrid();}@TestpublicvoidrunRemote(){ChromeOptionsoptions=getDefaultChromeOptions();driver=newRemoteWebDriver(gridUrl,options);}@Testpublicvoiduploads(){ChromeOptionsoptions=getDefaultChromeOptions();driver=newRemoteWebDriver(gridUrl,options);driver.get("https://the-internet.herokuapp.com/upload");FileuploadFile=newFile("src/test/resources/selenium-snapshot.png");((RemoteWebDriver)driver).setFileDetector(newLocalFileDetector());WebElementfileInput=driver.findElement(By.cssSelector("input[type=file]"));fileInput.sendKeys(uploadFile.getAbsolutePath());driver.findElement(By.id("file-submit")).click();WebElementfileName=driver.findElement(By.id("uploaded-files"));Assertions.assertEquals("selenium-snapshot.png",fileName.getText());}@Testpublicvoiddownloads()throwsIOException{ChromeOptionsoptions=getDefaultChromeOptions();options.setEnableDownloads(true);driver=newRemoteWebDriver(gridUrl,options);List<String>fileNames=newArrayList<>();fileNames.add("file_1.txt");fileNames.add("file_2.jpg");driver.get("https://www.selenium.dev/selenium/web/downloads/download.html");driver.findElement(By.id("file-1")).click();driver.findElement(By.id("file-2")).click();newWebDriverWait(driver,Duration.ofSeconds(5)).until(d->((HasDownloads)d).getDownloadableFiles().contains("file_2.jpg"));List<String>files=((HasDownloads)driver).getDownloadableFiles();// Sorting them to avoid differences when comparing the filesfileNames.sort(Comparator.naturalOrder());files.sort(Comparator.naturalOrder());Assertions.assertEquals(fileNames,files);StringdownloadableFile=files.get(0);PathtargetDirectory=Files.createTempDirectory("download");((HasDownloads)driver).downloadFile(downloadableFile,targetDirectory);StringfileContent=String.join("",Files.readAllLines(targetDirectory.resolve(downloadableFile)));Assertions.assertEquals("Hello, World!",fileContent);((HasDownloads)driver).deleteDownloadableFiles();Assertions.assertTrue(((HasDownloads)driver).getDownloadableFiles().isEmpty());}@Testpublicvoidaugment(){ChromeOptionsoptions=getDefaultChromeOptions();driver=newRemoteWebDriver(gridUrl,options);driver=newAugmenter().augment(driver);Assertions.assertTrue(driverinstanceofHasCasting);}@TestpublicvoidremoteWebDriverBuilder(){driver=RemoteWebDriver.builder().address(gridUrl).oneOf(getDefaultChromeOptions()).setCapability("ext:options",Map.of("key","value")).config(ClientConfig.defaultConfig()).build();Assertions.assertTrue(driverinstanceofHasCasting);}}
Each browser has implemented special functionality that is available only to that browser.
Each of the Selenium bindings has implemented a different way to use those features in a Remote Session
Java requires you to use the Augmenter class, which allows it to automatically pull in implementations for
all interfaces that match the capabilities used with the RemoteWebDriver
driver=newAugmenter().augment(driver);
Show full example
packagedev.selenium.drivers;importdev.selenium.BaseTest;importjava.io.File;importjava.io.IOException;importjava.net.URL;importjava.nio.file.Files;importjava.nio.file.Path;importjava.time.Duration;importjava.util.ArrayList;importjava.util.Comparator;importjava.util.List;importjava.util.Map;importorg.junit.jupiter.api.Assertions;importorg.junit.jupiter.api.BeforeEach;importorg.junit.jupiter.api.Test;importorg.openqa.selenium.By;importorg.openqa.selenium.HasDownloads;importorg.openqa.selenium.WebElement;importorg.openqa.selenium.chrome.ChromeOptions;importorg.openqa.selenium.chromium.HasCasting;importorg.openqa.selenium.remote.Augmenter;importorg.openqa.selenium.remote.LocalFileDetector;importorg.openqa.selenium.remote.RemoteWebDriver;importorg.openqa.selenium.remote.http.ClientConfig;importorg.openqa.selenium.support.ui.WebDriverWait;publicclassRemoteWebDriverTestextendsBaseTest{URLgridUrl;@BeforeEachpublicvoidstartGrid(){gridUrl=startStandaloneGrid();}@TestpublicvoidrunRemote(){ChromeOptionsoptions=getDefaultChromeOptions();driver=newRemoteWebDriver(gridUrl,options);}@Testpublicvoiduploads(){ChromeOptionsoptions=getDefaultChromeOptions();driver=newRemoteWebDriver(gridUrl,options);driver.get("https://the-internet.herokuapp.com/upload");FileuploadFile=newFile("src/test/resources/selenium-snapshot.png");((RemoteWebDriver)driver).setFileDetector(newLocalFileDetector());WebElementfileInput=driver.findElement(By.cssSelector("input[type=file]"));fileInput.sendKeys(uploadFile.getAbsolutePath());driver.findElement(By.id("file-submit")).click();WebElementfileName=driver.findElement(By.id("uploaded-files"));Assertions.assertEquals("selenium-snapshot.png",fileName.getText());}@Testpublicvoiddownloads()throwsIOException{ChromeOptionsoptions=getDefaultChromeOptions();options.setEnableDownloads(true);driver=newRemoteWebDriver(gridUrl,options);List<String>fileNames=newArrayList<>();fileNames.add("file_1.txt");fileNames.add("file_2.jpg");driver.get("https://www.selenium.dev/selenium/web/downloads/download.html");driver.findElement(By.id("file-1")).click();driver.findElement(By.id("file-2")).click();newWebDriverWait(driver,Duration.ofSeconds(5)).until(d->((HasDownloads)d).getDownloadableFiles().contains("file_2.jpg"));List<String>files=((HasDownloads)driver).getDownloadableFiles();// Sorting them to avoid differences when comparing the filesfileNames.sort(Comparator.naturalOrder());files.sort(Comparator.naturalOrder());Assertions.assertEquals(fileNames,files);StringdownloadableFile=files.get(0);PathtargetDirectory=Files.createTempDirectory("download");((HasDownloads)driver).downloadFile(downloadableFile,targetDirectory);StringfileContent=String.join("",Files.readAllLines(targetDirectory.resolve(downloadableFile)));Assertions.assertEquals("Hello, World!",fileContent);((HasDownloads)driver).deleteDownloadableFiles();Assertions.assertTrue(((HasDownloads)driver).getDownloadableFiles().isEmpty());}@Testpublicvoidaugment(){ChromeOptionsoptions=getDefaultChromeOptions();driver=newRemoteWebDriver(gridUrl,options);driver=newAugmenter().augment(driver);Assertions.assertTrue(driverinstanceofHasCasting);}@TestpublicvoidremoteWebDriverBuilder(){driver=RemoteWebDriver.builder().address(gridUrl).oneOf(getDefaultChromeOptions()).setCapability("ext:options",Map.of("key","value")).config(ClientConfig.defaultConfig()).build();Assertions.assertTrue(driverinstanceofHasCasting);}}
packagedev.selenium.drivers;importdev.selenium.BaseTest;importjava.io.File;importjava.io.IOException;importjava.net.URL;importjava.nio.file.Files;importjava.nio.file.Path;importjava.time.Duration;importjava.util.ArrayList;importjava.util.Comparator;importjava.util.List;importjava.util.Map;importorg.junit.jupiter.api.Assertions;importorg.junit.jupiter.api.BeforeEach;importorg.junit.jupiter.api.Test;importorg.openqa.selenium.By;importorg.openqa.selenium.HasDownloads;importorg.openqa.selenium.WebElement;importorg.openqa.selenium.chrome.ChromeOptions;importorg.openqa.selenium.chromium.HasCasting;importorg.openqa.selenium.remote.Augmenter;importorg.openqa.selenium.remote.LocalFileDetector;importorg.openqa.selenium.remote.RemoteWebDriver;importorg.openqa.selenium.remote.http.ClientConfig;importorg.openqa.selenium.support.ui.WebDriverWait;publicclassRemoteWebDriverTestextendsBaseTest{URLgridUrl;@BeforeEachpublicvoidstartGrid(){gridUrl=startStandaloneGrid();}@TestpublicvoidrunRemote(){ChromeOptionsoptions=getDefaultChromeOptions();driver=newRemoteWebDriver(gridUrl,options);}@Testpublicvoiduploads(){ChromeOptionsoptions=getDefaultChromeOptions();driver=newRemoteWebDriver(gridUrl,options);driver.get("https://the-internet.herokuapp.com/upload");FileuploadFile=newFile("src/test/resources/selenium-snapshot.png");((RemoteWebDriver)driver).setFileDetector(newLocalFileDetector());WebElementfileInput=driver.findElement(By.cssSelector("input[type=file]"));fileInput.sendKeys(uploadFile.getAbsolutePath());driver.findElement(By.id("file-submit")).click();WebElementfileName=driver.findElement(By.id("uploaded-files"));Assertions.assertEquals("selenium-snapshot.png",fileName.getText());}@Testpublicvoiddownloads()throwsIOException{ChromeOptionsoptions=getDefaultChromeOptions();options.setEnableDownloads(true);driver=newRemoteWebDriver(gridUrl,options);List<String>fileNames=newArrayList<>();fileNames.add("file_1.txt");fileNames.add("file_2.jpg");driver.get("https://www.selenium.dev/selenium/web/downloads/download.html");driver.findElement(By.id("file-1")).click();driver.findElement(By.id("file-2")).click();newWebDriverWait(driver,Duration.ofSeconds(5)).until(d->((HasDownloads)d).getDownloadableFiles().contains("file_2.jpg"));List<String>files=((HasDownloads)driver).getDownloadableFiles();// Sorting them to avoid differences when comparing the filesfileNames.sort(Comparator.naturalOrder());files.sort(Comparator.naturalOrder());Assertions.assertEquals(fileNames,files);StringdownloadableFile=files.get(0);PathtargetDirectory=Files.createTempDirectory("download");((HasDownloads)driver).downloadFile(downloadableFile,targetDirectory);StringfileContent=String.join("",Files.readAllLines(targetDirectory.resolve(downloadableFile)));Assertions.assertEquals("Hello, World!",fileContent);((HasDownloads)driver).deleteDownloadableFiles();Assertions.assertTrue(((HasDownloads)driver).getDownloadableFiles().isEmpty());}@Testpublicvoidaugment(){ChromeOptionsoptions=getDefaultChromeOptions();driver=newRemoteWebDriver(gridUrl,options);driver=newAugmenter().augment(driver);Assertions.assertTrue(driverinstanceofHasCasting);}@TestpublicvoidremoteWebDriverBuilder(){driver=RemoteWebDriver.builder().address(gridUrl).oneOf(getDefaultChromeOptions()).setCapability("ext:options",Map.of("key","value")).config(ClientConfig.defaultConfig()).build();Assertions.assertTrue(driverinstanceofHasCasting);}}
This feature is only available for Java client binding (Beta onwards). The Remote WebDriver client sends requests to the Selenium Grid server, which passes them to the WebDriver. Tracing should be enabled at the server and client-side to trace the HTTP requests end-to-end. Both ends should have a trace exporter setup pointing to the visualization framework.
By default, tracing is enabled for both client and server.
To set up the visualization framework Jaeger UI and Selenium Grid 4, please refer to Tracing Setup for the desired version.
For client-side setup, follow the steps below.
Add the required dependencies
Installation of external libraries for tracing exporter can be done using Maven.
Add the opentelemetry-exporter-jaeger and grpc-netty dependency in your project pom.xml: