ใใฉใฆใถใผใชใใทใงใณ
Selenium 3 ใงใฏใCapabilitiesใฏ Desired Capabilities ใฏใฉในใไฝฟ็จใใฆใปใใทใงใณใงๅฎ็พฉใใฆใใพใใใ Selenium 4 ไปฅ้ใใใฉใฆใถ ใชใใทใงใณ ใฏใฉในใไฝฟ็จใใๅฟ ่ฆใใใใพใใ ใชใขใผใ ใใฉใคใใผ ใปใใทใงใณใฎๅ ดๅใไฝฟ็จใใใใฉใฆใถใผใๆฑบใใใใใใใฉใฆใถใผใชใใทใงใณใคใณในใฟใณในใๅฟ ่ฆใงใใ
ใใใใฎใชใใทใงใณใฏใCapabilities ใฎ w3cไปๆงใง่ชฌๆใใฆใใพใใ
ๅใใฉใฆใถใซใฏใw3cไปๆงใงๅฎ็พฉใใฆใใใใฎใซๅ ใใฆๅฎ็พฉๅฏ่ฝใช ใซในใฟใ ใชใใทใงใณ ใใใใพใใ
browserName
ใชใใทใงใณใฏใฉในใฎใคใณในใฟใณในใไฝฟ็จใใใจใใใฉใฆใถๅใฏใใใฉใซใใง่จญๅฎใใใพใใ
ChromeOptions chromeOptions = getDefaultChromeOptions();
String name = chromeOptions.getBrowserName();
/examples/java/src/test/java/dev/selenium/drivers/OptionsTest.java
package dev.selenium.drivers;
import dev.selenium.BaseTest;
import java.time.Duration;
import java.time.temporal.ChronoUnit;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Assertions;
import org.openqa.selenium.PageLoadStrategy;
import org.openqa.selenium.UnexpectedAlertBehaviour;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.chrome.ChromeDriver;
public class OptionsTest extends BaseTest {
@Test
public void setPageLoadStrategyNormal() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
chromeOptions.setPageLoadStrategy(PageLoadStrategy.NORMAL);
WebDriver driver = new ChromeDriver(chromeOptions);
try {
// Navigate to Url
driver.get("https://selenium.dev");
} finally {
driver.quit();
}
}
@Test
public void setPageLoadStrategyEager() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
chromeOptions.setPageLoadStrategy(PageLoadStrategy.EAGER);
WebDriver driver = new ChromeDriver(chromeOptions);
try {
// Navigate to Url
driver.get("https://selenium.dev");
} finally {
driver.quit();
}
}
@Test
public void setPageLoadStrategyNone() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
chromeOptions.setPageLoadStrategy(PageLoadStrategy.NONE);
WebDriver driver = new ChromeDriver(chromeOptions);
try {
// Navigate to Url
driver.get("https://selenium.dev");
} finally {
driver.quit();
}
}
@Test
public void setAcceptInsecureCerts() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
chromeOptions.setAcceptInsecureCerts(true);
WebDriver driver = new ChromeDriver(chromeOptions);
try {
// Navigate to Url
driver.get("https://selenium.dev");
} finally {
driver.quit();
}
}
@Test
public void getBrowserName() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
String name = chromeOptions.getBrowserName();
Assertions.assertFalse(name.isEmpty(), "Browser name should not be empty");
}
@Test
public void setBrowserVersion() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
String version = "latest";
chromeOptions.setBrowserVersion(version);
Assertions.assertEquals(version, chromeOptions.getBrowserVersion());
}
@Test
public void setPlatformName() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
String platform = "OS X 10.6";
chromeOptions.setPlatformName(platform);
Assertions.assertEquals(platform, chromeOptions.getPlatformName().toString());
}
@Test
public void setScriptTimeout() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
Duration duration = Duration.of(5, ChronoUnit.SECONDS);
chromeOptions.setScriptTimeout(duration);
WebDriver driver = new ChromeDriver(chromeOptions);
try {
Duration timeout = driver.manage().timeouts().getScriptTimeout();
Assertions.assertEquals(timeout, duration, "The script timeout should be set to 5 seconds.");
} finally {
driver.quit();
}
}
@Test
public void setPageLoadTimeout() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
Duration duration = Duration.of(5, ChronoUnit.SECONDS);
chromeOptions.setPageLoadTimeout(duration);
WebDriver driver = new ChromeDriver(chromeOptions);
try {
Duration timeout = driver.manage().timeouts().getPageLoadTimeout();
Assertions.assertEquals(timeout, duration, "The page load timeout should be set to 5 seconds.");
} finally {
driver.quit();
}
}
@Test
public void setImplicitWaitTimeout() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
Duration duration = Duration.of(5, ChronoUnit.SECONDS);
chromeOptions.setImplicitWaitTimeout(duration);
WebDriver driver = new ChromeDriver(chromeOptions);
try {
Duration timeout = driver.manage().timeouts().getImplicitWaitTimeout();
Assertions.assertEquals(timeout, duration, "The implicit wait timeout should be set to 5 seconds.");
} finally {
driver.quit();
}
}
@Test
public void setUnhandledPromptBehaviour() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
chromeOptions.setUnhandledPromptBehaviour(UnexpectedAlertBehaviour.DISMISS_AND_NOTIFY);
//verify the capability object is not null
Object capabilityObject = 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());
}
@Test
public void setWindowRect() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
chromeOptions.setCapability(CapabilityType.SET_WINDOW_RECT, true);
//verify the capability object is not null
Object capabilityObject = chromeOptions.getCapability(CapabilityType.SET_WINDOW_RECT);
Assertions.assertNotNull(capabilityObject, "Capability SET_WINDOW_RECT should not be null.");
Boolean capability = (Boolean) capabilityObject;
Assertions.assertTrue(capability, "The capability SET_WINDOW_RECT should be set to true.");
}
@Test
public void setStrictFileInteractability() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
chromeOptions.setCapability(CapabilityType.STRICT_FILE_INTERACTABILITY, true);
//verify the capability object is not null
Object capabilityObject = chromeOptions.getCapability(CapabilityType.STRICT_FILE_INTERACTABILITY);
Assertions.assertNotNull(capabilityObject, "Capability STRICT_FILE_INTERACTABILITY should not be null.");
Boolean capability = (Boolean) capabilityObject;
Assertions.assertTrue(capability, "The capability STRICT_FILE_INTERACTABILITY should be set to true.");
}
}
options = get_default_chrome_options()
assert options.capabilities['browserName'] == 'chrome'
/examples/python/tests/drivers/test_options.py
from selenium import webdriver
from selenium.webdriver.common.proxy import Proxy
from selenium.webdriver.common.proxy import ProxyType
def test_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()
def test_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()
def test_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()
def test_timeouts_script():
options = get_default_chrome_options()
options.timeouts = { 'script': 5000 }
driver = webdriver.Chrome(options=options)
driver.get("https://www.selenium.dev/")
driver.quit()
def test_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()
def test_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()
def test_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()
def test_set_window_rect():
options = webdriver.FirefoxOptions()
options.set_window_rect = True # Full support in Firefox
driver = webdriver.Firefox(options=options)
driver.get("https://www.selenium.dev/")
driver.quit()
def test_strict_file_interactability():
options = get_default_chrome_options()
options.strict_file_interactability = True
driver = webdriver.Chrome(options=options)
driver.get("https://www.selenium.dev/")
driver.quit()
def test_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()
def test_set_browser_name():
options = get_default_chrome_options()
assert options.capabilities['browserName'] == 'chrome'
driver = webdriver.Chrome(options=options)
driver.get("https://www.selenium.dev/")
driver.quit()
def test_set_browser_version():
options = get_default_chrome_options()
options.browser_version = 'stable'
assert options.capabilities['browserVersion'] == 'stable'
driver = webdriver.Chrome(options=options)
driver.get("https://www.selenium.dev/")
driver.quit()
def test_platform_name():
options = get_default_chrome_options()
options.platform_name = 'any'
driver = webdriver.Chrome(options=options)
driver.get("https://www.selenium.dev/")
driver.quit()
def test_accept_insecure_certs():
options = get_default_chrome_options()
options.accept_insecure_certs = True
driver = webdriver.Chrome(options=options)
driver.get("https://www.selenium.dev/")
driver.quit()
def get_default_chrome_options():
options = webdriver.ChromeOptions()
options.add_argument("--no-sandbox")
return options
options = Selenium::WebDriver::Options.chrome
/examples/ruby/spec/drivers/options_spec.rb
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe 'Chrome' do
describe 'Driver Options' do
let(:chrome_location) { driver_finder && ENV.fetch('CHROME_BIN', nil) }
let(:url) { 'https://www.selenium.dev/selenium/web/' }
it 'page load strategy normal' do
options = Selenium::WebDriver::Options.chrome
options.page_load_strategy = :normal
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'page load strategy eager' do
options = Selenium::WebDriver::Options.chrome
options.page_load_strategy = :eager
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'page load strategy none' do
options = Selenium::WebDriver::Options.chrome
options.page_load_strategy = :none
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'sets remote capabilities', skip: 'this is example code that will not execute' do
options = Selenium::WebDriver::Options.firefox
options.platform_name = 'Windows 10'
options.browser_version = 'latest'
cloud_options = {}
cloud_options[:build] = my_test_build
cloud_options[:name] = my_test_name
options.add_option('cloud:options', cloud_options)
driver = Selenium::WebDriver.for :remote, capabilities: options
driver.get(url)
driver.quit
end
it 'accepts untrusted certificates' do
options = Selenium::WebDriver::Options.chrome
options.accept_insecure_certs = true
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'sets unhandled prompt behavior' do
options = Selenium::WebDriver::Options.chrome
options.unhandled_prompt_behavior = :accept
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'sets window rect' do
options = Selenium::WebDriver::Options.firefox
options.set_window_rect = true
driver = Selenium::WebDriver.for :firefox, options: options
driver.get(url)
driver.quit
end
it 'sets strict file interactability' do
options = Selenium::WebDriver::Options.chrome
options.strict_file_interactability = true
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'sets the proxy' do
options = Selenium::WebDriver::Options.chrome
options.proxy = Selenium::WebDriver::Proxy.new(http: 'myproxy.com:8080')
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'sets the implicit timeout' do
options = Selenium::WebDriver::Options.chrome
options.timeouts = {implicit: 1}
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'sets the page load timeout' do
options = Selenium::WebDriver::Options.chrome
options.timeouts = {page_load: 400_000}
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'sets the script timeout' do
options = Selenium::WebDriver::Options.chrome
options.timeouts = {script: 40_000}
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'sets capabilities in the pre-selenium 4 way', skip: 'this is example code that will not execute' do
caps = Selenium::WebDriver::Remote::Capabilities.firefox
caps[:platform] = 'Windows 10'
caps[:version] = '92'
caps[:build] = my_test_build
caps[:name] = my_test_name
driver = Selenium::WebDriver.for :remote, url: cloud_url, desired_capabilities: caps
driver.get(url)
driver.quit
end
end
end
browserVersion
ใใฎๆฉ่ฝใฏใชใใทใงใณใงใใใใชใขใผใๅดใงไฝฟ็จๅฏ่ฝใชใใฉใฆใถใฎใใผใธใงใณใ่จญๅฎใใใใใซไฝฟ็จใใใพใใๆ่ฟใฎSeleniumใฎใใผใธใงใณใงใฏใใทในใใ ใซใใผใธใงใณใ่ฆใคใใใชใๅ ดๅใSelenium Manager ใซใใฃใฆ่ชๅ็ใซใใฆใณใญใผใใใใพใใ
ChromeOptions chromeOptions = getDefaultChromeOptions();
String version = "latest";
chromeOptions.setBrowserVersion(version);
/examples/java/src/test/java/dev/selenium/drivers/OptionsTest.java
package dev.selenium.drivers;
import dev.selenium.BaseTest;
import java.time.Duration;
import java.time.temporal.ChronoUnit;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Assertions;
import org.openqa.selenium.PageLoadStrategy;
import org.openqa.selenium.UnexpectedAlertBehaviour;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.chrome.ChromeDriver;
public class OptionsTest extends BaseTest {
@Test
public void setPageLoadStrategyNormal() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
chromeOptions.setPageLoadStrategy(PageLoadStrategy.NORMAL);
WebDriver driver = new ChromeDriver(chromeOptions);
try {
// Navigate to Url
driver.get("https://selenium.dev");
} finally {
driver.quit();
}
}
@Test
public void setPageLoadStrategyEager() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
chromeOptions.setPageLoadStrategy(PageLoadStrategy.EAGER);
WebDriver driver = new ChromeDriver(chromeOptions);
try {
// Navigate to Url
driver.get("https://selenium.dev");
} finally {
driver.quit();
}
}
@Test
public void setPageLoadStrategyNone() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
chromeOptions.setPageLoadStrategy(PageLoadStrategy.NONE);
WebDriver driver = new ChromeDriver(chromeOptions);
try {
// Navigate to Url
driver.get("https://selenium.dev");
} finally {
driver.quit();
}
}
@Test
public void setAcceptInsecureCerts() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
chromeOptions.setAcceptInsecureCerts(true);
WebDriver driver = new ChromeDriver(chromeOptions);
try {
// Navigate to Url
driver.get("https://selenium.dev");
} finally {
driver.quit();
}
}
@Test
public void getBrowserName() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
String name = chromeOptions.getBrowserName();
Assertions.assertFalse(name.isEmpty(), "Browser name should not be empty");
}
@Test
public void setBrowserVersion() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
String version = "latest";
chromeOptions.setBrowserVersion(version);
Assertions.assertEquals(version, chromeOptions.getBrowserVersion());
}
@Test
public void setPlatformName() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
String platform = "OS X 10.6";
chromeOptions.setPlatformName(platform);
Assertions.assertEquals(platform, chromeOptions.getPlatformName().toString());
}
@Test
public void setScriptTimeout() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
Duration duration = Duration.of(5, ChronoUnit.SECONDS);
chromeOptions.setScriptTimeout(duration);
WebDriver driver = new ChromeDriver(chromeOptions);
try {
Duration timeout = driver.manage().timeouts().getScriptTimeout();
Assertions.assertEquals(timeout, duration, "The script timeout should be set to 5 seconds.");
} finally {
driver.quit();
}
}
@Test
public void setPageLoadTimeout() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
Duration duration = Duration.of(5, ChronoUnit.SECONDS);
chromeOptions.setPageLoadTimeout(duration);
WebDriver driver = new ChromeDriver(chromeOptions);
try {
Duration timeout = driver.manage().timeouts().getPageLoadTimeout();
Assertions.assertEquals(timeout, duration, "The page load timeout should be set to 5 seconds.");
} finally {
driver.quit();
}
}
@Test
public void setImplicitWaitTimeout() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
Duration duration = Duration.of(5, ChronoUnit.SECONDS);
chromeOptions.setImplicitWaitTimeout(duration);
WebDriver driver = new ChromeDriver(chromeOptions);
try {
Duration timeout = driver.manage().timeouts().getImplicitWaitTimeout();
Assertions.assertEquals(timeout, duration, "The implicit wait timeout should be set to 5 seconds.");
} finally {
driver.quit();
}
}
@Test
public void setUnhandledPromptBehaviour() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
chromeOptions.setUnhandledPromptBehaviour(UnexpectedAlertBehaviour.DISMISS_AND_NOTIFY);
//verify the capability object is not null
Object capabilityObject = 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());
}
@Test
public void setWindowRect() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
chromeOptions.setCapability(CapabilityType.SET_WINDOW_RECT, true);
//verify the capability object is not null
Object capabilityObject = chromeOptions.getCapability(CapabilityType.SET_WINDOW_RECT);
Assertions.assertNotNull(capabilityObject, "Capability SET_WINDOW_RECT should not be null.");
Boolean capability = (Boolean) capabilityObject;
Assertions.assertTrue(capability, "The capability SET_WINDOW_RECT should be set to true.");
}
@Test
public void setStrictFileInteractability() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
chromeOptions.setCapability(CapabilityType.STRICT_FILE_INTERACTABILITY, true);
//verify the capability object is not null
Object capabilityObject = chromeOptions.getCapability(CapabilityType.STRICT_FILE_INTERACTABILITY);
Assertions.assertNotNull(capabilityObject, "Capability STRICT_FILE_INTERACTABILITY should not be null.");
Boolean capability = (Boolean) capabilityObject;
Assertions.assertTrue(capability, "The capability STRICT_FILE_INTERACTABILITY should be set to true.");
}
}
options = get_default_chrome_options()
options.browser_version = 'stable'
assert options.capabilities['browserVersion'] == 'stable'
/examples/python/tests/drivers/test_options.py
from selenium import webdriver
from selenium.webdriver.common.proxy import Proxy
from selenium.webdriver.common.proxy import ProxyType
def test_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()
def test_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()
def test_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()
def test_timeouts_script():
options = get_default_chrome_options()
options.timeouts = { 'script': 5000 }
driver = webdriver.Chrome(options=options)
driver.get("https://www.selenium.dev/")
driver.quit()
def test_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()
def test_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()
def test_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()
def test_set_window_rect():
options = webdriver.FirefoxOptions()
options.set_window_rect = True # Full support in Firefox
driver = webdriver.Firefox(options=options)
driver.get("https://www.selenium.dev/")
driver.quit()
def test_strict_file_interactability():
options = get_default_chrome_options()
options.strict_file_interactability = True
driver = webdriver.Chrome(options=options)
driver.get("https://www.selenium.dev/")
driver.quit()
def test_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()
def test_set_browser_name():
options = get_default_chrome_options()
assert options.capabilities['browserName'] == 'chrome'
driver = webdriver.Chrome(options=options)
driver.get("https://www.selenium.dev/")
driver.quit()
def test_set_browser_version():
options = get_default_chrome_options()
options.browser_version = 'stable'
assert options.capabilities['browserVersion'] == 'stable'
driver = webdriver.Chrome(options=options)
driver.get("https://www.selenium.dev/")
driver.quit()
def test_platform_name():
options = get_default_chrome_options()
options.platform_name = 'any'
driver = webdriver.Chrome(options=options)
driver.get("https://www.selenium.dev/")
driver.quit()
def test_accept_insecure_certs():
options = get_default_chrome_options()
options.accept_insecure_certs = True
driver = webdriver.Chrome(options=options)
driver.get("https://www.selenium.dev/")
driver.quit()
def get_default_chrome_options():
options = webdriver.ChromeOptions()
options.add_argument("--no-sandbox")
return options
options.browser_version = 'latest'
/examples/ruby/spec/drivers/options_spec.rb
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe 'Chrome' do
describe 'Driver Options' do
let(:chrome_location) { driver_finder && ENV.fetch('CHROME_BIN', nil) }
let(:url) { 'https://www.selenium.dev/selenium/web/' }
it 'page load strategy normal' do
options = Selenium::WebDriver::Options.chrome
options.page_load_strategy = :normal
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'page load strategy eager' do
options = Selenium::WebDriver::Options.chrome
options.page_load_strategy = :eager
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'page load strategy none' do
options = Selenium::WebDriver::Options.chrome
options.page_load_strategy = :none
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'sets remote capabilities', skip: 'this is example code that will not execute' do
options = Selenium::WebDriver::Options.firefox
options.platform_name = 'Windows 10'
options.browser_version = 'latest'
cloud_options = {}
cloud_options[:build] = my_test_build
cloud_options[:name] = my_test_name
options.add_option('cloud:options', cloud_options)
driver = Selenium::WebDriver.for :remote, capabilities: options
driver.get(url)
driver.quit
end
it 'accepts untrusted certificates' do
options = Selenium::WebDriver::Options.chrome
options.accept_insecure_certs = true
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'sets unhandled prompt behavior' do
options = Selenium::WebDriver::Options.chrome
options.unhandled_prompt_behavior = :accept
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'sets window rect' do
options = Selenium::WebDriver::Options.firefox
options.set_window_rect = true
driver = Selenium::WebDriver.for :firefox, options: options
driver.get(url)
driver.quit
end
it 'sets strict file interactability' do
options = Selenium::WebDriver::Options.chrome
options.strict_file_interactability = true
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'sets the proxy' do
options = Selenium::WebDriver::Options.chrome
options.proxy = Selenium::WebDriver::Proxy.new(http: 'myproxy.com:8080')
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'sets the implicit timeout' do
options = Selenium::WebDriver::Options.chrome
options.timeouts = {implicit: 1}
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'sets the page load timeout' do
options = Selenium::WebDriver::Options.chrome
options.timeouts = {page_load: 400_000}
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'sets the script timeout' do
options = Selenium::WebDriver::Options.chrome
options.timeouts = {script: 40_000}
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'sets capabilities in the pre-selenium 4 way', skip: 'this is example code that will not execute' do
caps = Selenium::WebDriver::Remote::Capabilities.firefox
caps[:platform] = 'Windows 10'
caps[:version] = '92'
caps[:build] = my_test_build
caps[:name] = my_test_name
driver = Selenium::WebDriver.for :remote, url: cloud_url, desired_capabilities: caps
driver.get(url)
driver.quit
end
end
end
pageLoadStrategy
3็จฎ้กใฎใใผใธ่ชญใฟ่พผใฟๆฆ็ฅใๅฉ็จใงใใพใใ
ใใผใธ่ชญใฟ่พผใฟๆฆ็ฅใฏใๆฌกใฎ่กจใง่ชฌๆใใฆใใพใใ
ๆฆ็ฅ | ๆบๅๅฎไบ็ถๆ | ๆณจ้ |
---|---|---|
normal | complete | ใใใฉใซใใงไฝฟ็จใใใใในใฆใฎใชใฝใผในใใใฆใณใญใผใใใใฎใๅพ ใกใพใ |
eager | interactive | DOM ใขใฏใปในใฎๆบๅใฏๆดใฃใฆใใพใใใ็ปๅใชใฉใฎไปใฎใชใฝใผในใฏใพใ ใญใผใไธญใฎๅฏ่ฝๆงใใใใพใ |
none | Any | WebDriver ใใพใฃใใใใญใใฏใใพใใ |
ใใญใฅใกใณใใฎ document.readyState ใใญใใใฃใฏใ็พๅจใฎใใญใฅใกใณใใฎ่ชญใฟ่พผใฟ็ถๆ ใ็คบใใพใใ
URL ็ต็ฑใงๆฐใใใใผใธใซ็งปๅใใๅ ดๅใใใใฉใซใใงใฏใWebDriver ใฏใใใญใฅใกใณใใฎๆบๅๅฎไบ็ถๆ ใๅฎไบใใใพใงใ ใใใฒใผใทใงใณ ใกใฝใใ (driver.navigate().get() ใชใฉ) ใฎๅฎไบใไฟ็ใใพใใ ใใใฏๅฟ ใใใใใผใธใฎ่ชญใฟ่พผใฟใๅฎไบใใใใจใๆๅณใใใใใงใฏใใใพใใใ ็นใซใReady State ใๅฎไบใใๅพใซ JavaScript ใไฝฟ็จใใฆใณใณใใณใใๅ็ใซ่ชญใฟ่พผใใทใณใฐใซ ใใผใธ ใขใใชใฑใผใทใงใณใฎใใใชใตใคใใฎๅ ดๅใฏใใใงใใ ใพใใใใฎๅไฝใฏใ่ฆ็ด ใฎใฏใชใใฏใพใใฏใใฉใผใ ใฎ้ไฟกใฎ็ตๆใงใใใใใฒใผใทใงใณใซใฏ้ฉ็จใใใชใใใจใซๆณจๆใใฆใใ ใใใ
่ชๅๅใซใจใฃใฆ้่ฆใงใฏใชใใขใปใใ (็ปๅใcssใjs ใชใฉ) ใใใฆใณใญใผใใใ็ตๆใใใผใธใฎ่ชญใฟ่พผใฟใซๆ้ใใใใๅ ดๅใฏใ
ใใใฉใซใใฎใใฉใกใผใฟใผใงใใ normal
ใ eager
ใพใใฏ none
ใซๅคๆดใใฆใใปใใทใงใณใฎ่ชญใฟ่พผใฟใ้ซ้ๅใงใใพใใ
ใใฎๅคใฏใปใใทใงใณๅ
จไฝใซ้ฉ็จใใใใใใ ๅพ
ๆฉๆฆ็ฅ
ใไธๅฎๅฎใใๆๅฐ้ใซๆใใใฎใซๅๅใงใใใใจใ็ขบ่ชใใฆใใ ใใใ
normal (ใใใฉใซใ)
WebDriver ใฏ load ใคใใณใๆค็ฅใใใพใงๅพ ๆฉใใพใใ
ChromeOptions chromeOptions = getDefaultChromeOptions();
chromeOptions.setPageLoadStrategy(PageLoadStrategy.NORMAL);
WebDriver driver = new ChromeDriver(chromeOptions);
/examples/java/src/test/java/dev/selenium/drivers/OptionsTest.java
package dev.selenium.drivers;
import dev.selenium.BaseTest;
import java.time.Duration;
import java.time.temporal.ChronoUnit;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Assertions;
import org.openqa.selenium.PageLoadStrategy;
import org.openqa.selenium.UnexpectedAlertBehaviour;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.chrome.ChromeDriver;
public class OptionsTest extends BaseTest {
@Test
public void setPageLoadStrategyNormal() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
chromeOptions.setPageLoadStrategy(PageLoadStrategy.NORMAL);
WebDriver driver = new ChromeDriver(chromeOptions);
try {
// Navigate to Url
driver.get("https://selenium.dev");
} finally {
driver.quit();
}
}
@Test
public void setPageLoadStrategyEager() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
chromeOptions.setPageLoadStrategy(PageLoadStrategy.EAGER);
WebDriver driver = new ChromeDriver(chromeOptions);
try {
// Navigate to Url
driver.get("https://selenium.dev");
} finally {
driver.quit();
}
}
@Test
public void setPageLoadStrategyNone() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
chromeOptions.setPageLoadStrategy(PageLoadStrategy.NONE);
WebDriver driver = new ChromeDriver(chromeOptions);
try {
// Navigate to Url
driver.get("https://selenium.dev");
} finally {
driver.quit();
}
}
@Test
public void setAcceptInsecureCerts() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
chromeOptions.setAcceptInsecureCerts(true);
WebDriver driver = new ChromeDriver(chromeOptions);
try {
// Navigate to Url
driver.get("https://selenium.dev");
} finally {
driver.quit();
}
}
@Test
public void getBrowserName() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
String name = chromeOptions.getBrowserName();
Assertions.assertFalse(name.isEmpty(), "Browser name should not be empty");
}
@Test
public void setBrowserVersion() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
String version = "latest";
chromeOptions.setBrowserVersion(version);
Assertions.assertEquals(version, chromeOptions.getBrowserVersion());
}
@Test
public void setPlatformName() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
String platform = "OS X 10.6";
chromeOptions.setPlatformName(platform);
Assertions.assertEquals(platform, chromeOptions.getPlatformName().toString());
}
@Test
public void setScriptTimeout() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
Duration duration = Duration.of(5, ChronoUnit.SECONDS);
chromeOptions.setScriptTimeout(duration);
WebDriver driver = new ChromeDriver(chromeOptions);
try {
Duration timeout = driver.manage().timeouts().getScriptTimeout();
Assertions.assertEquals(timeout, duration, "The script timeout should be set to 5 seconds.");
} finally {
driver.quit();
}
}
@Test
public void setPageLoadTimeout() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
Duration duration = Duration.of(5, ChronoUnit.SECONDS);
chromeOptions.setPageLoadTimeout(duration);
WebDriver driver = new ChromeDriver(chromeOptions);
try {
Duration timeout = driver.manage().timeouts().getPageLoadTimeout();
Assertions.assertEquals(timeout, duration, "The page load timeout should be set to 5 seconds.");
} finally {
driver.quit();
}
}
@Test
public void setImplicitWaitTimeout() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
Duration duration = Duration.of(5, ChronoUnit.SECONDS);
chromeOptions.setImplicitWaitTimeout(duration);
WebDriver driver = new ChromeDriver(chromeOptions);
try {
Duration timeout = driver.manage().timeouts().getImplicitWaitTimeout();
Assertions.assertEquals(timeout, duration, "The implicit wait timeout should be set to 5 seconds.");
} finally {
driver.quit();
}
}
@Test
public void setUnhandledPromptBehaviour() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
chromeOptions.setUnhandledPromptBehaviour(UnexpectedAlertBehaviour.DISMISS_AND_NOTIFY);
//verify the capability object is not null
Object capabilityObject = 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());
}
@Test
public void setWindowRect() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
chromeOptions.setCapability(CapabilityType.SET_WINDOW_RECT, true);
//verify the capability object is not null
Object capabilityObject = chromeOptions.getCapability(CapabilityType.SET_WINDOW_RECT);
Assertions.assertNotNull(capabilityObject, "Capability SET_WINDOW_RECT should not be null.");
Boolean capability = (Boolean) capabilityObject;
Assertions.assertTrue(capability, "The capability SET_WINDOW_RECT should be set to true.");
}
@Test
public void setStrictFileInteractability() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
chromeOptions.setCapability(CapabilityType.STRICT_FILE_INTERACTABILITY, true);
//verify the capability object is not null
Object capabilityObject = chromeOptions.getCapability(CapabilityType.STRICT_FILE_INTERACTABILITY);
Assertions.assertNotNull(capabilityObject, "Capability STRICT_FILE_INTERACTABILITY should not be null.");
Boolean capability = (Boolean) capabilityObject;
Assertions.assertTrue(capability, "The capability STRICT_FILE_INTERACTABILITY should be set to true.");
}
}
options = get_default_chrome_options()
options.page_load_strategy = 'normal'
driver = webdriver.Chrome(options=options)
/examples/python/tests/drivers/test_options.py
from selenium import webdriver
from selenium.webdriver.common.proxy import Proxy
from selenium.webdriver.common.proxy import ProxyType
def test_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()
def test_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()
def test_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()
def test_timeouts_script():
options = get_default_chrome_options()
options.timeouts = { 'script': 5000 }
driver = webdriver.Chrome(options=options)
driver.get("https://www.selenium.dev/")
driver.quit()
def test_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()
def test_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()
def test_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()
def test_set_window_rect():
options = webdriver.FirefoxOptions()
options.set_window_rect = True # Full support in Firefox
driver = webdriver.Firefox(options=options)
driver.get("https://www.selenium.dev/")
driver.quit()
def test_strict_file_interactability():
options = get_default_chrome_options()
options.strict_file_interactability = True
driver = webdriver.Chrome(options=options)
driver.get("https://www.selenium.dev/")
driver.quit()
def test_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()
def test_set_browser_name():
options = get_default_chrome_options()
assert options.capabilities['browserName'] == 'chrome'
driver = webdriver.Chrome(options=options)
driver.get("https://www.selenium.dev/")
driver.quit()
def test_set_browser_version():
options = get_default_chrome_options()
options.browser_version = 'stable'
assert options.capabilities['browserVersion'] == 'stable'
driver = webdriver.Chrome(options=options)
driver.get("https://www.selenium.dev/")
driver.quit()
def test_platform_name():
options = get_default_chrome_options()
options.platform_name = 'any'
driver = webdriver.Chrome(options=options)
driver.get("https://www.selenium.dev/")
driver.quit()
def test_accept_insecure_certs():
options = get_default_chrome_options()
options.accept_insecure_certs = True
driver = webdriver.Chrome(options=options)
driver.get("https://www.selenium.dev/")
driver.quit()
def get_default_chrome_options():
options = webdriver.ChromeOptions()
options.add_argument("--no-sandbox")
return options
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.PageLoadStrategy = PageLoadStrategy.Normal;
/examples/dotnet/SeleniumDocs/Drivers/OptionsTest.cs
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
namespace SeleniumDocs.Drivers
{
[TestClass]
public class OptionsTest : BaseTest
{
[TestMethod]
public void SetPageLoadStrategyNormal()
{
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.PageLoadStrategy = PageLoadStrategy.Normal;
IWebDriver driver = new ChromeDriver(chromeOptions);
try
{
// Navigate to Url
driver.Navigate().GoToUrl("https://selenium.dev");
}
finally
{
driver.Quit();
}
}
[TestMethod]
public void SetPageLoadStrategyEager()
{
var chromeOptions = new ChromeOptions();
chromeOptions.PageLoadStrategy = PageLoadStrategy.Eager;
IWebDriver driver = new ChromeDriver(chromeOptions);
try
{
driver.Navigate().GoToUrl("https://selenium.dev");
}
finally
{
driver.Quit();
}
}
[TestMethod]
public void SetPageLoadStrategyNone()
{
var chromeOptions = new ChromeOptions();
chromeOptions.PageLoadStrategy = PageLoadStrategy.None;
IWebDriver driver = new ChromeDriver(chromeOptions);
try
{
driver.Navigate().GoToUrl("https://selenium.dev");
}
finally
{
driver.Quit();
}
}
}
}
options = Selenium::WebDriver::Options.chrome
options.page_load_strategy = :normal
/examples/ruby/spec/drivers/options_spec.rb
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe 'Chrome' do
describe 'Driver Options' do
let(:chrome_location) { driver_finder && ENV.fetch('CHROME_BIN', nil) }
let(:url) { 'https://www.selenium.dev/selenium/web/' }
it 'page load strategy normal' do
options = Selenium::WebDriver::Options.chrome
options.page_load_strategy = :normal
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'page load strategy eager' do
options = Selenium::WebDriver::Options.chrome
options.page_load_strategy = :eager
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'page load strategy none' do
options = Selenium::WebDriver::Options.chrome
options.page_load_strategy = :none
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'sets remote capabilities', skip: 'this is example code that will not execute' do
options = Selenium::WebDriver::Options.firefox
options.platform_name = 'Windows 10'
options.browser_version = 'latest'
cloud_options = {}
cloud_options[:build] = my_test_build
cloud_options[:name] = my_test_name
options.add_option('cloud:options', cloud_options)
driver = Selenium::WebDriver.for :remote, capabilities: options
driver.get(url)
driver.quit
end
it 'accepts untrusted certificates' do
options = Selenium::WebDriver::Options.chrome
options.accept_insecure_certs = true
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'sets unhandled prompt behavior' do
options = Selenium::WebDriver::Options.chrome
options.unhandled_prompt_behavior = :accept
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'sets window rect' do
options = Selenium::WebDriver::Options.firefox
options.set_window_rect = true
driver = Selenium::WebDriver.for :firefox, options: options
driver.get(url)
driver.quit
end
it 'sets strict file interactability' do
options = Selenium::WebDriver::Options.chrome
options.strict_file_interactability = true
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'sets the proxy' do
options = Selenium::WebDriver::Options.chrome
options.proxy = Selenium::WebDriver::Proxy.new(http: 'myproxy.com:8080')
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'sets the implicit timeout' do
options = Selenium::WebDriver::Options.chrome
options.timeouts = {implicit: 1}
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'sets the page load timeout' do
options = Selenium::WebDriver::Options.chrome
options.timeouts = {page_load: 400_000}
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'sets the script timeout' do
options = Selenium::WebDriver::Options.chrome
options.timeouts = {script: 40_000}
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'sets capabilities in the pre-selenium 4 way', skip: 'this is example code that will not execute' do
caps = Selenium::WebDriver::Remote::Capabilities.firefox
caps[:platform] = 'Windows 10'
caps[:version] = '92'
caps[:build] = my_test_build
caps[:name] = my_test_name
driver = Selenium::WebDriver.for :remote, url: cloud_url, desired_capabilities: caps
driver.get(url)
driver.quit
end
end
end
let driver = new Builder()
.forBrowser(Browser.CHROME)
.setChromeOptions(options.setPageLoadStrategy('normal'))
.build();
await driver.get('https://www.selenium.dev/selenium/web/blank.html');
await driver.quit();
/examples/javascript/test/capabilities/pageLoading.spec.js
const Chrome = require('selenium-webdriver/chrome');
const {Browser, Builder} = require("selenium-webdriver");
const options = new Chrome.Options()
describe('Page loading strategies', function () {
it('Navigate using eager page loading strategy', async function () {
let driver = new Builder()
.forBrowser(Browser.CHROME)
.setChromeOptions(options.setPageLoadStrategy('eager'))
.build();
await driver.get('https://www.selenium.dev/selenium/web/blank.html');
await driver.quit();
});
it('Navigate using none page loading strategy', async function () {
let driver = new Builder()
.forBrowser(Browser.CHROME)
.setChromeOptions(options.setPageLoadStrategy('none'))
.build();
await driver.get('https://www.selenium.dev/selenium/web/blank.html');
await driver.quit();
});
it('Navigate using normal page loading strategy', async function () {
let driver = new Builder()
.forBrowser(Browser.CHROME)
.setChromeOptions(options.setPageLoadStrategy('normal'))
.build();
await driver.get('https://www.selenium.dev/selenium/web/blank.html');
await driver.quit();
});
it('Should be able to accept certs', async function () {
let driver = new Builder()
.forBrowser(Browser.CHROME)
.setChromeOptions(options.setAcceptInsecureCerts(true))
.build();
await driver.get('https://www.selenium.dev/selenium/web/blank.html');
await driver.quit();
});
});
import org.openqa.selenium.PageLoadStrategy
import org.openqa.selenium.chrome.ChromeDriver
import org.openqa.selenium.chrome.ChromeOptions
fun main() {
val chromeOptions = ChromeOptions()
chromeOptions.setPageLoadStrategy(PageLoadStrategy.NORMAL)
val driver = ChromeDriver(chromeOptions)
try {
driver.get("https://www.google.com")
}
finally {
driver.quit()
}
}
eager
WebDriver ใฏใDOMContentLoaded ใคใใณใใๆค็ฅใใใพใงๅพ ๆฉใใพใใ
ChromeOptions chromeOptions = getDefaultChromeOptions();
chromeOptions.setPageLoadStrategy(PageLoadStrategy.EAGER);
WebDriver driver = new ChromeDriver(chromeOptions);
/examples/java/src/test/java/dev/selenium/drivers/OptionsTest.java
package dev.selenium.drivers;
import dev.selenium.BaseTest;
import java.time.Duration;
import java.time.temporal.ChronoUnit;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Assertions;
import org.openqa.selenium.PageLoadStrategy;
import org.openqa.selenium.UnexpectedAlertBehaviour;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.chrome.ChromeDriver;
public class OptionsTest extends BaseTest {
@Test
public void setPageLoadStrategyNormal() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
chromeOptions.setPageLoadStrategy(PageLoadStrategy.NORMAL);
WebDriver driver = new ChromeDriver(chromeOptions);
try {
// Navigate to Url
driver.get("https://selenium.dev");
} finally {
driver.quit();
}
}
@Test
public void setPageLoadStrategyEager() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
chromeOptions.setPageLoadStrategy(PageLoadStrategy.EAGER);
WebDriver driver = new ChromeDriver(chromeOptions);
try {
// Navigate to Url
driver.get("https://selenium.dev");
} finally {
driver.quit();
}
}
@Test
public void setPageLoadStrategyNone() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
chromeOptions.setPageLoadStrategy(PageLoadStrategy.NONE);
WebDriver driver = new ChromeDriver(chromeOptions);
try {
// Navigate to Url
driver.get("https://selenium.dev");
} finally {
driver.quit();
}
}
@Test
public void setAcceptInsecureCerts() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
chromeOptions.setAcceptInsecureCerts(true);
WebDriver driver = new ChromeDriver(chromeOptions);
try {
// Navigate to Url
driver.get("https://selenium.dev");
} finally {
driver.quit();
}
}
@Test
public void getBrowserName() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
String name = chromeOptions.getBrowserName();
Assertions.assertFalse(name.isEmpty(), "Browser name should not be empty");
}
@Test
public void setBrowserVersion() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
String version = "latest";
chromeOptions.setBrowserVersion(version);
Assertions.assertEquals(version, chromeOptions.getBrowserVersion());
}
@Test
public void setPlatformName() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
String platform = "OS X 10.6";
chromeOptions.setPlatformName(platform);
Assertions.assertEquals(platform, chromeOptions.getPlatformName().toString());
}
@Test
public void setScriptTimeout() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
Duration duration = Duration.of(5, ChronoUnit.SECONDS);
chromeOptions.setScriptTimeout(duration);
WebDriver driver = new ChromeDriver(chromeOptions);
try {
Duration timeout = driver.manage().timeouts().getScriptTimeout();
Assertions.assertEquals(timeout, duration, "The script timeout should be set to 5 seconds.");
} finally {
driver.quit();
}
}
@Test
public void setPageLoadTimeout() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
Duration duration = Duration.of(5, ChronoUnit.SECONDS);
chromeOptions.setPageLoadTimeout(duration);
WebDriver driver = new ChromeDriver(chromeOptions);
try {
Duration timeout = driver.manage().timeouts().getPageLoadTimeout();
Assertions.assertEquals(timeout, duration, "The page load timeout should be set to 5 seconds.");
} finally {
driver.quit();
}
}
@Test
public void setImplicitWaitTimeout() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
Duration duration = Duration.of(5, ChronoUnit.SECONDS);
chromeOptions.setImplicitWaitTimeout(duration);
WebDriver driver = new ChromeDriver(chromeOptions);
try {
Duration timeout = driver.manage().timeouts().getImplicitWaitTimeout();
Assertions.assertEquals(timeout, duration, "The implicit wait timeout should be set to 5 seconds.");
} finally {
driver.quit();
}
}
@Test
public void setUnhandledPromptBehaviour() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
chromeOptions.setUnhandledPromptBehaviour(UnexpectedAlertBehaviour.DISMISS_AND_NOTIFY);
//verify the capability object is not null
Object capabilityObject = 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());
}
@Test
public void setWindowRect() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
chromeOptions.setCapability(CapabilityType.SET_WINDOW_RECT, true);
//verify the capability object is not null
Object capabilityObject = chromeOptions.getCapability(CapabilityType.SET_WINDOW_RECT);
Assertions.assertNotNull(capabilityObject, "Capability SET_WINDOW_RECT should not be null.");
Boolean capability = (Boolean) capabilityObject;
Assertions.assertTrue(capability, "The capability SET_WINDOW_RECT should be set to true.");
}
@Test
public void setStrictFileInteractability() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
chromeOptions.setCapability(CapabilityType.STRICT_FILE_INTERACTABILITY, true);
//verify the capability object is not null
Object capabilityObject = chromeOptions.getCapability(CapabilityType.STRICT_FILE_INTERACTABILITY);
Assertions.assertNotNull(capabilityObject, "Capability STRICT_FILE_INTERACTABILITY should not be null.");
Boolean capability = (Boolean) capabilityObject;
Assertions.assertTrue(capability, "The capability STRICT_FILE_INTERACTABILITY should be set to true.");
}
}
options = get_default_chrome_options()
options.page_load_strategy = 'eager'
driver = webdriver.Chrome(options=options)
/examples/python/tests/drivers/test_options.py
from selenium import webdriver
from selenium.webdriver.common.proxy import Proxy
from selenium.webdriver.common.proxy import ProxyType
def test_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()
def test_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()
def test_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()
def test_timeouts_script():
options = get_default_chrome_options()
options.timeouts = { 'script': 5000 }
driver = webdriver.Chrome(options=options)
driver.get("https://www.selenium.dev/")
driver.quit()
def test_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()
def test_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()
def test_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()
def test_set_window_rect():
options = webdriver.FirefoxOptions()
options.set_window_rect = True # Full support in Firefox
driver = webdriver.Firefox(options=options)
driver.get("https://www.selenium.dev/")
driver.quit()
def test_strict_file_interactability():
options = get_default_chrome_options()
options.strict_file_interactability = True
driver = webdriver.Chrome(options=options)
driver.get("https://www.selenium.dev/")
driver.quit()
def test_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()
def test_set_browser_name():
options = get_default_chrome_options()
assert options.capabilities['browserName'] == 'chrome'
driver = webdriver.Chrome(options=options)
driver.get("https://www.selenium.dev/")
driver.quit()
def test_set_browser_version():
options = get_default_chrome_options()
options.browser_version = 'stable'
assert options.capabilities['browserVersion'] == 'stable'
driver = webdriver.Chrome(options=options)
driver.get("https://www.selenium.dev/")
driver.quit()
def test_platform_name():
options = get_default_chrome_options()
options.platform_name = 'any'
driver = webdriver.Chrome(options=options)
driver.get("https://www.selenium.dev/")
driver.quit()
def test_accept_insecure_certs():
options = get_default_chrome_options()
options.accept_insecure_certs = True
driver = webdriver.Chrome(options=options)
driver.get("https://www.selenium.dev/")
driver.quit()
def get_default_chrome_options():
options = webdriver.ChromeOptions()
options.add_argument("--no-sandbox")
return options
var chromeOptions = new ChromeOptions();
chromeOptions.PageLoadStrategy = PageLoadStrategy.Eager;
/examples/dotnet/SeleniumDocs/Drivers/OptionsTest.cs
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
namespace SeleniumDocs.Drivers
{
[TestClass]
public class OptionsTest : BaseTest
{
[TestMethod]
public void SetPageLoadStrategyNormal()
{
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.PageLoadStrategy = PageLoadStrategy.Normal;
IWebDriver driver = new ChromeDriver(chromeOptions);
try
{
// Navigate to Url
driver.Navigate().GoToUrl("https://selenium.dev");
}
finally
{
driver.Quit();
}
}
[TestMethod]
public void SetPageLoadStrategyEager()
{
var chromeOptions = new ChromeOptions();
chromeOptions.PageLoadStrategy = PageLoadStrategy.Eager;
IWebDriver driver = new ChromeDriver(chromeOptions);
try
{
driver.Navigate().GoToUrl("https://selenium.dev");
}
finally
{
driver.Quit();
}
}
[TestMethod]
public void SetPageLoadStrategyNone()
{
var chromeOptions = new ChromeOptions();
chromeOptions.PageLoadStrategy = PageLoadStrategy.None;
IWebDriver driver = new ChromeDriver(chromeOptions);
try
{
driver.Navigate().GoToUrl("https://selenium.dev");
}
finally
{
driver.Quit();
}
}
}
}
options = Selenium::WebDriver::Options.chrome
options.page_load_strategy = :eager
/examples/ruby/spec/drivers/options_spec.rb
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe 'Chrome' do
describe 'Driver Options' do
let(:chrome_location) { driver_finder && ENV.fetch('CHROME_BIN', nil) }
let(:url) { 'https://www.selenium.dev/selenium/web/' }
it 'page load strategy normal' do
options = Selenium::WebDriver::Options.chrome
options.page_load_strategy = :normal
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'page load strategy eager' do
options = Selenium::WebDriver::Options.chrome
options.page_load_strategy = :eager
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'page load strategy none' do
options = Selenium::WebDriver::Options.chrome
options.page_load_strategy = :none
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'sets remote capabilities', skip: 'this is example code that will not execute' do
options = Selenium::WebDriver::Options.firefox
options.platform_name = 'Windows 10'
options.browser_version = 'latest'
cloud_options = {}
cloud_options[:build] = my_test_build
cloud_options[:name] = my_test_name
options.add_option('cloud:options', cloud_options)
driver = Selenium::WebDriver.for :remote, capabilities: options
driver.get(url)
driver.quit
end
it 'accepts untrusted certificates' do
options = Selenium::WebDriver::Options.chrome
options.accept_insecure_certs = true
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'sets unhandled prompt behavior' do
options = Selenium::WebDriver::Options.chrome
options.unhandled_prompt_behavior = :accept
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'sets window rect' do
options = Selenium::WebDriver::Options.firefox
options.set_window_rect = true
driver = Selenium::WebDriver.for :firefox, options: options
driver.get(url)
driver.quit
end
it 'sets strict file interactability' do
options = Selenium::WebDriver::Options.chrome
options.strict_file_interactability = true
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'sets the proxy' do
options = Selenium::WebDriver::Options.chrome
options.proxy = Selenium::WebDriver::Proxy.new(http: 'myproxy.com:8080')
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'sets the implicit timeout' do
options = Selenium::WebDriver::Options.chrome
options.timeouts = {implicit: 1}
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'sets the page load timeout' do
options = Selenium::WebDriver::Options.chrome
options.timeouts = {page_load: 400_000}
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'sets the script timeout' do
options = Selenium::WebDriver::Options.chrome
options.timeouts = {script: 40_000}
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'sets capabilities in the pre-selenium 4 way', skip: 'this is example code that will not execute' do
caps = Selenium::WebDriver::Remote::Capabilities.firefox
caps[:platform] = 'Windows 10'
caps[:version] = '92'
caps[:build] = my_test_build
caps[:name] = my_test_name
driver = Selenium::WebDriver.for :remote, url: cloud_url, desired_capabilities: caps
driver.get(url)
driver.quit
end
end
end
let driver = new Builder()
.forBrowser(Browser.CHROME)
.setChromeOptions(options.setPageLoadStrategy('eager'))
.build();
await driver.get('https://www.selenium.dev/selenium/web/blank.html');
await driver.quit();
/examples/javascript/test/capabilities/pageLoading.spec.js
const Chrome = require('selenium-webdriver/chrome');
const {Browser, Builder} = require("selenium-webdriver");
const options = new Chrome.Options()
describe('Page loading strategies', function () {
it('Navigate using eager page loading strategy', async function () {
let driver = new Builder()
.forBrowser(Browser.CHROME)
.setChromeOptions(options.setPageLoadStrategy('eager'))
.build();
await driver.get('https://www.selenium.dev/selenium/web/blank.html');
await driver.quit();
});
it('Navigate using none page loading strategy', async function () {
let driver = new Builder()
.forBrowser(Browser.CHROME)
.setChromeOptions(options.setPageLoadStrategy('none'))
.build();
await driver.get('https://www.selenium.dev/selenium/web/blank.html');
await driver.quit();
});
it('Navigate using normal page loading strategy', async function () {
let driver = new Builder()
.forBrowser(Browser.CHROME)
.setChromeOptions(options.setPageLoadStrategy('normal'))
.build();
await driver.get('https://www.selenium.dev/selenium/web/blank.html');
await driver.quit();
});
it('Should be able to accept certs', async function () {
let driver = new Builder()
.forBrowser(Browser.CHROME)
.setChromeOptions(options.setAcceptInsecureCerts(true))
.build();
await driver.get('https://www.selenium.dev/selenium/web/blank.html');
await driver.quit();
});
});
import org.openqa.selenium.PageLoadStrategy
import org.openqa.selenium.chrome.ChromeDriver
import org.openqa.selenium.chrome.ChromeOptions
fun main() {
val chromeOptions = ChromeOptions()
chromeOptions.setPageLoadStrategy(PageLoadStrategy.EAGER)
val driver = ChromeDriver(chromeOptions)
try {
driver.get("https://www.google.com")
}
finally {
driver.quit()
}
}
none
WebDriver ใฏใๆๅใฎใใผใธใใใฆใณใญใผใใใใใพใงๅพ ๆฉใใพใใ
ChromeOptions chromeOptions = getDefaultChromeOptions();
chromeOptions.setPageLoadStrategy(PageLoadStrategy.NONE);
WebDriver driver = new ChromeDriver(chromeOptions);
/examples/java/src/test/java/dev/selenium/drivers/OptionsTest.java
package dev.selenium.drivers;
import dev.selenium.BaseTest;
import java.time.Duration;
import java.time.temporal.ChronoUnit;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Assertions;
import org.openqa.selenium.PageLoadStrategy;
import org.openqa.selenium.UnexpectedAlertBehaviour;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.chrome.ChromeDriver;
public class OptionsTest extends BaseTest {
@Test
public void setPageLoadStrategyNormal() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
chromeOptions.setPageLoadStrategy(PageLoadStrategy.NORMAL);
WebDriver driver = new ChromeDriver(chromeOptions);
try {
// Navigate to Url
driver.get("https://selenium.dev");
} finally {
driver.quit();
}
}
@Test
public void setPageLoadStrategyEager() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
chromeOptions.setPageLoadStrategy(PageLoadStrategy.EAGER);
WebDriver driver = new ChromeDriver(chromeOptions);
try {
// Navigate to Url
driver.get("https://selenium.dev");
} finally {
driver.quit();
}
}
@Test
public void setPageLoadStrategyNone() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
chromeOptions.setPageLoadStrategy(PageLoadStrategy.NONE);
WebDriver driver = new ChromeDriver(chromeOptions);
try {
// Navigate to Url
driver.get("https://selenium.dev");
} finally {
driver.quit();
}
}
@Test
public void setAcceptInsecureCerts() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
chromeOptions.setAcceptInsecureCerts(true);
WebDriver driver = new ChromeDriver(chromeOptions);
try {
// Navigate to Url
driver.get("https://selenium.dev");
} finally {
driver.quit();
}
}
@Test
public void getBrowserName() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
String name = chromeOptions.getBrowserName();
Assertions.assertFalse(name.isEmpty(), "Browser name should not be empty");
}
@Test
public void setBrowserVersion() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
String version = "latest";
chromeOptions.setBrowserVersion(version);
Assertions.assertEquals(version, chromeOptions.getBrowserVersion());
}
@Test
public void setPlatformName() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
String platform = "OS X 10.6";
chromeOptions.setPlatformName(platform);
Assertions.assertEquals(platform, chromeOptions.getPlatformName().toString());
}
@Test
public void setScriptTimeout() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
Duration duration = Duration.of(5, ChronoUnit.SECONDS);
chromeOptions.setScriptTimeout(duration);
WebDriver driver = new ChromeDriver(chromeOptions);
try {
Duration timeout = driver.manage().timeouts().getScriptTimeout();
Assertions.assertEquals(timeout, duration, "The script timeout should be set to 5 seconds.");
} finally {
driver.quit();
}
}
@Test
public void setPageLoadTimeout() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
Duration duration = Duration.of(5, ChronoUnit.SECONDS);
chromeOptions.setPageLoadTimeout(duration);
WebDriver driver = new ChromeDriver(chromeOptions);
try {
Duration timeout = driver.manage().timeouts().getPageLoadTimeout();
Assertions.assertEquals(timeout, duration, "The page load timeout should be set to 5 seconds.");
} finally {
driver.quit();
}
}
@Test
public void setImplicitWaitTimeout() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
Duration duration = Duration.of(5, ChronoUnit.SECONDS);
chromeOptions.setImplicitWaitTimeout(duration);
WebDriver driver = new ChromeDriver(chromeOptions);
try {
Duration timeout = driver.manage().timeouts().getImplicitWaitTimeout();
Assertions.assertEquals(timeout, duration, "The implicit wait timeout should be set to 5 seconds.");
} finally {
driver.quit();
}
}
@Test
public void setUnhandledPromptBehaviour() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
chromeOptions.setUnhandledPromptBehaviour(UnexpectedAlertBehaviour.DISMISS_AND_NOTIFY);
//verify the capability object is not null
Object capabilityObject = 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());
}
@Test
public void setWindowRect() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
chromeOptions.setCapability(CapabilityType.SET_WINDOW_RECT, true);
//verify the capability object is not null
Object capabilityObject = chromeOptions.getCapability(CapabilityType.SET_WINDOW_RECT);
Assertions.assertNotNull(capabilityObject, "Capability SET_WINDOW_RECT should not be null.");
Boolean capability = (Boolean) capabilityObject;
Assertions.assertTrue(capability, "The capability SET_WINDOW_RECT should be set to true.");
}
@Test
public void setStrictFileInteractability() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
chromeOptions.setCapability(CapabilityType.STRICT_FILE_INTERACTABILITY, true);
//verify the capability object is not null
Object capabilityObject = chromeOptions.getCapability(CapabilityType.STRICT_FILE_INTERACTABILITY);
Assertions.assertNotNull(capabilityObject, "Capability STRICT_FILE_INTERACTABILITY should not be null.");
Boolean capability = (Boolean) capabilityObject;
Assertions.assertTrue(capability, "The capability STRICT_FILE_INTERACTABILITY should be set to true.");
}
}
options = get_default_chrome_options()
options.page_load_strategy = 'none'
driver = webdriver.Chrome(options=options)
/examples/python/tests/drivers/test_options.py
from selenium import webdriver
from selenium.webdriver.common.proxy import Proxy
from selenium.webdriver.common.proxy import ProxyType
def test_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()
def test_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()
def test_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()
def test_timeouts_script():
options = get_default_chrome_options()
options.timeouts = { 'script': 5000 }
driver = webdriver.Chrome(options=options)
driver.get("https://www.selenium.dev/")
driver.quit()
def test_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()
def test_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()
def test_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()
def test_set_window_rect():
options = webdriver.FirefoxOptions()
options.set_window_rect = True # Full support in Firefox
driver = webdriver.Firefox(options=options)
driver.get("https://www.selenium.dev/")
driver.quit()
def test_strict_file_interactability():
options = get_default_chrome_options()
options.strict_file_interactability = True
driver = webdriver.Chrome(options=options)
driver.get("https://www.selenium.dev/")
driver.quit()
def test_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()
def test_set_browser_name():
options = get_default_chrome_options()
assert options.capabilities['browserName'] == 'chrome'
driver = webdriver.Chrome(options=options)
driver.get("https://www.selenium.dev/")
driver.quit()
def test_set_browser_version():
options = get_default_chrome_options()
options.browser_version = 'stable'
assert options.capabilities['browserVersion'] == 'stable'
driver = webdriver.Chrome(options=options)
driver.get("https://www.selenium.dev/")
driver.quit()
def test_platform_name():
options = get_default_chrome_options()
options.platform_name = 'any'
driver = webdriver.Chrome(options=options)
driver.get("https://www.selenium.dev/")
driver.quit()
def test_accept_insecure_certs():
options = get_default_chrome_options()
options.accept_insecure_certs = True
driver = webdriver.Chrome(options=options)
driver.get("https://www.selenium.dev/")
driver.quit()
def get_default_chrome_options():
options = webdriver.ChromeOptions()
options.add_argument("--no-sandbox")
return options
[TestMethod]
public void SetPageLoadStrategyNone()
/examples/dotnet/SeleniumDocs/Drivers/OptionsTest.cs
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
namespace SeleniumDocs.Drivers
{
[TestClass]
public class OptionsTest : BaseTest
{
[TestMethod]
public void SetPageLoadStrategyNormal()
{
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.PageLoadStrategy = PageLoadStrategy.Normal;
IWebDriver driver = new ChromeDriver(chromeOptions);
try
{
// Navigate to Url
driver.Navigate().GoToUrl("https://selenium.dev");
}
finally
{
driver.Quit();
}
}
[TestMethod]
public void SetPageLoadStrategyEager()
{
var chromeOptions = new ChromeOptions();
chromeOptions.PageLoadStrategy = PageLoadStrategy.Eager;
IWebDriver driver = new ChromeDriver(chromeOptions);
try
{
driver.Navigate().GoToUrl("https://selenium.dev");
}
finally
{
driver.Quit();
}
}
[TestMethod]
public void SetPageLoadStrategyNone()
{
var chromeOptions = new ChromeOptions();
chromeOptions.PageLoadStrategy = PageLoadStrategy.None;
IWebDriver driver = new ChromeDriver(chromeOptions);
try
{
driver.Navigate().GoToUrl("https://selenium.dev");
}
finally
{
driver.Quit();
}
}
}
}
options = Selenium::WebDriver::Options.chrome
options.page_load_strategy = :none
/examples/ruby/spec/drivers/options_spec.rb
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe 'Chrome' do
describe 'Driver Options' do
let(:chrome_location) { driver_finder && ENV.fetch('CHROME_BIN', nil) }
let(:url) { 'https://www.selenium.dev/selenium/web/' }
it 'page load strategy normal' do
options = Selenium::WebDriver::Options.chrome
options.page_load_strategy = :normal
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'page load strategy eager' do
options = Selenium::WebDriver::Options.chrome
options.page_load_strategy = :eager
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'page load strategy none' do
options = Selenium::WebDriver::Options.chrome
options.page_load_strategy = :none
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'sets remote capabilities', skip: 'this is example code that will not execute' do
options = Selenium::WebDriver::Options.firefox
options.platform_name = 'Windows 10'
options.browser_version = 'latest'
cloud_options = {}
cloud_options[:build] = my_test_build
cloud_options[:name] = my_test_name
options.add_option('cloud:options', cloud_options)
driver = Selenium::WebDriver.for :remote, capabilities: options
driver.get(url)
driver.quit
end
it 'accepts untrusted certificates' do
options = Selenium::WebDriver::Options.chrome
options.accept_insecure_certs = true
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'sets unhandled prompt behavior' do
options = Selenium::WebDriver::Options.chrome
options.unhandled_prompt_behavior = :accept
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'sets window rect' do
options = Selenium::WebDriver::Options.firefox
options.set_window_rect = true
driver = Selenium::WebDriver.for :firefox, options: options
driver.get(url)
driver.quit
end
it 'sets strict file interactability' do
options = Selenium::WebDriver::Options.chrome
options.strict_file_interactability = true
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'sets the proxy' do
options = Selenium::WebDriver::Options.chrome
options.proxy = Selenium::WebDriver::Proxy.new(http: 'myproxy.com:8080')
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'sets the implicit timeout' do
options = Selenium::WebDriver::Options.chrome
options.timeouts = {implicit: 1}
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'sets the page load timeout' do
options = Selenium::WebDriver::Options.chrome
options.timeouts = {page_load: 400_000}
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'sets the script timeout' do
options = Selenium::WebDriver::Options.chrome
options.timeouts = {script: 40_000}
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'sets capabilities in the pre-selenium 4 way', skip: 'this is example code that will not execute' do
caps = Selenium::WebDriver::Remote::Capabilities.firefox
caps[:platform] = 'Windows 10'
caps[:version] = '92'
caps[:build] = my_test_build
caps[:name] = my_test_name
driver = Selenium::WebDriver.for :remote, url: cloud_url, desired_capabilities: caps
driver.get(url)
driver.quit
end
end
end
let driver = new Builder()
.forBrowser(Browser.CHROME)
.setChromeOptions(options.setPageLoadStrategy('none'))
.build();
await driver.get('https://www.selenium.dev/selenium/web/blank.html');
await driver.quit();
/examples/javascript/test/capabilities/pageLoading.spec.js
const Chrome = require('selenium-webdriver/chrome');
const {Browser, Builder} = require("selenium-webdriver");
const options = new Chrome.Options()
describe('Page loading strategies', function () {
it('Navigate using eager page loading strategy', async function () {
let driver = new Builder()
.forBrowser(Browser.CHROME)
.setChromeOptions(options.setPageLoadStrategy('eager'))
.build();
await driver.get('https://www.selenium.dev/selenium/web/blank.html');
await driver.quit();
});
it('Navigate using none page loading strategy', async function () {
let driver = new Builder()
.forBrowser(Browser.CHROME)
.setChromeOptions(options.setPageLoadStrategy('none'))
.build();
await driver.get('https://www.selenium.dev/selenium/web/blank.html');
await driver.quit();
});
it('Navigate using normal page loading strategy', async function () {
let driver = new Builder()
.forBrowser(Browser.CHROME)
.setChromeOptions(options.setPageLoadStrategy('normal'))
.build();
await driver.get('https://www.selenium.dev/selenium/web/blank.html');
await driver.quit();
});
it('Should be able to accept certs', async function () {
let driver = new Builder()
.forBrowser(Browser.CHROME)
.setChromeOptions(options.setAcceptInsecureCerts(true))
.build();
await driver.get('https://www.selenium.dev/selenium/web/blank.html');
await driver.quit();
});
});
import org.openqa.selenium.PageLoadStrategy
import org.openqa.selenium.chrome.ChromeDriver
import org.openqa.selenium.chrome.ChromeOptions
fun main() {
val chromeOptions = ChromeOptions()
chromeOptions.setPageLoadStrategy(PageLoadStrategy.NONE)
val driver = ChromeDriver(chromeOptions)
try {
driver.get("https://www.google.com")
}
finally {
driver.quit()
}
}
platformName
ใใใซใใใใชใขใผใใจใณใใฎใชใใฌใผใใฃใณใฐใทในใใ ใ่ญๅฅใใใ platformName
ใๅๅพใใใจOSๅใ่ฟใใใพใใ
ใฏใฉใฆใใใผในใฎใใญใใคใใผใงใฏใ platformName
ใ่จญๅฎใใใจใใชใขใผใใจใณใใฎOSใ่จญๅฎใใใพใใ
ChromeOptions chromeOptions = getDefaultChromeOptions();
String platform = "OS X 10.6";
chromeOptions.setPlatformName(platform);
/examples/java/src/test/java/dev/selenium/drivers/OptionsTest.java
package dev.selenium.drivers;
import dev.selenium.BaseTest;
import java.time.Duration;
import java.time.temporal.ChronoUnit;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Assertions;
import org.openqa.selenium.PageLoadStrategy;
import org.openqa.selenium.UnexpectedAlertBehaviour;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.chrome.ChromeDriver;
public class OptionsTest extends BaseTest {
@Test
public void setPageLoadStrategyNormal() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
chromeOptions.setPageLoadStrategy(PageLoadStrategy.NORMAL);
WebDriver driver = new ChromeDriver(chromeOptions);
try {
// Navigate to Url
driver.get("https://selenium.dev");
} finally {
driver.quit();
}
}
@Test
public void setPageLoadStrategyEager() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
chromeOptions.setPageLoadStrategy(PageLoadStrategy.EAGER);
WebDriver driver = new ChromeDriver(chromeOptions);
try {
// Navigate to Url
driver.get("https://selenium.dev");
} finally {
driver.quit();
}
}
@Test
public void setPageLoadStrategyNone() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
chromeOptions.setPageLoadStrategy(PageLoadStrategy.NONE);
WebDriver driver = new ChromeDriver(chromeOptions);
try {
// Navigate to Url
driver.get("https://selenium.dev");
} finally {
driver.quit();
}
}
@Test
public void setAcceptInsecureCerts() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
chromeOptions.setAcceptInsecureCerts(true);
WebDriver driver = new ChromeDriver(chromeOptions);
try {
// Navigate to Url
driver.get("https://selenium.dev");
} finally {
driver.quit();
}
}
@Test
public void getBrowserName() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
String name = chromeOptions.getBrowserName();
Assertions.assertFalse(name.isEmpty(), "Browser name should not be empty");
}
@Test
public void setBrowserVersion() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
String version = "latest";
chromeOptions.setBrowserVersion(version);
Assertions.assertEquals(version, chromeOptions.getBrowserVersion());
}
@Test
public void setPlatformName() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
String platform = "OS X 10.6";
chromeOptions.setPlatformName(platform);
Assertions.assertEquals(platform, chromeOptions.getPlatformName().toString());
}
@Test
public void setScriptTimeout() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
Duration duration = Duration.of(5, ChronoUnit.SECONDS);
chromeOptions.setScriptTimeout(duration);
WebDriver driver = new ChromeDriver(chromeOptions);
try {
Duration timeout = driver.manage().timeouts().getScriptTimeout();
Assertions.assertEquals(timeout, duration, "The script timeout should be set to 5 seconds.");
} finally {
driver.quit();
}
}
@Test
public void setPageLoadTimeout() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
Duration duration = Duration.of(5, ChronoUnit.SECONDS);
chromeOptions.setPageLoadTimeout(duration);
WebDriver driver = new ChromeDriver(chromeOptions);
try {
Duration timeout = driver.manage().timeouts().getPageLoadTimeout();
Assertions.assertEquals(timeout, duration, "The page load timeout should be set to 5 seconds.");
} finally {
driver.quit();
}
}
@Test
public void setImplicitWaitTimeout() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
Duration duration = Duration.of(5, ChronoUnit.SECONDS);
chromeOptions.setImplicitWaitTimeout(duration);
WebDriver driver = new ChromeDriver(chromeOptions);
try {
Duration timeout = driver.manage().timeouts().getImplicitWaitTimeout();
Assertions.assertEquals(timeout, duration, "The implicit wait timeout should be set to 5 seconds.");
} finally {
driver.quit();
}
}
@Test
public void setUnhandledPromptBehaviour() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
chromeOptions.setUnhandledPromptBehaviour(UnexpectedAlertBehaviour.DISMISS_AND_NOTIFY);
//verify the capability object is not null
Object capabilityObject = 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());
}
@Test
public void setWindowRect() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
chromeOptions.setCapability(CapabilityType.SET_WINDOW_RECT, true);
//verify the capability object is not null
Object capabilityObject = chromeOptions.getCapability(CapabilityType.SET_WINDOW_RECT);
Assertions.assertNotNull(capabilityObject, "Capability SET_WINDOW_RECT should not be null.");
Boolean capability = (Boolean) capabilityObject;
Assertions.assertTrue(capability, "The capability SET_WINDOW_RECT should be set to true.");
}
@Test
public void setStrictFileInteractability() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
chromeOptions.setCapability(CapabilityType.STRICT_FILE_INTERACTABILITY, true);
//verify the capability object is not null
Object capabilityObject = chromeOptions.getCapability(CapabilityType.STRICT_FILE_INTERACTABILITY);
Assertions.assertNotNull(capabilityObject, "Capability STRICT_FILE_INTERACTABILITY should not be null.");
Boolean capability = (Boolean) capabilityObject;
Assertions.assertTrue(capability, "The capability STRICT_FILE_INTERACTABILITY should be set to true.");
}
}
options = get_default_chrome_options()
options.platform_name = 'any'
driver = webdriver.Chrome(options=options)
/examples/python/tests/drivers/test_options.py
from selenium import webdriver
from selenium.webdriver.common.proxy import Proxy
from selenium.webdriver.common.proxy import ProxyType
def test_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()
def test_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()
def test_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()
def test_timeouts_script():
options = get_default_chrome_options()
options.timeouts = { 'script': 5000 }
driver = webdriver.Chrome(options=options)
driver.get("https://www.selenium.dev/")
driver.quit()
def test_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()
def test_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()
def test_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()
def test_set_window_rect():
options = webdriver.FirefoxOptions()
options.set_window_rect = True # Full support in Firefox
driver = webdriver.Firefox(options=options)
driver.get("https://www.selenium.dev/")
driver.quit()
def test_strict_file_interactability():
options = get_default_chrome_options()
options.strict_file_interactability = True
driver = webdriver.Chrome(options=options)
driver.get("https://www.selenium.dev/")
driver.quit()
def test_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()
def test_set_browser_name():
options = get_default_chrome_options()
assert options.capabilities['browserName'] == 'chrome'
driver = webdriver.Chrome(options=options)
driver.get("https://www.selenium.dev/")
driver.quit()
def test_set_browser_version():
options = get_default_chrome_options()
options.browser_version = 'stable'
assert options.capabilities['browserVersion'] == 'stable'
driver = webdriver.Chrome(options=options)
driver.get("https://www.selenium.dev/")
driver.quit()
def test_platform_name():
options = get_default_chrome_options()
options.platform_name = 'any'
driver = webdriver.Chrome(options=options)
driver.get("https://www.selenium.dev/")
driver.quit()
def test_accept_insecure_certs():
options = get_default_chrome_options()
options.accept_insecure_certs = True
driver = webdriver.Chrome(options=options)
driver.get("https://www.selenium.dev/")
driver.quit()
def get_default_chrome_options():
options = webdriver.ChromeOptions()
options.add_argument("--no-sandbox")
return options
options = Selenium::WebDriver::Options.firefox
options.platform_name = 'Windows 10'
/examples/ruby/spec/drivers/options_spec.rb
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe 'Chrome' do
describe 'Driver Options' do
let(:chrome_location) { driver_finder && ENV.fetch('CHROME_BIN', nil) }
let(:url) { 'https://www.selenium.dev/selenium/web/' }
it 'page load strategy normal' do
options = Selenium::WebDriver::Options.chrome
options.page_load_strategy = :normal
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'page load strategy eager' do
options = Selenium::WebDriver::Options.chrome
options.page_load_strategy = :eager
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'page load strategy none' do
options = Selenium::WebDriver::Options.chrome
options.page_load_strategy = :none
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'sets remote capabilities', skip: 'this is example code that will not execute' do
options = Selenium::WebDriver::Options.firefox
options.platform_name = 'Windows 10'
options.browser_version = 'latest'
cloud_options = {}
cloud_options[:build] = my_test_build
cloud_options[:name] = my_test_name
options.add_option('cloud:options', cloud_options)
driver = Selenium::WebDriver.for :remote, capabilities: options
driver.get(url)
driver.quit
end
it 'accepts untrusted certificates' do
options = Selenium::WebDriver::Options.chrome
options.accept_insecure_certs = true
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'sets unhandled prompt behavior' do
options = Selenium::WebDriver::Options.chrome
options.unhandled_prompt_behavior = :accept
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'sets window rect' do
options = Selenium::WebDriver::Options.firefox
options.set_window_rect = true
driver = Selenium::WebDriver.for :firefox, options: options
driver.get(url)
driver.quit
end
it 'sets strict file interactability' do
options = Selenium::WebDriver::Options.chrome
options.strict_file_interactability = true
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'sets the proxy' do
options = Selenium::WebDriver::Options.chrome
options.proxy = Selenium::WebDriver::Proxy.new(http: 'myproxy.com:8080')
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'sets the implicit timeout' do
options = Selenium::WebDriver::Options.chrome
options.timeouts = {implicit: 1}
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'sets the page load timeout' do
options = Selenium::WebDriver::Options.chrome
options.timeouts = {page_load: 400_000}
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'sets the script timeout' do
options = Selenium::WebDriver::Options.chrome
options.timeouts = {script: 40_000}
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'sets capabilities in the pre-selenium 4 way', skip: 'this is example code that will not execute' do
caps = Selenium::WebDriver::Remote::Capabilities.firefox
caps[:platform] = 'Windows 10'
caps[:version] = '92'
caps[:build] = my_test_build
caps[:name] = my_test_name
driver = Selenium::WebDriver.for :remote, url: cloud_url, desired_capabilities: caps
driver.get(url)
driver.quit
end
end
end
acceptInsecureCerts
ใใฎๆฉ่ฝใฏใใปใใทใงใณไธญใฎใใใฒใผใทใงใณไธญใซใๆ้ๅใ๏ผใพใใฏ๏ผ็กๅนใช TLS่จผๆๆธ
ใไฝฟ็จใใใฆใใใใฉใใใ็ขบ่ชใใพใใ
ๆฉ่ฝใ false
ใซ่จญๅฎใใใฆใใๅ ดๅใใใใฒใผใทใงใณใงใใกใคใณ่จผๆๆธใฎๅ้กใ็บ็ใใใจใ
insecure certificate error ใ่ฟใใใพใใ
true
ใซ่จญๅฎใใใจใ็กๅนใช่จผๆๆธใฏใใฉใฆใถใผใซใใฃใฆไฟก้ ผใใใพใใ
ใในใฆใฎ่ชๅทฑ็ฝฒๅ่จผๆๆธใฏใใใใฉใซใใงใใฎๆฉ่ฝใซใใฃใฆไฟก้ ผใใใพใใ
ไธๅบฆ่จญๅฎใใใจใ acceptInsecureCerts
Capabilityใฏใปใใทใงใณๅ
จไฝใซๅฝฑ้ฟใใพใใ
ChromeOptions chromeOptions = getDefaultChromeOptions();
chromeOptions.setAcceptInsecureCerts(true);
/examples/java/src/test/java/dev/selenium/drivers/OptionsTest.java
package dev.selenium.drivers;
import dev.selenium.BaseTest;
import java.time.Duration;
import java.time.temporal.ChronoUnit;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Assertions;
import org.openqa.selenium.PageLoadStrategy;
import org.openqa.selenium.UnexpectedAlertBehaviour;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.chrome.ChromeDriver;
public class OptionsTest extends BaseTest {
@Test
public void setPageLoadStrategyNormal() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
chromeOptions.setPageLoadStrategy(PageLoadStrategy.NORMAL);
WebDriver driver = new ChromeDriver(chromeOptions);
try {
// Navigate to Url
driver.get("https://selenium.dev");
} finally {
driver.quit();
}
}
@Test
public void setPageLoadStrategyEager() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
chromeOptions.setPageLoadStrategy(PageLoadStrategy.EAGER);
WebDriver driver = new ChromeDriver(chromeOptions);
try {
// Navigate to Url
driver.get("https://selenium.dev");
} finally {
driver.quit();
}
}
@Test
public void setPageLoadStrategyNone() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
chromeOptions.setPageLoadStrategy(PageLoadStrategy.NONE);
WebDriver driver = new ChromeDriver(chromeOptions);
try {
// Navigate to Url
driver.get("https://selenium.dev");
} finally {
driver.quit();
}
}
@Test
public void setAcceptInsecureCerts() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
chromeOptions.setAcceptInsecureCerts(true);
WebDriver driver = new ChromeDriver(chromeOptions);
try {
// Navigate to Url
driver.get("https://selenium.dev");
} finally {
driver.quit();
}
}
@Test
public void getBrowserName() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
String name = chromeOptions.getBrowserName();
Assertions.assertFalse(name.isEmpty(), "Browser name should not be empty");
}
@Test
public void setBrowserVersion() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
String version = "latest";
chromeOptions.setBrowserVersion(version);
Assertions.assertEquals(version, chromeOptions.getBrowserVersion());
}
@Test
public void setPlatformName() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
String platform = "OS X 10.6";
chromeOptions.setPlatformName(platform);
Assertions.assertEquals(platform, chromeOptions.getPlatformName().toString());
}
@Test
public void setScriptTimeout() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
Duration duration = Duration.of(5, ChronoUnit.SECONDS);
chromeOptions.setScriptTimeout(duration);
WebDriver driver = new ChromeDriver(chromeOptions);
try {
Duration timeout = driver.manage().timeouts().getScriptTimeout();
Assertions.assertEquals(timeout, duration, "The script timeout should be set to 5 seconds.");
} finally {
driver.quit();
}
}
@Test
public void setPageLoadTimeout() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
Duration duration = Duration.of(5, ChronoUnit.SECONDS);
chromeOptions.setPageLoadTimeout(duration);
WebDriver driver = new ChromeDriver(chromeOptions);
try {
Duration timeout = driver.manage().timeouts().getPageLoadTimeout();
Assertions.assertEquals(timeout, duration, "The page load timeout should be set to 5 seconds.");
} finally {
driver.quit();
}
}
@Test
public void setImplicitWaitTimeout() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
Duration duration = Duration.of(5, ChronoUnit.SECONDS);
chromeOptions.setImplicitWaitTimeout(duration);
WebDriver driver = new ChromeDriver(chromeOptions);
try {
Duration timeout = driver.manage().timeouts().getImplicitWaitTimeout();
Assertions.assertEquals(timeout, duration, "The implicit wait timeout should be set to 5 seconds.");
} finally {
driver.quit();
}
}
@Test
public void setUnhandledPromptBehaviour() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
chromeOptions.setUnhandledPromptBehaviour(UnexpectedAlertBehaviour.DISMISS_AND_NOTIFY);
//verify the capability object is not null
Object capabilityObject = 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());
}
@Test
public void setWindowRect() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
chromeOptions.setCapability(CapabilityType.SET_WINDOW_RECT, true);
//verify the capability object is not null
Object capabilityObject = chromeOptions.getCapability(CapabilityType.SET_WINDOW_RECT);
Assertions.assertNotNull(capabilityObject, "Capability SET_WINDOW_RECT should not be null.");
Boolean capability = (Boolean) capabilityObject;
Assertions.assertTrue(capability, "The capability SET_WINDOW_RECT should be set to true.");
}
@Test
public void setStrictFileInteractability() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
chromeOptions.setCapability(CapabilityType.STRICT_FILE_INTERACTABILITY, true);
//verify the capability object is not null
Object capabilityObject = chromeOptions.getCapability(CapabilityType.STRICT_FILE_INTERACTABILITY);
Assertions.assertNotNull(capabilityObject, "Capability STRICT_FILE_INTERACTABILITY should not be null.");
Boolean capability = (Boolean) capabilityObject;
Assertions.assertTrue(capability, "The capability STRICT_FILE_INTERACTABILITY should be set to true.");
}
}
options = get_default_chrome_options()
options.accept_insecure_certs = True
driver = webdriver.Chrome(options=options)
/examples/python/tests/drivers/test_options.py
from selenium import webdriver
from selenium.webdriver.common.proxy import Proxy
from selenium.webdriver.common.proxy import ProxyType
def test_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()
def test_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()
def test_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()
def test_timeouts_script():
options = get_default_chrome_options()
options.timeouts = { 'script': 5000 }
driver = webdriver.Chrome(options=options)
driver.get("https://www.selenium.dev/")
driver.quit()
def test_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()
def test_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()
def test_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()
def test_set_window_rect():
options = webdriver.FirefoxOptions()
options.set_window_rect = True # Full support in Firefox
driver = webdriver.Firefox(options=options)
driver.get("https://www.selenium.dev/")
driver.quit()
def test_strict_file_interactability():
options = get_default_chrome_options()
options.strict_file_interactability = True
driver = webdriver.Chrome(options=options)
driver.get("https://www.selenium.dev/")
driver.quit()
def test_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()
def test_set_browser_name():
options = get_default_chrome_options()
assert options.capabilities['browserName'] == 'chrome'
driver = webdriver.Chrome(options=options)
driver.get("https://www.selenium.dev/")
driver.quit()
def test_set_browser_version():
options = get_default_chrome_options()
options.browser_version = 'stable'
assert options.capabilities['browserVersion'] == 'stable'
driver = webdriver.Chrome(options=options)
driver.get("https://www.selenium.dev/")
driver.quit()
def test_platform_name():
options = get_default_chrome_options()
options.platform_name = 'any'
driver = webdriver.Chrome(options=options)
driver.get("https://www.selenium.dev/")
driver.quit()
def test_accept_insecure_certs():
options = get_default_chrome_options()
options.accept_insecure_certs = True
driver = webdriver.Chrome(options=options)
driver.get("https://www.selenium.dev/")
driver.quit()
def get_default_chrome_options():
options = webdriver.ChromeOptions()
options.add_argument("--no-sandbox")
return options
options = Selenium::WebDriver::Options.chrome
options.accept_insecure_certs = true
/examples/ruby/spec/drivers/options_spec.rb
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe 'Chrome' do
describe 'Driver Options' do
let(:chrome_location) { driver_finder && ENV.fetch('CHROME_BIN', nil) }
let(:url) { 'https://www.selenium.dev/selenium/web/' }
it 'page load strategy normal' do
options = Selenium::WebDriver::Options.chrome
options.page_load_strategy = :normal
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'page load strategy eager' do
options = Selenium::WebDriver::Options.chrome
options.page_load_strategy = :eager
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'page load strategy none' do
options = Selenium::WebDriver::Options.chrome
options.page_load_strategy = :none
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'sets remote capabilities', skip: 'this is example code that will not execute' do
options = Selenium::WebDriver::Options.firefox
options.platform_name = 'Windows 10'
options.browser_version = 'latest'
cloud_options = {}
cloud_options[:build] = my_test_build
cloud_options[:name] = my_test_name
options.add_option('cloud:options', cloud_options)
driver = Selenium::WebDriver.for :remote, capabilities: options
driver.get(url)
driver.quit
end
it 'accepts untrusted certificates' do
options = Selenium::WebDriver::Options.chrome
options.accept_insecure_certs = true
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'sets unhandled prompt behavior' do
options = Selenium::WebDriver::Options.chrome
options.unhandled_prompt_behavior = :accept
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'sets window rect' do
options = Selenium::WebDriver::Options.firefox
options.set_window_rect = true
driver = Selenium::WebDriver.for :firefox, options: options
driver.get(url)
driver.quit
end
it 'sets strict file interactability' do
options = Selenium::WebDriver::Options.chrome
options.strict_file_interactability = true
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'sets the proxy' do
options = Selenium::WebDriver::Options.chrome
options.proxy = Selenium::WebDriver::Proxy.new(http: 'myproxy.com:8080')
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'sets the implicit timeout' do
options = Selenium::WebDriver::Options.chrome
options.timeouts = {implicit: 1}
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'sets the page load timeout' do
options = Selenium::WebDriver::Options.chrome
options.timeouts = {page_load: 400_000}
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'sets the script timeout' do
options = Selenium::WebDriver::Options.chrome
options.timeouts = {script: 40_000}
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'sets capabilities in the pre-selenium 4 way', skip: 'this is example code that will not execute' do
caps = Selenium::WebDriver::Remote::Capabilities.firefox
caps[:platform] = 'Windows 10'
caps[:version] = '92'
caps[:build] = my_test_build
caps[:name] = my_test_name
driver = Selenium::WebDriver.for :remote, url: cloud_url, desired_capabilities: caps
driver.get(url)
driver.quit
end
end
end
let driver = new Builder()
.forBrowser(Browser.CHROME)
.setChromeOptions(options.setAcceptInsecureCerts(true))
.build();
/examples/javascript/test/capabilities/pageLoading.spec.js
const Chrome = require('selenium-webdriver/chrome');
const {Browser, Builder} = require("selenium-webdriver");
const options = new Chrome.Options()
describe('Page loading strategies', function () {
it('Navigate using eager page loading strategy', async function () {
let driver = new Builder()
.forBrowser(Browser.CHROME)
.setChromeOptions(options.setPageLoadStrategy('eager'))
.build();
await driver.get('https://www.selenium.dev/selenium/web/blank.html');
await driver.quit();
});
it('Navigate using none page loading strategy', async function () {
let driver = new Builder()
.forBrowser(Browser.CHROME)
.setChromeOptions(options.setPageLoadStrategy('none'))
.build();
await driver.get('https://www.selenium.dev/selenium/web/blank.html');
await driver.quit();
});
it('Navigate using normal page loading strategy', async function () {
let driver = new Builder()
.forBrowser(Browser.CHROME)
.setChromeOptions(options.setPageLoadStrategy('normal'))
.build();
await driver.get('https://www.selenium.dev/selenium/web/blank.html');
await driver.quit();
});
it('Should be able to accept certs', async function () {
let driver = new Builder()
.forBrowser(Browser.CHROME)
.setChromeOptions(options.setAcceptInsecureCerts(true))
.build();
await driver.get('https://www.selenium.dev/selenium/web/blank.html');
await driver.quit();
});
});
timeouts
WebDriverใฎ ใปใใทใงใณ
ใซใฏ็นๅฎใฎ ใปใใทใงใณใฟใคใ ใขใฆใ
้้ใ่จญๅฎใใใฆใใใ
ใใฎ้ใใฆใผใถใผใฏในใฏใชใใใฎๅฎ่กใพใใฏใใฉใฆใถใผใใใฎๆ
ๅ ฑใฎๅๅพใฎๅไฝใๅถๅพกใงใใพใใ
ๅใปใใทใงใณใฟใคใ ใขใฆใใฏใไปฅไธใง่ชฌๆใใใใใซใ็ฐใชใ ใฟใคใ ใขใฆใ
ใฎ็ตใฟๅใใใงๆงๆใใใพใใ
Script Timeout:
็พๅจใฎใใฉใฆใธใณใฐใณใณใใญในใใงๅฎ่กไธญใฎในใฏใชใใใใใคไธญๆญใใใใๆๅฎใใพใใ ๆฐใใใปใใทใงใณใWebDriverใซใใฃใฆไฝๆใใใใจใใใใฉใซใใฎใฟใคใ ใขใฆใ 30,000 ใ่ชฒใใใพใใ
ChromeOptions chromeOptions = getDefaultChromeOptions();
Duration duration = Duration.of(5, ChronoUnit.SECONDS);
chromeOptions.setScriptTimeout(duration);
/examples/java/src/test/java/dev/selenium/drivers/OptionsTest.java
package dev.selenium.drivers;
import dev.selenium.BaseTest;
import java.time.Duration;
import java.time.temporal.ChronoUnit;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Assertions;
import org.openqa.selenium.PageLoadStrategy;
import org.openqa.selenium.UnexpectedAlertBehaviour;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.chrome.ChromeDriver;
public class OptionsTest extends BaseTest {
@Test
public void setPageLoadStrategyNormal() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
chromeOptions.setPageLoadStrategy(PageLoadStrategy.NORMAL);
WebDriver driver = new ChromeDriver(chromeOptions);
try {
// Navigate to Url
driver.get("https://selenium.dev");
} finally {
driver.quit();
}
}
@Test
public void setPageLoadStrategyEager() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
chromeOptions.setPageLoadStrategy(PageLoadStrategy.EAGER);
WebDriver driver = new ChromeDriver(chromeOptions);
try {
// Navigate to Url
driver.get("https://selenium.dev");
} finally {
driver.quit();
}
}
@Test
public void setPageLoadStrategyNone() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
chromeOptions.setPageLoadStrategy(PageLoadStrategy.NONE);
WebDriver driver = new ChromeDriver(chromeOptions);
try {
// Navigate to Url
driver.get("https://selenium.dev");
} finally {
driver.quit();
}
}
@Test
public void setAcceptInsecureCerts() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
chromeOptions.setAcceptInsecureCerts(true);
WebDriver driver = new ChromeDriver(chromeOptions);
try {
// Navigate to Url
driver.get("https://selenium.dev");
} finally {
driver.quit();
}
}
@Test
public void getBrowserName() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
String name = chromeOptions.getBrowserName();
Assertions.assertFalse(name.isEmpty(), "Browser name should not be empty");
}
@Test
public void setBrowserVersion() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
String version = "latest";
chromeOptions.setBrowserVersion(version);
Assertions.assertEquals(version, chromeOptions.getBrowserVersion());
}
@Test
public void setPlatformName() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
String platform = "OS X 10.6";
chromeOptions.setPlatformName(platform);
Assertions.assertEquals(platform, chromeOptions.getPlatformName().toString());
}
@Test
public void setScriptTimeout() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
Duration duration = Duration.of(5, ChronoUnit.SECONDS);
chromeOptions.setScriptTimeout(duration);
WebDriver driver = new ChromeDriver(chromeOptions);
try {
Duration timeout = driver.manage().timeouts().getScriptTimeout();
Assertions.assertEquals(timeout, duration, "The script timeout should be set to 5 seconds.");
} finally {
driver.quit();
}
}
@Test
public void setPageLoadTimeout() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
Duration duration = Duration.of(5, ChronoUnit.SECONDS);
chromeOptions.setPageLoadTimeout(duration);
WebDriver driver = new ChromeDriver(chromeOptions);
try {
Duration timeout = driver.manage().timeouts().getPageLoadTimeout();
Assertions.assertEquals(timeout, duration, "The page load timeout should be set to 5 seconds.");
} finally {
driver.quit();
}
}
@Test
public void setImplicitWaitTimeout() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
Duration duration = Duration.of(5, ChronoUnit.SECONDS);
chromeOptions.setImplicitWaitTimeout(duration);
WebDriver driver = new ChromeDriver(chromeOptions);
try {
Duration timeout = driver.manage().timeouts().getImplicitWaitTimeout();
Assertions.assertEquals(timeout, duration, "The implicit wait timeout should be set to 5 seconds.");
} finally {
driver.quit();
}
}
@Test
public void setUnhandledPromptBehaviour() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
chromeOptions.setUnhandledPromptBehaviour(UnexpectedAlertBehaviour.DISMISS_AND_NOTIFY);
//verify the capability object is not null
Object capabilityObject = 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());
}
@Test
public void setWindowRect() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
chromeOptions.setCapability(CapabilityType.SET_WINDOW_RECT, true);
//verify the capability object is not null
Object capabilityObject = chromeOptions.getCapability(CapabilityType.SET_WINDOW_RECT);
Assertions.assertNotNull(capabilityObject, "Capability SET_WINDOW_RECT should not be null.");
Boolean capability = (Boolean) capabilityObject;
Assertions.assertTrue(capability, "The capability SET_WINDOW_RECT should be set to true.");
}
@Test
public void setStrictFileInteractability() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
chromeOptions.setCapability(CapabilityType.STRICT_FILE_INTERACTABILITY, true);
//verify the capability object is not null
Object capabilityObject = chromeOptions.getCapability(CapabilityType.STRICT_FILE_INTERACTABILITY);
Assertions.assertNotNull(capabilityObject, "Capability STRICT_FILE_INTERACTABILITY should not be null.");
Boolean capability = (Boolean) capabilityObject;
Assertions.assertTrue(capability, "The capability STRICT_FILE_INTERACTABILITY should be set to true.");
}
}
options = get_default_chrome_options()
options.timeouts = { 'script': 5000 }
driver = webdriver.Chrome(options=options)
/examples/python/tests/drivers/test_options.py
from selenium import webdriver
from selenium.webdriver.common.proxy import Proxy
from selenium.webdriver.common.proxy import ProxyType
def test_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()
def test_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()
def test_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()
def test_timeouts_script():
options = get_default_chrome_options()
options.timeouts = { 'script': 5000 }
driver = webdriver.Chrome(options=options)
driver.get("https://www.selenium.dev/")
driver.quit()
def test_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()
def test_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()
def test_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()
def test_set_window_rect():
options = webdriver.FirefoxOptions()
options.set_window_rect = True # Full support in Firefox
driver = webdriver.Firefox(options=options)
driver.get("https://www.selenium.dev/")
driver.quit()
def test_strict_file_interactability():
options = get_default_chrome_options()
options.strict_file_interactability = True
driver = webdriver.Chrome(options=options)
driver.get("https://www.selenium.dev/")
driver.quit()
def test_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()
def test_set_browser_name():
options = get_default_chrome_options()
assert options.capabilities['browserName'] == 'chrome'
driver = webdriver.Chrome(options=options)
driver.get("https://www.selenium.dev/")
driver.quit()
def test_set_browser_version():
options = get_default_chrome_options()
options.browser_version = 'stable'
assert options.capabilities['browserVersion'] == 'stable'
driver = webdriver.Chrome(options=options)
driver.get("https://www.selenium.dev/")
driver.quit()
def test_platform_name():
options = get_default_chrome_options()
options.platform_name = 'any'
driver = webdriver.Chrome(options=options)
driver.get("https://www.selenium.dev/")
driver.quit()
def test_accept_insecure_certs():
options = get_default_chrome_options()
options.accept_insecure_certs = True
driver = webdriver.Chrome(options=options)
driver.get("https://www.selenium.dev/")
driver.quit()
def get_default_chrome_options():
options = webdriver.ChromeOptions()
options.add_argument("--no-sandbox")
return options
options = Selenium::WebDriver::Options.chrome
options.timeouts = {script: 40_000}
/examples/ruby/spec/drivers/options_spec.rb
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe 'Chrome' do
describe 'Driver Options' do
let(:chrome_location) { driver_finder && ENV.fetch('CHROME_BIN', nil) }
let(:url) { 'https://www.selenium.dev/selenium/web/' }
it 'page load strategy normal' do
options = Selenium::WebDriver::Options.chrome
options.page_load_strategy = :normal
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'page load strategy eager' do
options = Selenium::WebDriver::Options.chrome
options.page_load_strategy = :eager
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'page load strategy none' do
options = Selenium::WebDriver::Options.chrome
options.page_load_strategy = :none
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'sets remote capabilities', skip: 'this is example code that will not execute' do
options = Selenium::WebDriver::Options.firefox
options.platform_name = 'Windows 10'
options.browser_version = 'latest'
cloud_options = {}
cloud_options[:build] = my_test_build
cloud_options[:name] = my_test_name
options.add_option('cloud:options', cloud_options)
driver = Selenium::WebDriver.for :remote, capabilities: options
driver.get(url)
driver.quit
end
it 'accepts untrusted certificates' do
options = Selenium::WebDriver::Options.chrome
options.accept_insecure_certs = true
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'sets unhandled prompt behavior' do
options = Selenium::WebDriver::Options.chrome
options.unhandled_prompt_behavior = :accept
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'sets window rect' do
options = Selenium::WebDriver::Options.firefox
options.set_window_rect = true
driver = Selenium::WebDriver.for :firefox, options: options
driver.get(url)
driver.quit
end
it 'sets strict file interactability' do
options = Selenium::WebDriver::Options.chrome
options.strict_file_interactability = true
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'sets the proxy' do
options = Selenium::WebDriver::Options.chrome
options.proxy = Selenium::WebDriver::Proxy.new(http: 'myproxy.com:8080')
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'sets the implicit timeout' do
options = Selenium::WebDriver::Options.chrome
options.timeouts = {implicit: 1}
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'sets the page load timeout' do
options = Selenium::WebDriver::Options.chrome
options.timeouts = {page_load: 400_000}
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'sets the script timeout' do
options = Selenium::WebDriver::Options.chrome
options.timeouts = {script: 40_000}
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'sets capabilities in the pre-selenium 4 way', skip: 'this is example code that will not execute' do
caps = Selenium::WebDriver::Remote::Capabilities.firefox
caps[:platform] = 'Windows 10'
caps[:version] = '92'
caps[:build] = my_test_build
caps[:name] = my_test_name
driver = Selenium::WebDriver.for :remote, url: cloud_url, desired_capabilities: caps
driver.get(url)
driver.quit
end
end
end
Page Load Timeout:
็พๅจใฎใใฉใฆใธใณใฐใณใณใใญในใใงWebใใผใธใใญใผใใใๅฟ ่ฆใใใๆ้้้ใๆๅฎใใพใใ ๆฐใใใปใใทใงใณใWebDriverใซใใฃใฆไฝๆใใใใจใใใใฉใซใใฎใฟใคใ ใขใฆใ 300,000 ใ่ชฒใใใพใใ ใใผใธใฎ่ชญใฟ่พผใฟใๆๅฎ/ใใใฉใซใใฎๆ้ๆ ใๅถ้ใใๅ ดๅใในใฏใชใใใฏใTimeoutExceptionใใซใใฃใฆๅๆญขใใใพใใ
ChromeOptions chromeOptions = getDefaultChromeOptions();
Duration duration = Duration.of(5, ChronoUnit.SECONDS);
chromeOptions.setPageLoadTimeout(duration);
/examples/java/src/test/java/dev/selenium/drivers/OptionsTest.java
package dev.selenium.drivers;
import dev.selenium.BaseTest;
import java.time.Duration;
import java.time.temporal.ChronoUnit;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Assertions;
import org.openqa.selenium.PageLoadStrategy;
import org.openqa.selenium.UnexpectedAlertBehaviour;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.chrome.ChromeDriver;
public class OptionsTest extends BaseTest {
@Test
public void setPageLoadStrategyNormal() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
chromeOptions.setPageLoadStrategy(PageLoadStrategy.NORMAL);
WebDriver driver = new ChromeDriver(chromeOptions);
try {
// Navigate to Url
driver.get("https://selenium.dev");
} finally {
driver.quit();
}
}
@Test
public void setPageLoadStrategyEager() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
chromeOptions.setPageLoadStrategy(PageLoadStrategy.EAGER);
WebDriver driver = new ChromeDriver(chromeOptions);
try {
// Navigate to Url
driver.get("https://selenium.dev");
} finally {
driver.quit();
}
}
@Test
public void setPageLoadStrategyNone() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
chromeOptions.setPageLoadStrategy(PageLoadStrategy.NONE);
WebDriver driver = new ChromeDriver(chromeOptions);
try {
// Navigate to Url
driver.get("https://selenium.dev");
} finally {
driver.quit();
}
}
@Test
public void setAcceptInsecureCerts() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
chromeOptions.setAcceptInsecureCerts(true);
WebDriver driver = new ChromeDriver(chromeOptions);
try {
// Navigate to Url
driver.get("https://selenium.dev");
} finally {
driver.quit();
}
}
@Test
public void getBrowserName() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
String name = chromeOptions.getBrowserName();
Assertions.assertFalse(name.isEmpty(), "Browser name should not be empty");
}
@Test
public void setBrowserVersion() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
String version = "latest";
chromeOptions.setBrowserVersion(version);
Assertions.assertEquals(version, chromeOptions.getBrowserVersion());
}
@Test
public void setPlatformName() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
String platform = "OS X 10.6";
chromeOptions.setPlatformName(platform);
Assertions.assertEquals(platform, chromeOptions.getPlatformName().toString());
}
@Test
public void setScriptTimeout() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
Duration duration = Duration.of(5, ChronoUnit.SECONDS);
chromeOptions.setScriptTimeout(duration);
WebDriver driver = new ChromeDriver(chromeOptions);
try {
Duration timeout = driver.manage().timeouts().getScriptTimeout();
Assertions.assertEquals(timeout, duration, "The script timeout should be set to 5 seconds.");
} finally {
driver.quit();
}
}
@Test
public void setPageLoadTimeout() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
Duration duration = Duration.of(5, ChronoUnit.SECONDS);
chromeOptions.setPageLoadTimeout(duration);
WebDriver driver = new ChromeDriver(chromeOptions);
try {
Duration timeout = driver.manage().timeouts().getPageLoadTimeout();
Assertions.assertEquals(timeout, duration, "The page load timeout should be set to 5 seconds.");
} finally {
driver.quit();
}
}
@Test
public void setImplicitWaitTimeout() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
Duration duration = Duration.of(5, ChronoUnit.SECONDS);
chromeOptions.setImplicitWaitTimeout(duration);
WebDriver driver = new ChromeDriver(chromeOptions);
try {
Duration timeout = driver.manage().timeouts().getImplicitWaitTimeout();
Assertions.assertEquals(timeout, duration, "The implicit wait timeout should be set to 5 seconds.");
} finally {
driver.quit();
}
}
@Test
public void setUnhandledPromptBehaviour() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
chromeOptions.setUnhandledPromptBehaviour(UnexpectedAlertBehaviour.DISMISS_AND_NOTIFY);
//verify the capability object is not null
Object capabilityObject = 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());
}
@Test
public void setWindowRect() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
chromeOptions.setCapability(CapabilityType.SET_WINDOW_RECT, true);
//verify the capability object is not null
Object capabilityObject = chromeOptions.getCapability(CapabilityType.SET_WINDOW_RECT);
Assertions.assertNotNull(capabilityObject, "Capability SET_WINDOW_RECT should not be null.");
Boolean capability = (Boolean) capabilityObject;
Assertions.assertTrue(capability, "The capability SET_WINDOW_RECT should be set to true.");
}
@Test
public void setStrictFileInteractability() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
chromeOptions.setCapability(CapabilityType.STRICT_FILE_INTERACTABILITY, true);
//verify the capability object is not null
Object capabilityObject = chromeOptions.getCapability(CapabilityType.STRICT_FILE_INTERACTABILITY);
Assertions.assertNotNull(capabilityObject, "Capability STRICT_FILE_INTERACTABILITY should not be null.");
Boolean capability = (Boolean) capabilityObject;
Assertions.assertTrue(capability, "The capability STRICT_FILE_INTERACTABILITY should be set to true.");
}
}
options = get_default_chrome_options()
options.timeouts = { 'pageLoad': 5000 }
driver = webdriver.Chrome(options=options)
/examples/python/tests/drivers/test_options.py
from selenium import webdriver
from selenium.webdriver.common.proxy import Proxy
from selenium.webdriver.common.proxy import ProxyType
def test_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()
def test_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()
def test_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()
def test_timeouts_script():
options = get_default_chrome_options()
options.timeouts = { 'script': 5000 }
driver = webdriver.Chrome(options=options)
driver.get("https://www.selenium.dev/")
driver.quit()
def test_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()
def test_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()
def test_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()
def test_set_window_rect():
options = webdriver.FirefoxOptions()
options.set_window_rect = True # Full support in Firefox
driver = webdriver.Firefox(options=options)
driver.get("https://www.selenium.dev/")
driver.quit()
def test_strict_file_interactability():
options = get_default_chrome_options()
options.strict_file_interactability = True
driver = webdriver.Chrome(options=options)
driver.get("https://www.selenium.dev/")
driver.quit()
def test_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()
def test_set_browser_name():
options = get_default_chrome_options()
assert options.capabilities['browserName'] == 'chrome'
driver = webdriver.Chrome(options=options)
driver.get("https://www.selenium.dev/")
driver.quit()
def test_set_browser_version():
options = get_default_chrome_options()
options.browser_version = 'stable'
assert options.capabilities['browserVersion'] == 'stable'
driver = webdriver.Chrome(options=options)
driver.get("https://www.selenium.dev/")
driver.quit()
def test_platform_name():
options = get_default_chrome_options()
options.platform_name = 'any'
driver = webdriver.Chrome(options=options)
driver.get("https://www.selenium.dev/")
driver.quit()
def test_accept_insecure_certs():
options = get_default_chrome_options()
options.accept_insecure_certs = True
driver = webdriver.Chrome(options=options)
driver.get("https://www.selenium.dev/")
driver.quit()
def get_default_chrome_options():
options = webdriver.ChromeOptions()
options.add_argument("--no-sandbox")
return options
options = Selenium::WebDriver::Options.chrome
options.timeouts = {page_load: 400_000}
/examples/ruby/spec/drivers/options_spec.rb
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe 'Chrome' do
describe 'Driver Options' do
let(:chrome_location) { driver_finder && ENV.fetch('CHROME_BIN', nil) }
let(:url) { 'https://www.selenium.dev/selenium/web/' }
it 'page load strategy normal' do
options = Selenium::WebDriver::Options.chrome
options.page_load_strategy = :normal
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'page load strategy eager' do
options = Selenium::WebDriver::Options.chrome
options.page_load_strategy = :eager
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'page load strategy none' do
options = Selenium::WebDriver::Options.chrome
options.page_load_strategy = :none
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'sets remote capabilities', skip: 'this is example code that will not execute' do
options = Selenium::WebDriver::Options.firefox
options.platform_name = 'Windows 10'
options.browser_version = 'latest'
cloud_options = {}
cloud_options[:build] = my_test_build
cloud_options[:name] = my_test_name
options.add_option('cloud:options', cloud_options)
driver = Selenium::WebDriver.for :remote, capabilities: options
driver.get(url)
driver.quit
end
it 'accepts untrusted certificates' do
options = Selenium::WebDriver::Options.chrome
options.accept_insecure_certs = true
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'sets unhandled prompt behavior' do
options = Selenium::WebDriver::Options.chrome
options.unhandled_prompt_behavior = :accept
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'sets window rect' do
options = Selenium::WebDriver::Options.firefox
options.set_window_rect = true
driver = Selenium::WebDriver.for :firefox, options: options
driver.get(url)
driver.quit
end
it 'sets strict file interactability' do
options = Selenium::WebDriver::Options.chrome
options.strict_file_interactability = true
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'sets the proxy' do
options = Selenium::WebDriver::Options.chrome
options.proxy = Selenium::WebDriver::Proxy.new(http: 'myproxy.com:8080')
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'sets the implicit timeout' do
options = Selenium::WebDriver::Options.chrome
options.timeouts = {implicit: 1}
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'sets the page load timeout' do
options = Selenium::WebDriver::Options.chrome
options.timeouts = {page_load: 400_000}
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'sets the script timeout' do
options = Selenium::WebDriver::Options.chrome
options.timeouts = {script: 40_000}
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'sets capabilities in the pre-selenium 4 way', skip: 'this is example code that will not execute' do
caps = Selenium::WebDriver::Remote::Capabilities.firefox
caps[:platform] = 'Windows 10'
caps[:version] = '92'
caps[:build] = my_test_build
caps[:name] = my_test_name
driver = Selenium::WebDriver.for :remote, url: cloud_url, desired_capabilities: caps
driver.get(url)
driver.quit
end
end
end
Implicit Wait Timeout
ใใใฏใ่ฆ็ด ใๆค็ดขใใใจใใซๆ้ป็ใช่ฆ็ด ใฎๆค็ดขๆฆ็ฅใๅพ ใคๆ้ใๆๅฎใใพใใ ๆฐใใใปใใทใงใณใWebDriverใซใใฃใฆไฝๆใใใใจใใใใฉใซใใฎใฟใคใ ใขใฆใ 0 ใ่ชฒใใใพใใ
ChromeOptions chromeOptions = getDefaultChromeOptions();
Duration duration = Duration.of(5, ChronoUnit.SECONDS);
chromeOptions.setImplicitWaitTimeout(duration);
/examples/java/src/test/java/dev/selenium/drivers/OptionsTest.java
package dev.selenium.drivers;
import dev.selenium.BaseTest;
import java.time.Duration;
import java.time.temporal.ChronoUnit;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Assertions;
import org.openqa.selenium.PageLoadStrategy;
import org.openqa.selenium.UnexpectedAlertBehaviour;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.chrome.ChromeDriver;
public class OptionsTest extends BaseTest {
@Test
public void setPageLoadStrategyNormal() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
chromeOptions.setPageLoadStrategy(PageLoadStrategy.NORMAL);
WebDriver driver = new ChromeDriver(chromeOptions);
try {
// Navigate to Url
driver.get("https://selenium.dev");
} finally {
driver.quit();
}
}
@Test
public void setPageLoadStrategyEager() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
chromeOptions.setPageLoadStrategy(PageLoadStrategy.EAGER);
WebDriver driver = new ChromeDriver(chromeOptions);
try {
// Navigate to Url
driver.get("https://selenium.dev");
} finally {
driver.quit();
}
}
@Test
public void setPageLoadStrategyNone() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
chromeOptions.setPageLoadStrategy(PageLoadStrategy.NONE);
WebDriver driver = new ChromeDriver(chromeOptions);
try {
// Navigate to Url
driver.get("https://selenium.dev");
} finally {
driver.quit();
}
}
@Test
public void setAcceptInsecureCerts() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
chromeOptions.setAcceptInsecureCerts(true);
WebDriver driver = new ChromeDriver(chromeOptions);
try {
// Navigate to Url
driver.get("https://selenium.dev");
} finally {
driver.quit();
}
}
@Test
public void getBrowserName() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
String name = chromeOptions.getBrowserName();
Assertions.assertFalse(name.isEmpty(), "Browser name should not be empty");
}
@Test
public void setBrowserVersion() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
String version = "latest";
chromeOptions.setBrowserVersion(version);
Assertions.assertEquals(version, chromeOptions.getBrowserVersion());
}
@Test
public void setPlatformName() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
String platform = "OS X 10.6";
chromeOptions.setPlatformName(platform);
Assertions.assertEquals(platform, chromeOptions.getPlatformName().toString());
}
@Test
public void setScriptTimeout() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
Duration duration = Duration.of(5, ChronoUnit.SECONDS);
chromeOptions.setScriptTimeout(duration);
WebDriver driver = new ChromeDriver(chromeOptions);
try {
Duration timeout = driver.manage().timeouts().getScriptTimeout();
Assertions.assertEquals(timeout, duration, "The script timeout should be set to 5 seconds.");
} finally {
driver.quit();
}
}
@Test
public void setPageLoadTimeout() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
Duration duration = Duration.of(5, ChronoUnit.SECONDS);
chromeOptions.setPageLoadTimeout(duration);
WebDriver driver = new ChromeDriver(chromeOptions);
try {
Duration timeout = driver.manage().timeouts().getPageLoadTimeout();
Assertions.assertEquals(timeout, duration, "The page load timeout should be set to 5 seconds.");
} finally {
driver.quit();
}
}
@Test
public void setImplicitWaitTimeout() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
Duration duration = Duration.of(5, ChronoUnit.SECONDS);
chromeOptions.setImplicitWaitTimeout(duration);
WebDriver driver = new ChromeDriver(chromeOptions);
try {
Duration timeout = driver.manage().timeouts().getImplicitWaitTimeout();
Assertions.assertEquals(timeout, duration, "The implicit wait timeout should be set to 5 seconds.");
} finally {
driver.quit();
}
}
@Test
public void setUnhandledPromptBehaviour() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
chromeOptions.setUnhandledPromptBehaviour(UnexpectedAlertBehaviour.DISMISS_AND_NOTIFY);
//verify the capability object is not null
Object capabilityObject = 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());
}
@Test
public void setWindowRect() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
chromeOptions.setCapability(CapabilityType.SET_WINDOW_RECT, true);
//verify the capability object is not null
Object capabilityObject = chromeOptions.getCapability(CapabilityType.SET_WINDOW_RECT);
Assertions.assertNotNull(capabilityObject, "Capability SET_WINDOW_RECT should not be null.");
Boolean capability = (Boolean) capabilityObject;
Assertions.assertTrue(capability, "The capability SET_WINDOW_RECT should be set to true.");
}
@Test
public void setStrictFileInteractability() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
chromeOptions.setCapability(CapabilityType.STRICT_FILE_INTERACTABILITY, true);
//verify the capability object is not null
Object capabilityObject = chromeOptions.getCapability(CapabilityType.STRICT_FILE_INTERACTABILITY);
Assertions.assertNotNull(capabilityObject, "Capability STRICT_FILE_INTERACTABILITY should not be null.");
Boolean capability = (Boolean) capabilityObject;
Assertions.assertTrue(capability, "The capability STRICT_FILE_INTERACTABILITY should be set to true.");
}
}
options = get_default_chrome_options()
options.timeouts = { 'implicit': 5000 }
driver = webdriver.Chrome(options=options)
/examples/python/tests/drivers/test_options.py
from selenium import webdriver
from selenium.webdriver.common.proxy import Proxy
from selenium.webdriver.common.proxy import ProxyType
def test_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()
def test_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()
def test_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()
def test_timeouts_script():
options = get_default_chrome_options()
options.timeouts = { 'script': 5000 }
driver = webdriver.Chrome(options=options)
driver.get("https://www.selenium.dev/")
driver.quit()
def test_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()
def test_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()
def test_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()
def test_set_window_rect():
options = webdriver.FirefoxOptions()
options.set_window_rect = True # Full support in Firefox
driver = webdriver.Firefox(options=options)
driver.get("https://www.selenium.dev/")
driver.quit()
def test_strict_file_interactability():
options = get_default_chrome_options()
options.strict_file_interactability = True
driver = webdriver.Chrome(options=options)
driver.get("https://www.selenium.dev/")
driver.quit()
def test_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()
def test_set_browser_name():
options = get_default_chrome_options()
assert options.capabilities['browserName'] == 'chrome'
driver = webdriver.Chrome(options=options)
driver.get("https://www.selenium.dev/")
driver.quit()
def test_set_browser_version():
options = get_default_chrome_options()
options.browser_version = 'stable'
assert options.capabilities['browserVersion'] == 'stable'
driver = webdriver.Chrome(options=options)
driver.get("https://www.selenium.dev/")
driver.quit()
def test_platform_name():
options = get_default_chrome_options()
options.platform_name = 'any'
driver = webdriver.Chrome(options=options)
driver.get("https://www.selenium.dev/")
driver.quit()
def test_accept_insecure_certs():
options = get_default_chrome_options()
options.accept_insecure_certs = True
driver = webdriver.Chrome(options=options)
driver.get("https://www.selenium.dev/")
driver.quit()
def get_default_chrome_options():
options = webdriver.ChromeOptions()
options.add_argument("--no-sandbox")
return options
options = Selenium::WebDriver::Options.chrome
options.timeouts = {implicit: 1}
/examples/ruby/spec/drivers/options_spec.rb
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe 'Chrome' do
describe 'Driver Options' do
let(:chrome_location) { driver_finder && ENV.fetch('CHROME_BIN', nil) }
let(:url) { 'https://www.selenium.dev/selenium/web/' }
it 'page load strategy normal' do
options = Selenium::WebDriver::Options.chrome
options.page_load_strategy = :normal
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'page load strategy eager' do
options = Selenium::WebDriver::Options.chrome
options.page_load_strategy = :eager
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'page load strategy none' do
options = Selenium::WebDriver::Options.chrome
options.page_load_strategy = :none
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'sets remote capabilities', skip: 'this is example code that will not execute' do
options = Selenium::WebDriver::Options.firefox
options.platform_name = 'Windows 10'
options.browser_version = 'latest'
cloud_options = {}
cloud_options[:build] = my_test_build
cloud_options[:name] = my_test_name
options.add_option('cloud:options', cloud_options)
driver = Selenium::WebDriver.for :remote, capabilities: options
driver.get(url)
driver.quit
end
it 'accepts untrusted certificates' do
options = Selenium::WebDriver::Options.chrome
options.accept_insecure_certs = true
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'sets unhandled prompt behavior' do
options = Selenium::WebDriver::Options.chrome
options.unhandled_prompt_behavior = :accept
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'sets window rect' do
options = Selenium::WebDriver::Options.firefox
options.set_window_rect = true
driver = Selenium::WebDriver.for :firefox, options: options
driver.get(url)
driver.quit
end
it 'sets strict file interactability' do
options = Selenium::WebDriver::Options.chrome
options.strict_file_interactability = true
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'sets the proxy' do
options = Selenium::WebDriver::Options.chrome
options.proxy = Selenium::WebDriver::Proxy.new(http: 'myproxy.com:8080')
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'sets the implicit timeout' do
options = Selenium::WebDriver::Options.chrome
options.timeouts = {implicit: 1}
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'sets the page load timeout' do
options = Selenium::WebDriver::Options.chrome
options.timeouts = {page_load: 400_000}
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'sets the script timeout' do
options = Selenium::WebDriver::Options.chrome
options.timeouts = {script: 40_000}
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'sets capabilities in the pre-selenium 4 way', skip: 'this is example code that will not execute' do
caps = Selenium::WebDriver::Remote::Capabilities.firefox
caps[:platform] = 'Windows 10'
caps[:version] = '92'
caps[:build] = my_test_build
caps[:name] = my_test_name
driver = Selenium::WebDriver.for :remote, url: cloud_url, desired_capabilities: caps
driver.get(url)
driver.quit
end
end
end
unhandledPromptBehavior
็พๅจใฎใปใใทใงใณใฎ ใฆใผใถใผใใญใณใใใใณใใฉใผ
ใฎ็ถๆ
ใๆๅฎใใพใใ
ใใใฉใซใใงใฏใ dismiss and notify (ๅดไธใใฆ้็ฅใใ) ็ถๆ
ใจใชใใพใใ
User Prompt Handler
ใใใฏใใชใขใผใใจใณใใงใฆใผใถใผใใญใณใใใ่กจ็คบใใใใจใใซๅฎ่กใใๅฟ
่ฆใใใใขใฏใทใงใณใๅฎ็พฉใใพใใ
ใใใฏใ unhandledPromptBehavior
Capabilityใซใใฃใฆๅฎ็พฉใใใๆฌกใฎ็ถๆ
ใใใใพใใ
- dismiss (ๅดไธ)
- accept (ๅๅ ฅ)
- dismiss and notify (ๅดไธใใฆ้็ฅ)
- accept and notify (ๅใๅ ฅใใฆ้็ฅ)
- ignore (็ก่ฆ)
ChromeOptions chromeOptions = getDefaultChromeOptions();
chromeOptions.setUnhandledPromptBehaviour(UnexpectedAlertBehaviour.DISMISS_AND_NOTIFY);
/examples/java/src/test/java/dev/selenium/drivers/OptionsTest.java
package dev.selenium.drivers;
import dev.selenium.BaseTest;
import java.time.Duration;
import java.time.temporal.ChronoUnit;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Assertions;
import org.openqa.selenium.PageLoadStrategy;
import org.openqa.selenium.UnexpectedAlertBehaviour;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.chrome.ChromeDriver;
public class OptionsTest extends BaseTest {
@Test
public void setPageLoadStrategyNormal() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
chromeOptions.setPageLoadStrategy(PageLoadStrategy.NORMAL);
WebDriver driver = new ChromeDriver(chromeOptions);
try {
// Navigate to Url
driver.get("https://selenium.dev");
} finally {
driver.quit();
}
}
@Test
public void setPageLoadStrategyEager() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
chromeOptions.setPageLoadStrategy(PageLoadStrategy.EAGER);
WebDriver driver = new ChromeDriver(chromeOptions);
try {
// Navigate to Url
driver.get("https://selenium.dev");
} finally {
driver.quit();
}
}
@Test
public void setPageLoadStrategyNone() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
chromeOptions.setPageLoadStrategy(PageLoadStrategy.NONE);
WebDriver driver = new ChromeDriver(chromeOptions);
try {
// Navigate to Url
driver.get("https://selenium.dev");
} finally {
driver.quit();
}
}
@Test
public void setAcceptInsecureCerts() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
chromeOptions.setAcceptInsecureCerts(true);
WebDriver driver = new ChromeDriver(chromeOptions);
try {
// Navigate to Url
driver.get("https://selenium.dev");
} finally {
driver.quit();
}
}
@Test
public void getBrowserName() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
String name = chromeOptions.getBrowserName();
Assertions.assertFalse(name.isEmpty(), "Browser name should not be empty");
}
@Test
public void setBrowserVersion() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
String version = "latest";
chromeOptions.setBrowserVersion(version);
Assertions.assertEquals(version, chromeOptions.getBrowserVersion());
}
@Test
public void setPlatformName() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
String platform = "OS X 10.6";
chromeOptions.setPlatformName(platform);
Assertions.assertEquals(platform, chromeOptions.getPlatformName().toString());
}
@Test
public void setScriptTimeout() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
Duration duration = Duration.of(5, ChronoUnit.SECONDS);
chromeOptions.setScriptTimeout(duration);
WebDriver driver = new ChromeDriver(chromeOptions);
try {
Duration timeout = driver.manage().timeouts().getScriptTimeout();
Assertions.assertEquals(timeout, duration, "The script timeout should be set to 5 seconds.");
} finally {
driver.quit();
}
}
@Test
public void setPageLoadTimeout() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
Duration duration = Duration.of(5, ChronoUnit.SECONDS);
chromeOptions.setPageLoadTimeout(duration);
WebDriver driver = new ChromeDriver(chromeOptions);
try {
Duration timeout = driver.manage().timeouts().getPageLoadTimeout();
Assertions.assertEquals(timeout, duration, "The page load timeout should be set to 5 seconds.");
} finally {
driver.quit();
}
}
@Test
public void setImplicitWaitTimeout() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
Duration duration = Duration.of(5, ChronoUnit.SECONDS);
chromeOptions.setImplicitWaitTimeout(duration);
WebDriver driver = new ChromeDriver(chromeOptions);
try {
Duration timeout = driver.manage().timeouts().getImplicitWaitTimeout();
Assertions.assertEquals(timeout, duration, "The implicit wait timeout should be set to 5 seconds.");
} finally {
driver.quit();
}
}
@Test
public void setUnhandledPromptBehaviour() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
chromeOptions.setUnhandledPromptBehaviour(UnexpectedAlertBehaviour.DISMISS_AND_NOTIFY);
//verify the capability object is not null
Object capabilityObject = 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());
}
@Test
public void setWindowRect() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
chromeOptions.setCapability(CapabilityType.SET_WINDOW_RECT, true);
//verify the capability object is not null
Object capabilityObject = chromeOptions.getCapability(CapabilityType.SET_WINDOW_RECT);
Assertions.assertNotNull(capabilityObject, "Capability SET_WINDOW_RECT should not be null.");
Boolean capability = (Boolean) capabilityObject;
Assertions.assertTrue(capability, "The capability SET_WINDOW_RECT should be set to true.");
}
@Test
public void setStrictFileInteractability() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
chromeOptions.setCapability(CapabilityType.STRICT_FILE_INTERACTABILITY, true);
//verify the capability object is not null
Object capabilityObject = chromeOptions.getCapability(CapabilityType.STRICT_FILE_INTERACTABILITY);
Assertions.assertNotNull(capabilityObject, "Capability STRICT_FILE_INTERACTABILITY should not be null.");
Boolean capability = (Boolean) capabilityObject;
Assertions.assertTrue(capability, "The capability STRICT_FILE_INTERACTABILITY should be set to true.");
}
}
options = get_default_chrome_options()
options.unhandled_prompt_behavior = 'accept'
driver = webdriver.Chrome(options=options)
/examples/python/tests/drivers/test_options.py
from selenium import webdriver
from selenium.webdriver.common.proxy import Proxy
from selenium.webdriver.common.proxy import ProxyType
def test_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()
def test_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()
def test_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()
def test_timeouts_script():
options = get_default_chrome_options()
options.timeouts = { 'script': 5000 }
driver = webdriver.Chrome(options=options)
driver.get("https://www.selenium.dev/")
driver.quit()
def test_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()
def test_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()
def test_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()
def test_set_window_rect():
options = webdriver.FirefoxOptions()
options.set_window_rect = True # Full support in Firefox
driver = webdriver.Firefox(options=options)
driver.get("https://www.selenium.dev/")
driver.quit()
def test_strict_file_interactability():
options = get_default_chrome_options()
options.strict_file_interactability = True
driver = webdriver.Chrome(options=options)
driver.get("https://www.selenium.dev/")
driver.quit()
def test_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()
def test_set_browser_name():
options = get_default_chrome_options()
assert options.capabilities['browserName'] == 'chrome'
driver = webdriver.Chrome(options=options)
driver.get("https://www.selenium.dev/")
driver.quit()
def test_set_browser_version():
options = get_default_chrome_options()
options.browser_version = 'stable'
assert options.capabilities['browserVersion'] == 'stable'
driver = webdriver.Chrome(options=options)
driver.get("https://www.selenium.dev/")
driver.quit()
def test_platform_name():
options = get_default_chrome_options()
options.platform_name = 'any'
driver = webdriver.Chrome(options=options)
driver.get("https://www.selenium.dev/")
driver.quit()
def test_accept_insecure_certs():
options = get_default_chrome_options()
options.accept_insecure_certs = True
driver = webdriver.Chrome(options=options)
driver.get("https://www.selenium.dev/")
driver.quit()
def get_default_chrome_options():
options = webdriver.ChromeOptions()
options.add_argument("--no-sandbox")
return options
options = Selenium::WebDriver::Options.chrome
options.unhandled_prompt_behavior = :accept
/examples/ruby/spec/drivers/options_spec.rb
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe 'Chrome' do
describe 'Driver Options' do
let(:chrome_location) { driver_finder && ENV.fetch('CHROME_BIN', nil) }
let(:url) { 'https://www.selenium.dev/selenium/web/' }
it 'page load strategy normal' do
options = Selenium::WebDriver::Options.chrome
options.page_load_strategy = :normal
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'page load strategy eager' do
options = Selenium::WebDriver::Options.chrome
options.page_load_strategy = :eager
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'page load strategy none' do
options = Selenium::WebDriver::Options.chrome
options.page_load_strategy = :none
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'sets remote capabilities', skip: 'this is example code that will not execute' do
options = Selenium::WebDriver::Options.firefox
options.platform_name = 'Windows 10'
options.browser_version = 'latest'
cloud_options = {}
cloud_options[:build] = my_test_build
cloud_options[:name] = my_test_name
options.add_option('cloud:options', cloud_options)
driver = Selenium::WebDriver.for :remote, capabilities: options
driver.get(url)
driver.quit
end
it 'accepts untrusted certificates' do
options = Selenium::WebDriver::Options.chrome
options.accept_insecure_certs = true
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'sets unhandled prompt behavior' do
options = Selenium::WebDriver::Options.chrome
options.unhandled_prompt_behavior = :accept
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'sets window rect' do
options = Selenium::WebDriver::Options.firefox
options.set_window_rect = true
driver = Selenium::WebDriver.for :firefox, options: options
driver.get(url)
driver.quit
end
it 'sets strict file interactability' do
options = Selenium::WebDriver::Options.chrome
options.strict_file_interactability = true
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'sets the proxy' do
options = Selenium::WebDriver::Options.chrome
options.proxy = Selenium::WebDriver::Proxy.new(http: 'myproxy.com:8080')
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'sets the implicit timeout' do
options = Selenium::WebDriver::Options.chrome
options.timeouts = {implicit: 1}
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'sets the page load timeout' do
options = Selenium::WebDriver::Options.chrome
options.timeouts = {page_load: 400_000}
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'sets the script timeout' do
options = Selenium::WebDriver::Options.chrome
options.timeouts = {script: 40_000}
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'sets capabilities in the pre-selenium 4 way', skip: 'this is example code that will not execute' do
caps = Selenium::WebDriver::Remote::Capabilities.firefox
caps[:platform] = 'Windows 10'
caps[:version] = '92'
caps[:build] = my_test_build
caps[:name] = my_test_name
driver = Selenium::WebDriver.for :remote, url: cloud_url, desired_capabilities: caps
driver.get(url)
driver.quit
end
end
end
setWindowRect
ใชใขใผใ ใจใณใใใในใฆใฎใใตใคใบๅคๆดใใใณๅ้ ็ฝฎ ใณใใณใ ใใตใใผใใใใใฉใใใ็คบใใพใใ
ChromeOptions chromeOptions = getDefaultChromeOptions();
chromeOptions.setCapability(CapabilityType.SET_WINDOW_RECT, true);
/examples/java/src/test/java/dev/selenium/drivers/OptionsTest.java
package dev.selenium.drivers;
import dev.selenium.BaseTest;
import java.time.Duration;
import java.time.temporal.ChronoUnit;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Assertions;
import org.openqa.selenium.PageLoadStrategy;
import org.openqa.selenium.UnexpectedAlertBehaviour;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.chrome.ChromeDriver;
public class OptionsTest extends BaseTest {
@Test
public void setPageLoadStrategyNormal() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
chromeOptions.setPageLoadStrategy(PageLoadStrategy.NORMAL);
WebDriver driver = new ChromeDriver(chromeOptions);
try {
// Navigate to Url
driver.get("https://selenium.dev");
} finally {
driver.quit();
}
}
@Test
public void setPageLoadStrategyEager() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
chromeOptions.setPageLoadStrategy(PageLoadStrategy.EAGER);
WebDriver driver = new ChromeDriver(chromeOptions);
try {
// Navigate to Url
driver.get("https://selenium.dev");
} finally {
driver.quit();
}
}
@Test
public void setPageLoadStrategyNone() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
chromeOptions.setPageLoadStrategy(PageLoadStrategy.NONE);
WebDriver driver = new ChromeDriver(chromeOptions);
try {
// Navigate to Url
driver.get("https://selenium.dev");
} finally {
driver.quit();
}
}
@Test
public void setAcceptInsecureCerts() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
chromeOptions.setAcceptInsecureCerts(true);
WebDriver driver = new ChromeDriver(chromeOptions);
try {
// Navigate to Url
driver.get("https://selenium.dev");
} finally {
driver.quit();
}
}
@Test
public void getBrowserName() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
String name = chromeOptions.getBrowserName();
Assertions.assertFalse(name.isEmpty(), "Browser name should not be empty");
}
@Test
public void setBrowserVersion() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
String version = "latest";
chromeOptions.setBrowserVersion(version);
Assertions.assertEquals(version, chromeOptions.getBrowserVersion());
}
@Test
public void setPlatformName() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
String platform = "OS X 10.6";
chromeOptions.setPlatformName(platform);
Assertions.assertEquals(platform, chromeOptions.getPlatformName().toString());
}
@Test
public void setScriptTimeout() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
Duration duration = Duration.of(5, ChronoUnit.SECONDS);
chromeOptions.setScriptTimeout(duration);
WebDriver driver = new ChromeDriver(chromeOptions);
try {
Duration timeout = driver.manage().timeouts().getScriptTimeout();
Assertions.assertEquals(timeout, duration, "The script timeout should be set to 5 seconds.");
} finally {
driver.quit();
}
}
@Test
public void setPageLoadTimeout() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
Duration duration = Duration.of(5, ChronoUnit.SECONDS);
chromeOptions.setPageLoadTimeout(duration);
WebDriver driver = new ChromeDriver(chromeOptions);
try {
Duration timeout = driver.manage().timeouts().getPageLoadTimeout();
Assertions.assertEquals(timeout, duration, "The page load timeout should be set to 5 seconds.");
} finally {
driver.quit();
}
}
@Test
public void setImplicitWaitTimeout() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
Duration duration = Duration.of(5, ChronoUnit.SECONDS);
chromeOptions.setImplicitWaitTimeout(duration);
WebDriver driver = new ChromeDriver(chromeOptions);
try {
Duration timeout = driver.manage().timeouts().getImplicitWaitTimeout();
Assertions.assertEquals(timeout, duration, "The implicit wait timeout should be set to 5 seconds.");
} finally {
driver.quit();
}
}
@Test
public void setUnhandledPromptBehaviour() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
chromeOptions.setUnhandledPromptBehaviour(UnexpectedAlertBehaviour.DISMISS_AND_NOTIFY);
//verify the capability object is not null
Object capabilityObject = 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());
}
@Test
public void setWindowRect() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
chromeOptions.setCapability(CapabilityType.SET_WINDOW_RECT, true);
//verify the capability object is not null
Object capabilityObject = chromeOptions.getCapability(CapabilityType.SET_WINDOW_RECT);
Assertions.assertNotNull(capabilityObject, "Capability SET_WINDOW_RECT should not be null.");
Boolean capability = (Boolean) capabilityObject;
Assertions.assertTrue(capability, "The capability SET_WINDOW_RECT should be set to true.");
}
@Test
public void setStrictFileInteractability() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
chromeOptions.setCapability(CapabilityType.STRICT_FILE_INTERACTABILITY, true);
//verify the capability object is not null
Object capabilityObject = chromeOptions.getCapability(CapabilityType.STRICT_FILE_INTERACTABILITY);
Assertions.assertNotNull(capabilityObject, "Capability STRICT_FILE_INTERACTABILITY should not be null.");
Boolean capability = (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 Firefox
driver = webdriver.Firefox(options=options)
/examples/python/tests/drivers/test_options.py
from selenium import webdriver
from selenium.webdriver.common.proxy import Proxy
from selenium.webdriver.common.proxy import ProxyType
def test_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()
def test_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()
def test_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()
def test_timeouts_script():
options = get_default_chrome_options()
options.timeouts = { 'script': 5000 }
driver = webdriver.Chrome(options=options)
driver.get("https://www.selenium.dev/")
driver.quit()
def test_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()
def test_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()
def test_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()
def test_set_window_rect():
options = webdriver.FirefoxOptions()
options.set_window_rect = True # Full support in Firefox
driver = webdriver.Firefox(options=options)
driver.get("https://www.selenium.dev/")
driver.quit()
def test_strict_file_interactability():
options = get_default_chrome_options()
options.strict_file_interactability = True
driver = webdriver.Chrome(options=options)
driver.get("https://www.selenium.dev/")
driver.quit()
def test_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()
def test_set_browser_name():
options = get_default_chrome_options()
assert options.capabilities['browserName'] == 'chrome'
driver = webdriver.Chrome(options=options)
driver.get("https://www.selenium.dev/")
driver.quit()
def test_set_browser_version():
options = get_default_chrome_options()
options.browser_version = 'stable'
assert options.capabilities['browserVersion'] == 'stable'
driver = webdriver.Chrome(options=options)
driver.get("https://www.selenium.dev/")
driver.quit()
def test_platform_name():
options = get_default_chrome_options()
options.platform_name = 'any'
driver = webdriver.Chrome(options=options)
driver.get("https://www.selenium.dev/")
driver.quit()
def test_accept_insecure_certs():
options = get_default_chrome_options()
options.accept_insecure_certs = True
driver = webdriver.Chrome(options=options)
driver.get("https://www.selenium.dev/")
driver.quit()
def get_default_chrome_options():
options = webdriver.ChromeOptions()
options.add_argument("--no-sandbox")
return options
options = Selenium::WebDriver::Options.firefox
options.set_window_rect = true
/examples/ruby/spec/drivers/options_spec.rb
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe 'Chrome' do
describe 'Driver Options' do
let(:chrome_location) { driver_finder && ENV.fetch('CHROME_BIN', nil) }
let(:url) { 'https://www.selenium.dev/selenium/web/' }
it 'page load strategy normal' do
options = Selenium::WebDriver::Options.chrome
options.page_load_strategy = :normal
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'page load strategy eager' do
options = Selenium::WebDriver::Options.chrome
options.page_load_strategy = :eager
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'page load strategy none' do
options = Selenium::WebDriver::Options.chrome
options.page_load_strategy = :none
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'sets remote capabilities', skip: 'this is example code that will not execute' do
options = Selenium::WebDriver::Options.firefox
options.platform_name = 'Windows 10'
options.browser_version = 'latest'
cloud_options = {}
cloud_options[:build] = my_test_build
cloud_options[:name] = my_test_name
options.add_option('cloud:options', cloud_options)
driver = Selenium::WebDriver.for :remote, capabilities: options
driver.get(url)
driver.quit
end
it 'accepts untrusted certificates' do
options = Selenium::WebDriver::Options.chrome
options.accept_insecure_certs = true
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'sets unhandled prompt behavior' do
options = Selenium::WebDriver::Options.chrome
options.unhandled_prompt_behavior = :accept
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'sets window rect' do
options = Selenium::WebDriver::Options.firefox
options.set_window_rect = true
driver = Selenium::WebDriver.for :firefox, options: options
driver.get(url)
driver.quit
end
it 'sets strict file interactability' do
options = Selenium::WebDriver::Options.chrome
options.strict_file_interactability = true
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'sets the proxy' do
options = Selenium::WebDriver::Options.chrome
options.proxy = Selenium::WebDriver::Proxy.new(http: 'myproxy.com:8080')
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'sets the implicit timeout' do
options = Selenium::WebDriver::Options.chrome
options.timeouts = {implicit: 1}
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'sets the page load timeout' do
options = Selenium::WebDriver::Options.chrome
options.timeouts = {page_load: 400_000}
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'sets the script timeout' do
options = Selenium::WebDriver::Options.chrome
options.timeouts = {script: 40_000}
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'sets capabilities in the pre-selenium 4 way', skip: 'this is example code that will not execute' do
caps = Selenium::WebDriver::Remote::Capabilities.firefox
caps[:platform] = 'Windows 10'
caps[:version] = '92'
caps[:build] = my_test_build
caps[:name] = my_test_name
driver = Selenium::WebDriver.for :remote, url: cloud_url, desired_capabilities: caps
driver.get(url)
driver.quit
end
end
end
strictFileInteractability
ใใฎๆฐใใcapabilityใฏใๅณๅฏใช็ธไบไฝ็จใใงใใฏใ input type = file ่ฆ็ด ใซ้ฉ็จใใๅฟ ่ฆใใใใใฉใใใ็คบใใพใใ ๅณๅฏใช็ธไบไฝ็จใใงใใฏใฏใใใฉใซใใงใชใใซใชใฃใฆใใใใใ้ ใใใกใคใซใฎใขใใใญใผใใณใณใใญใผใซใง Element Send Keys ใไฝฟ็จใใๅ ดๅใฎๅไฝใๅคๆดใใใพใใ
ChromeOptions chromeOptions = getDefaultChromeOptions();
chromeOptions.setCapability(CapabilityType.STRICT_FILE_INTERACTABILITY, true);
/examples/java/src/test/java/dev/selenium/drivers/OptionsTest.java
package dev.selenium.drivers;
import dev.selenium.BaseTest;
import java.time.Duration;
import java.time.temporal.ChronoUnit;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Assertions;
import org.openqa.selenium.PageLoadStrategy;
import org.openqa.selenium.UnexpectedAlertBehaviour;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.chrome.ChromeDriver;
public class OptionsTest extends BaseTest {
@Test
public void setPageLoadStrategyNormal() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
chromeOptions.setPageLoadStrategy(PageLoadStrategy.NORMAL);
WebDriver driver = new ChromeDriver(chromeOptions);
try {
// Navigate to Url
driver.get("https://selenium.dev");
} finally {
driver.quit();
}
}
@Test
public void setPageLoadStrategyEager() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
chromeOptions.setPageLoadStrategy(PageLoadStrategy.EAGER);
WebDriver driver = new ChromeDriver(chromeOptions);
try {
// Navigate to Url
driver.get("https://selenium.dev");
} finally {
driver.quit();
}
}
@Test
public void setPageLoadStrategyNone() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
chromeOptions.setPageLoadStrategy(PageLoadStrategy.NONE);
WebDriver driver = new ChromeDriver(chromeOptions);
try {
// Navigate to Url
driver.get("https://selenium.dev");
} finally {
driver.quit();
}
}
@Test
public void setAcceptInsecureCerts() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
chromeOptions.setAcceptInsecureCerts(true);
WebDriver driver = new ChromeDriver(chromeOptions);
try {
// Navigate to Url
driver.get("https://selenium.dev");
} finally {
driver.quit();
}
}
@Test
public void getBrowserName() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
String name = chromeOptions.getBrowserName();
Assertions.assertFalse(name.isEmpty(), "Browser name should not be empty");
}
@Test
public void setBrowserVersion() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
String version = "latest";
chromeOptions.setBrowserVersion(version);
Assertions.assertEquals(version, chromeOptions.getBrowserVersion());
}
@Test
public void setPlatformName() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
String platform = "OS X 10.6";
chromeOptions.setPlatformName(platform);
Assertions.assertEquals(platform, chromeOptions.getPlatformName().toString());
}
@Test
public void setScriptTimeout() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
Duration duration = Duration.of(5, ChronoUnit.SECONDS);
chromeOptions.setScriptTimeout(duration);
WebDriver driver = new ChromeDriver(chromeOptions);
try {
Duration timeout = driver.manage().timeouts().getScriptTimeout();
Assertions.assertEquals(timeout, duration, "The script timeout should be set to 5 seconds.");
} finally {
driver.quit();
}
}
@Test
public void setPageLoadTimeout() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
Duration duration = Duration.of(5, ChronoUnit.SECONDS);
chromeOptions.setPageLoadTimeout(duration);
WebDriver driver = new ChromeDriver(chromeOptions);
try {
Duration timeout = driver.manage().timeouts().getPageLoadTimeout();
Assertions.assertEquals(timeout, duration, "The page load timeout should be set to 5 seconds.");
} finally {
driver.quit();
}
}
@Test
public void setImplicitWaitTimeout() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
Duration duration = Duration.of(5, ChronoUnit.SECONDS);
chromeOptions.setImplicitWaitTimeout(duration);
WebDriver driver = new ChromeDriver(chromeOptions);
try {
Duration timeout = driver.manage().timeouts().getImplicitWaitTimeout();
Assertions.assertEquals(timeout, duration, "The implicit wait timeout should be set to 5 seconds.");
} finally {
driver.quit();
}
}
@Test
public void setUnhandledPromptBehaviour() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
chromeOptions.setUnhandledPromptBehaviour(UnexpectedAlertBehaviour.DISMISS_AND_NOTIFY);
//verify the capability object is not null
Object capabilityObject = 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());
}
@Test
public void setWindowRect() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
chromeOptions.setCapability(CapabilityType.SET_WINDOW_RECT, true);
//verify the capability object is not null
Object capabilityObject = chromeOptions.getCapability(CapabilityType.SET_WINDOW_RECT);
Assertions.assertNotNull(capabilityObject, "Capability SET_WINDOW_RECT should not be null.");
Boolean capability = (Boolean) capabilityObject;
Assertions.assertTrue(capability, "The capability SET_WINDOW_RECT should be set to true.");
}
@Test
public void setStrictFileInteractability() {
ChromeOptions chromeOptions = getDefaultChromeOptions();
chromeOptions.setCapability(CapabilityType.STRICT_FILE_INTERACTABILITY, true);
//verify the capability object is not null
Object capabilityObject = chromeOptions.getCapability(CapabilityType.STRICT_FILE_INTERACTABILITY);
Assertions.assertNotNull(capabilityObject, "Capability STRICT_FILE_INTERACTABILITY should not be null.");
Boolean capability = (Boolean) capabilityObject;
Assertions.assertTrue(capability, "The capability STRICT_FILE_INTERACTABILITY should be set to true.");
}
}
options = get_default_chrome_options()
options.strict_file_interactability = True
driver = webdriver.Chrome(options=options)
/examples/python/tests/drivers/test_options.py
from selenium import webdriver
from selenium.webdriver.common.proxy import Proxy
from selenium.webdriver.common.proxy import ProxyType
def test_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()
def test_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()
def test_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()
def test_timeouts_script():
options = get_default_chrome_options()
options.timeouts = { 'script': 5000 }
driver = webdriver.Chrome(options=options)
driver.get("https://www.selenium.dev/")
driver.quit()
def test_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()
def test_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()
def test_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()
def test_set_window_rect():
options = webdriver.FirefoxOptions()
options.set_window_rect = True # Full support in Firefox
driver = webdriver.Firefox(options=options)
driver.get("https://www.selenium.dev/")
driver.quit()
def test_strict_file_interactability():
options = get_default_chrome_options()
options.strict_file_interactability = True
driver = webdriver.Chrome(options=options)
driver.get("https://www.selenium.dev/")
driver.quit()
def test_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()
def test_set_browser_name():
options = get_default_chrome_options()
assert options.capabilities['browserName'] == 'chrome'
driver = webdriver.Chrome(options=options)
driver.get("https://www.selenium.dev/")
driver.quit()
def test_set_browser_version():
options = get_default_chrome_options()
options.browser_version = 'stable'
assert options.capabilities['browserVersion'] == 'stable'
driver = webdriver.Chrome(options=options)
driver.get("https://www.selenium.dev/")
driver.quit()
def test_platform_name():
options = get_default_chrome_options()
options.platform_name = 'any'
driver = webdriver.Chrome(options=options)
driver.get("https://www.selenium.dev/")
driver.quit()
def test_accept_insecure_certs():
options = get_default_chrome_options()
options.accept_insecure_certs = True
driver = webdriver.Chrome(options=options)
driver.get("https://www.selenium.dev/")
driver.quit()
def get_default_chrome_options():
options = webdriver.ChromeOptions()
options.add_argument("--no-sandbox")
return options
options = Selenium::WebDriver::Options.chrome
options.strict_file_interactability = true
/examples/ruby/spec/drivers/options_spec.rb
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe 'Chrome' do
describe 'Driver Options' do
let(:chrome_location) { driver_finder && ENV.fetch('CHROME_BIN', nil) }
let(:url) { 'https://www.selenium.dev/selenium/web/' }
it 'page load strategy normal' do
options = Selenium::WebDriver::Options.chrome
options.page_load_strategy = :normal
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'page load strategy eager' do
options = Selenium::WebDriver::Options.chrome
options.page_load_strategy = :eager
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'page load strategy none' do
options = Selenium::WebDriver::Options.chrome
options.page_load_strategy = :none
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'sets remote capabilities', skip: 'this is example code that will not execute' do
options = Selenium::WebDriver::Options.firefox
options.platform_name = 'Windows 10'
options.browser_version = 'latest'
cloud_options = {}
cloud_options[:build] = my_test_build
cloud_options[:name] = my_test_name
options.add_option('cloud:options', cloud_options)
driver = Selenium::WebDriver.for :remote, capabilities: options
driver.get(url)
driver.quit
end
it 'accepts untrusted certificates' do
options = Selenium::WebDriver::Options.chrome
options.accept_insecure_certs = true
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'sets unhandled prompt behavior' do
options = Selenium::WebDriver::Options.chrome
options.unhandled_prompt_behavior = :accept
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'sets window rect' do
options = Selenium::WebDriver::Options.firefox
options.set_window_rect = true
driver = Selenium::WebDriver.for :firefox, options: options
driver.get(url)
driver.quit
end
it 'sets strict file interactability' do
options = Selenium::WebDriver::Options.chrome
options.strict_file_interactability = true
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'sets the proxy' do
options = Selenium::WebDriver::Options.chrome
options.proxy = Selenium::WebDriver::Proxy.new(http: 'myproxy.com:8080')
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'sets the implicit timeout' do
options = Selenium::WebDriver::Options.chrome
options.timeouts = {implicit: 1}
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'sets the page load timeout' do
options = Selenium::WebDriver::Options.chrome
options.timeouts = {page_load: 400_000}
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'sets the script timeout' do
options = Selenium::WebDriver::Options.chrome
options.timeouts = {script: 40_000}
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'sets capabilities in the pre-selenium 4 way', skip: 'this is example code that will not execute' do
caps = Selenium::WebDriver::Remote::Capabilities.firefox
caps[:platform] = 'Windows 10'
caps[:version] = '92'
caps[:build] = my_test_build
caps[:name] = my_test_name
driver = Selenium::WebDriver.for :remote, url: cloud_url, desired_capabilities: caps
driver.get(url)
driver.quit
end
end
end
proxy
ใใญใญใทใตใผใใผใฏใใฏใฉใคใขใณใใจใตใผใใผ้ใฎ่ฆๆฑใฎไปฒไปๅฝนใจใใฆๆฉ่ฝใใพใใ ็ฐกๅใซ่จใใฐใใใฉใใฃใใฏใฏใใญใญใทใตใผใใผใ็ต็ฑใใฆใ่ฆๆฑใใใขใใฌในใซๆปใใๆปใฃใฆใใพใใ
Seleniumใไฝฟ็จใใ่ชๅๅในใฏใชใใ็จใฎใใญใญใทใตใผใใผใฏใ
- ใใใใฏใผใฏใใฉใใฃใใฏใใญใฃใใใฃใใ
- ใฆใงใใตใคใใซใใฃใฆ่กใใใๆจกๆฌใใใฏใจใณใใๅผใณๅบใ
- ่ค้ใชใใใใฏใผใฏใใใญใธใผใพใใฏๅณๆ ผใชไผๆฅญใฎๅถ้/ใใชใทใผใฎไธใงใๅฟ ่ฆใชWebใตใคใใซใขใฏใปในใใพใใ
ไผๆฅญ็ฐๅขใงใใฉใฆใถใURLใธใฎๆฅ็ถใซๅคฑๆใใๅ ดๅใ็ฐๅขใซใขใฏใปในใใใซใฏใใญใญใทใๅฟ ่ฆใงใใใใจใๅๅ ใงใใใใจใๆใๅฏ่ฝๆงใ้ซใใงใใ
Selenium WebDriverใฏ่จญๅฎใใใญใญใทใใๆนๆณใๆไพใใพใใ
import org.openqa.selenium.Proxy;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
public class ProxyTest {
public static void main(String[] args) {
Proxy proxy = new Proxy();
proxy.setHttpProxy("<HOST:PORT>");
ChromeOptions options = new ChromeOptions();
options.setCapability("proxy", proxy);
WebDriver driver = new ChromeDriver(options);
driver.get("https://www.google.com/");
driver.manage().window().maximize();
driver.quit();
}
}
options = get_default_chrome_options()
options.proxy = Proxy({ 'proxyType': ProxyType.MANUAL, 'httpProxy' : 'http.proxy:1234'})
driver = webdriver.Chrome(options=options)
/examples/python/tests/drivers/test_options.py
from selenium import webdriver
from selenium.webdriver.common.proxy import Proxy
from selenium.webdriver.common.proxy import ProxyType
def test_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()
def test_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()
def test_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()
def test_timeouts_script():
options = get_default_chrome_options()
options.timeouts = { 'script': 5000 }
driver = webdriver.Chrome(options=options)
driver.get("https://www.selenium.dev/")
driver.quit()
def test_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()
def test_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()
def test_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()
def test_set_window_rect():
options = webdriver.FirefoxOptions()
options.set_window_rect = True # Full support in Firefox
driver = webdriver.Firefox(options=options)
driver.get("https://www.selenium.dev/")
driver.quit()
def test_strict_file_interactability():
options = get_default_chrome_options()
options.strict_file_interactability = True
driver = webdriver.Chrome(options=options)
driver.get("https://www.selenium.dev/")
driver.quit()
def test_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()
def test_set_browser_name():
options = get_default_chrome_options()
assert options.capabilities['browserName'] == 'chrome'
driver = webdriver.Chrome(options=options)
driver.get("https://www.selenium.dev/")
driver.quit()
def test_set_browser_version():
options = get_default_chrome_options()
options.browser_version = 'stable'
assert options.capabilities['browserVersion'] == 'stable'
driver = webdriver.Chrome(options=options)
driver.get("https://www.selenium.dev/")
driver.quit()
def test_platform_name():
options = get_default_chrome_options()
options.platform_name = 'any'
driver = webdriver.Chrome(options=options)
driver.get("https://www.selenium.dev/")
driver.quit()
def test_accept_insecure_certs():
options = get_default_chrome_options()
options.accept_insecure_certs = True
driver = webdriver.Chrome(options=options)
driver.get("https://www.selenium.dev/")
driver.quit()
def get_default_chrome_options():
options = webdriver.ChromeOptions()
options.add_argument("--no-sandbox")
return options
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
public class ProxyTest{
public static void Main() {
ChromeOptions options = new ChromeOptions();
Proxy proxy = new Proxy();
proxy.Kind = ProxyKind.Manual;
proxy.IsAutoDetect = false;
proxy.SslProxy = "<HOST:PORT>";
options.Proxy = proxy;
options.AddArgument("ignore-certificate-errors");
IWebDriver driver = new ChromeDriver(options);
driver.Navigate().GoToUrl("https://www.selenium.dev/");
}
}
options = Selenium::WebDriver::Options.chrome
options.proxy = Selenium::WebDriver::Proxy.new(http: 'myproxy.com:8080')
/examples/ruby/spec/drivers/options_spec.rb
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe 'Chrome' do
describe 'Driver Options' do
let(:chrome_location) { driver_finder && ENV.fetch('CHROME_BIN', nil) }
let(:url) { 'https://www.selenium.dev/selenium/web/' }
it 'page load strategy normal' do
options = Selenium::WebDriver::Options.chrome
options.page_load_strategy = :normal
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'page load strategy eager' do
options = Selenium::WebDriver::Options.chrome
options.page_load_strategy = :eager
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'page load strategy none' do
options = Selenium::WebDriver::Options.chrome
options.page_load_strategy = :none
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'sets remote capabilities', skip: 'this is example code that will not execute' do
options = Selenium::WebDriver::Options.firefox
options.platform_name = 'Windows 10'
options.browser_version = 'latest'
cloud_options = {}
cloud_options[:build] = my_test_build
cloud_options[:name] = my_test_name
options.add_option('cloud:options', cloud_options)
driver = Selenium::WebDriver.for :remote, capabilities: options
driver.get(url)
driver.quit
end
it 'accepts untrusted certificates' do
options = Selenium::WebDriver::Options.chrome
options.accept_insecure_certs = true
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'sets unhandled prompt behavior' do
options = Selenium::WebDriver::Options.chrome
options.unhandled_prompt_behavior = :accept
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'sets window rect' do
options = Selenium::WebDriver::Options.firefox
options.set_window_rect = true
driver = Selenium::WebDriver.for :firefox, options: options
driver.get(url)
driver.quit
end
it 'sets strict file interactability' do
options = Selenium::WebDriver::Options.chrome
options.strict_file_interactability = true
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'sets the proxy' do
options = Selenium::WebDriver::Options.chrome
options.proxy = Selenium::WebDriver::Proxy.new(http: 'myproxy.com:8080')
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'sets the implicit timeout' do
options = Selenium::WebDriver::Options.chrome
options.timeouts = {implicit: 1}
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'sets the page load timeout' do
options = Selenium::WebDriver::Options.chrome
options.timeouts = {page_load: 400_000}
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'sets the script timeout' do
options = Selenium::WebDriver::Options.chrome
options.timeouts = {script: 40_000}
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(url)
driver.quit
end
it 'sets capabilities in the pre-selenium 4 way', skip: 'this is example code that will not execute' do
caps = Selenium::WebDriver::Remote::Capabilities.firefox
caps[:platform] = 'Windows 10'
caps[:version] = '92'
caps[:build] = my_test_build
caps[:name] = my_test_name
driver = Selenium::WebDriver.for :remote, url: cloud_url, desired_capabilities: caps
driver.get(url)
driver.quit
end
end
end
let webdriver = require('selenium-webdriver');
let chrome = require('selenium-webdriver/chrome');
let proxy = require('selenium-webdriver/proxy');
let opts = new chrome.Options();
(async function example() {
opts.setProxy(proxy.manual({http: '<HOST:PORT>'}));
let driver = new webdriver.Builder()
.forBrowser('chrome')
.setChromeOptions(opts)
.build();
try {
await driver.get("https://selenium.dev");
}
finally {
await driver.quit();
}
}());
import org.openqa.selenium.Proxy
import org.openqa.selenium.WebDriver
import org.openqa.selenium.chrome.ChromeDriver
import org.openqa.selenium.chrome.ChromeOptions
class proxyTest {
fun main() {
val proxy = Proxy()
proxy.setHttpProxy("<HOST:PORT>")
val options = ChromeOptions()
options.setCapability("proxy", proxy)
val driver: WebDriver = ChromeDriver(options)
driver["https://www.google.com/"]
driver.manage().window().maximize()
driver.quit()
}
}