Best Selenium code snippet using org.openqa.selenium.remote.RemoteWebDriverBuilder
Source:RemoteWebDriverBuilderTest.java
...51import static org.openqa.selenium.remote.BrowserType.CHROME;52import static org.openqa.selenium.remote.BrowserType.FIREFOX;53import com.google.common.collect.ImmutableMap;54@Category(UnitTests.class)55public class RemoteWebDriverBuilderTest {56 private static final SessionId SESSION_ID = new SessionId(UUID.randomUUID());57 private static final HttpResponse CANNED_SESSION_RESPONSE = new HttpResponse()58 .setContent(Contents.asJson(ImmutableMap.of(59 "value", ImmutableMap.of(60 "sessionId", SESSION_ID,61 // Primula is a canned cheese. Boom boom!62 "capabilities", new ImmutableCapabilities("se:cheese", "primula")))));63 @Test64 public void justCallingBuildWithoutSettingAnyOptionsIsAnError() {65 RemoteWebDriverBuilder builder = RemoteWebDriver.builder();66 assertThatExceptionOfType(SessionNotCreatedException.class).isThrownBy(builder::build);67 }68 @Test69 public void mustSpecifyAtLeastOneSetOfOptions() {70 List<List<Capabilities>> caps = new ArrayList<>();71 RemoteWebDriver.builder().oneOf(new FirefoxOptions())72 .address("http://localhost:34576")73 .connectingWith(config -> req -> {74 caps.add(listCapabilities(req));75 return CANNED_SESSION_RESPONSE;76 })77 .build();78 assertThat(caps).hasSize(1);79 List<Capabilities> caps0 = caps.get(0);80 assertThat(caps0.get(0).getBrowserName()).isEqualTo(FIREFOX);81 }82 @Test83 public void settingAGlobalCapabilityCountsAsAnOption() {84 AtomicBoolean match = new AtomicBoolean(false);85 RemoteWebDriver.builder().setCapability("se:cheese", "anari")86 .address("http://localhost:34576")87 .connectingWith(config -> req -> {88 listCapabilities(req).stream()89 .map(caps -> "anari".equals(caps.getCapability("se:cheese")))90 .reduce(Boolean::logicalOr)91 .ifPresent(match::set);92 return CANNED_SESSION_RESPONSE;93 })94 .build();95 assertThat(match.get()).isTrue();96 }97 @Test98 public void shouldForbidGlobalCapabilitiesFromClobberingFirstMatchCapabilities() {99 RemoteWebDriverBuilder builder = RemoteWebDriver.builder()100 .oneOf(new ImmutableCapabilities("se:cheese", "stinking bishop"))101 .setCapability("se:cheese", "cheddar")102 .address("http://localhost:38746");103 assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(builder::build);104 }105 @Test106 public void requireAllOptionsAreW3CCompatible() {107 RemoteWebDriverBuilder builder = RemoteWebDriver.builder()108 .setCapability("cheese", "casu marzu")109 .address("http://localhost:45734");110 assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(builder::build);111 }112 @Test113 public void shouldRejectOldJsonWireProtocolNames() {114 RemoteWebDriverBuilder builder = RemoteWebDriver.builder()115 .oneOf(new ImmutableCapabilities("platform", Platform.getCurrent()))116 .address("http://localhost:35856");117 assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(builder::build);118 }119 @Test120 public void shouldAllowMetaDataToBeSet() {121 AtomicBoolean seen = new AtomicBoolean(false);122 RemoteWebDriver.builder()123 .oneOf(new FirefoxOptions())124 .addMetadata("cloud:options", "merhaba")125 .address("http://localhost:34576")126 .connectingWith(config -> req -> {127 Map<String, Object> payload = new Json().toType(Contents.string(req), MAP_TYPE);128 seen.set("merhaba".equals(payload.getOrDefault("cloud:options", "")));129 return CANNED_SESSION_RESPONSE;130 })131 .build();132 assertThat(seen.get()).isTrue();133 }134 @Test135 public void doesNotAllowFirstMatchToBeUsedAsAMetadataNameAsItIsConfusing() {136 RemoteWebDriverBuilder builder = RemoteWebDriver.builder();137 assertThatExceptionOfType(IllegalArgumentException.class)138 .isThrownBy(() -> builder.addMetadata("firstMatch", "cheese"));139 }140 @Test141 public void doesNotAllowAlwaysMatchToBeUsedAsAMetadataNameAsItIsConfusing() {142 RemoteWebDriverBuilder builder = RemoteWebDriver.builder();143 assertThatExceptionOfType(IllegalArgumentException.class)144 .isThrownBy(() -> builder.addMetadata("alwaysMatch", "cheese"));145 }146 @Test147 public void doesNotAllowCapabilitiesToBeUsedAsAMetadataName() {148 RemoteWebDriverBuilder builder = RemoteWebDriver.builder();149 assertThatExceptionOfType(IllegalArgumentException.class)150 .isThrownBy(() -> builder.addMetadata("capabilities", "cheese"));151 }152 @Test153 public void shouldAllowCapabilitiesToBeSetGlobally() {154 AtomicBoolean seen = new AtomicBoolean(false);155 RemoteWebDriver.builder()156 .oneOf(new FirefoxOptions())157 .setCapability("se:option", "cheese")158 .address("http://localhost:34576")159 .connectingWith(config -> req -> {160 listCapabilities(req).stream()161 .map(caps -> "cheese".equals(caps.getCapability("se:option")))162 .reduce(Boolean::logicalAnd)163 .ifPresent(seen::set);164 return CANNED_SESSION_RESPONSE;165 })166 .build();167 assertThat(seen.get()).isTrue();168 }169 @Test170 public void ifARemoteUrlIsGivenThatIsUsedForTheSession() {171 URI uri = URI.create("http://localhost:7575");172 AtomicReference<URI> seen = new AtomicReference<>();173 RemoteWebDriver.builder()174 .oneOf(new FirefoxOptions())175 .address(uri)176 .connectingWith(config -> req -> {177 seen.set(config.baseUri());178 return CANNED_SESSION_RESPONSE;179 })180 .build();181 assertThat(seen.get()).isEqualTo(uri);182 }183 @Test184 public void shouldUseGivenDriverServiceForUrlIfProvided() throws IOException {185 URI uri = URI.create("http://localhost:9898");186 URL url = uri.toURL();187 DriverService service = new FakeDriverService() {188 @Override189 public URL getUrl() {190 return url;191 }192 };193 AtomicReference<URI> seen = new AtomicReference<>();194 RemoteWebDriver.builder()195 .oneOf(new FirefoxOptions())196 .withDriverService(service)197 .connectingWith(config -> {198 seen.set(config.baseUri());199 return req -> CANNED_SESSION_RESPONSE;200 })201 .build();202 assertThat(seen.get()).isEqualTo(uri);203 }204 @Test205 public void settingBothDriverServiceAndUrlIsAnError() throws IOException {206 RemoteWebDriverBuilder builder = RemoteWebDriver.builder()207 .withDriverService(new FakeDriverService());208 assertThatExceptionOfType(IllegalArgumentException.class)209 .isThrownBy(() -> builder.address("http://localhost:89789"));210 }211 @Test212 public void oneOfWillClearOutTheCurrentlySetCapabilities() {213 AtomicBoolean allOk = new AtomicBoolean();214 RemoteWebDriver.builder()215 .oneOf(new FirefoxOptions())216 .addAlternative(new InternetExplorerOptions())217 .oneOf(new ChromeOptions())218 .address("http://localhost:34576")219 .connectingWith(config -> req -> {220 List<Capabilities> caps = listCapabilities(req);...
Source:RemoteWebDriverBuilder.java
...49import java.util.function.Consumer;50import java.util.function.Supplier;51import java.util.stream.StreamSupport;52@Beta53class RemoteWebDriverBuilder {54 private final static Set<String> ILLEGAL_METADATA_KEYS = ImmutableSet.of(55 "alwaysMatch",56 "capabilities",57 "firstMatch");58 private final static AcceptedW3CCapabilityKeys OK_KEYS = new AcceptedW3CCapabilityKeys();59 private final List<Map<String, Object>> options = new ArrayList<>();60 private final Map<String, Object> metadata = new TreeMap<>();61 private final Map<String, Object> additionalCapabilities = new TreeMap<>();62 private URL remoteHost;63 private DriverService service;64 public RemoteWebDriverBuilder oneOf(Capabilities... oneOfTheseOptions) {65 options.clear();66 for (int i = 0; i < oneOfTheseOptions.length; i++) {67 addAlternative(oneOfTheseOptions[i]);68 }69 return this;70 }71 public RemoteWebDriverBuilder addAlternative(Capabilities options) {72 Map<String, Object> serialized = validate(Objects.requireNonNull(options));73 this.options.add(serialized);74 return this;75 }76 public RemoteWebDriverBuilder addMetadata(String key, Object value) {77 if (ILLEGAL_METADATA_KEYS.contains(key)) {78 throw new IllegalArgumentException(key + " is a reserved key");79 }80 metadata.put(Objects.requireNonNull(key), Objects.requireNonNull(value));81 return this;82 }83 public RemoteWebDriverBuilder setCapability(String capabilityName, String value) {84 if (!OK_KEYS.test(capabilityName)) {85 throw new IllegalArgumentException("Capability is not valid");86 }87 if (value == null) {88 throw new IllegalArgumentException("Null values are not allowed");89 }90 additionalCapabilities.put(capabilityName, value);91 return this;92 }93 public RemoteWebDriverBuilder url(String url) {94 try {95 return url(new URL(url));96 } catch (MalformedURLException e) {97 throw new UncheckedIOException(e);98 }99 }100 public RemoteWebDriverBuilder url(URL url) {101 this.remoteHost = Objects.requireNonNull(url);102 validateDriverServiceAndUrlConstraint();103 return this;104 }105 public RemoteWebDriver build() {106 if (options.isEmpty()) {107 throw new SessionNotCreatedException("Refusing to create session without any capabilities");108 }109 Plan plan = getPlan();110 CommandExecutor executor;111 if (plan.isUsingDriverService()) {112 AtomicReference<DriverService> serviceRef = new AtomicReference<>();113 executor = new SpecCompliantExecutor(114 () -> {115 if (serviceRef.get() != null && serviceRef.get().isRunning()) {116 throw new SessionNotCreatedException(117 "Attempt to start the underlying service more than once");118 }119 try {120 serviceRef.set(plan.getDriverService());121 serviceRef.get().start();122 return serviceRef.get().getUrl();123 } catch (IOException e) {124 throw new SessionNotCreatedException(e.getMessage(), e);125 }126 },127 plan::writePayload,128 () -> serviceRef.get().stop());129 } else {130 executor = new SpecCompliantExecutor(() -> remoteHost, plan::writePayload, () -> {});131 }132 return new RemoteWebDriver(executor, new ImmutableCapabilities());133 }134 private Map<String, Object> validate(Capabilities options) {135 return options.asMap().entrySet().stream()136 // Ensure that the keys are ok137 .peek(138 entry -> {139 if (!OK_KEYS.test(entry.getKey())) {140 throw new IllegalArgumentException(141 "Capability key is not a valid w3c key: " + entry.getKey());142 }143 })144 // And remove null values, as these are ignored.145 .filter(entry -> entry.getValue() != null)146 .collect(ImmutableMap.toImmutableMap(Map.Entry::getKey, Map.Entry::getValue));147 }148 @VisibleForTesting149 Plan getPlan() {150 return new Plan();151 }152 public RemoteWebDriverBuilder withDriverService(DriverService service) {153 this.service = Objects.requireNonNull(service);154 validateDriverServiceAndUrlConstraint();155 return this;156 }157 private void validateDriverServiceAndUrlConstraint() {158 if (remoteHost != null && service != null) {159 throw new IllegalArgumentException(160 "You may only set one of the remote url or the driver service to use.");161 }162 }163 @VisibleForTesting164 class Plan {165 private Plan() {166 // Not for public consumption...
Source:CreateWebDriverAction.java
...22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.23 */24package com.github.grossopa.selenium.core.driver;25import org.openqa.selenium.WebDriver;26import org.openqa.selenium.remote.RemoteWebDriverBuilder;27import static org.openqa.selenium.remote.RemoteWebDriver.builder;28/**29 * Creates the {@link WebDriver} action. This method requires the {@link org.openqa.selenium.remote.service.DriverService}30 * to be created together.31 *32 * @author Jack Yin33 * @since 1.034 */35public class CreateWebDriverAction implements WebDriverType.WebDriverTypeFunction<CreateWebDriverParams, WebDriver> {36 @Override37 public WebDriver applyChrome(CreateWebDriverParams input) {38 return doBuild(getBuilder(), input);39 }40 @Override41 public WebDriver applyEdge(CreateWebDriverParams input) {42 return doBuild(getBuilder(), input);43 }44 @Override45 public WebDriver applyFirefox(CreateWebDriverParams input) {46 return doBuild(getBuilder(), input);47 }48 @Override49 public WebDriver applyIE(CreateWebDriverParams input) {50 return doBuild(getBuilder(), input);51 }52 @Override53 public WebDriver applyOpera(CreateWebDriverParams input) {54 return doBuild(getBuilder(), input);55 }56 @Override57 public WebDriver applySafari(CreateWebDriverParams input) {58 return doBuild(getBuilder(), input);59 }60 protected WebDriver doBuild(RemoteWebDriverBuilder builder, CreateWebDriverParams input) {61 return builder.addAlternative(input.getOptions()).withDriverService(input.getDriverService()).build();62 }63 /**64 * For Unit testing purpose65 *66 * @return the instance of the builder67 */68 protected RemoteWebDriverBuilder getBuilder() {69 return builder();70 }71}...
RemoteWebDriverBuilder
Using AI Code Generation
1import org.openqa.selenium.remote.RemoteWebDriverBuilder;2import org.openqa.selenium.chrome.ChromeDriverBuilder;3import org.openqa.selenium.firefox.FirefoxDriverBuilder;4import org.openqa.selenium.ie.InternetExplorerDriverBuilder;5import org.openqa.selenium.edge.EdgeDriverBuilder;6import org.openqa.selenium.safari.SafariDriverBuilder;7import org.openqa.selenium.opera.OperaDriverBuilder;8import org.openqa.selenium.phantomjs.PhantomJSDriverBuilder;9import org.openqa.selenium.android.AndroidDriverBuilder;10import org.openqa.selenium.iphone.IPhoneDriverBuilder;11import org.openqa.selenium.htmlunit.HtmlUnitDriverBuilder;12RemoteWebDriverBuilder builder = new RemoteWebDriverBuilder();13ChromeDriverBuilder builder = new ChromeDriverBuilder();14FirefoxDriverBuilder builder = new FirefoxDriverBuilder();15InternetExplorerDriverBuilder builder = new InternetExplorerDriverBuilder();16EdgeDriverBuilder builder = new EdgeDriverBuilder();17SafariDriverBuilder builder = new SafariDriverBuilder();18OperaDriverBuilder builder = new OperaDriverBuilder();19PhantomJSDriverBuilder builder = new PhantomJSDriverBuilder();
RemoteWebDriverBuilder
Using AI Code Generation
1import org.openqa.selenium.remote.RemoteWebDriverBuilder;2import org.openqa.selenium.remote.DesiredCapabilities;3import org.openqa.selenium.remote.RemoteWebDriver;4import org.openqa.selenium.chrome.ChromeDriver;5import org.openqa.selenium.chrome.ChromeOptions;6import org.openqa.selenium.remote.RemoteWebDriverBuilder;7import org.openqa.selenium.remote.DesiredCapabilities;8import org.openqa.selenium.remote.RemoteWebDriver;9import org.openqa.selenium.firefox.FirefoxDriver;10import org.openqa.selenium.firefox.FirefoxOptions;11import org.openqa.selenium.remote.RemoteWebDriverBuilder;12import org.openqa.selenium.remote.DesiredCapabilities;13import org.openqa.selenium.remote.RemoteWebDriver;14import org.openqa.selenium.edge.EdgeDriver;15import org.openqa.selenium.edge.EdgeOptions;16import org.openqa.selenium.remote.RemoteWebDriverBuilder;17import org.openqa.selenium.remote.DesiredCapabilities;18import org.openqa.selenium.remote.RemoteWebDriver;19import org.openqa.selenium.ie.InternetExplorerDriver;20import org.openqa.selenium.ie.InternetExplorerOptions;21import org.openqa.selenium.remote.RemoteWebDriverBuilder;22import org.openqa.selenium.remote.DesiredCapabilities;23import org.openqa.selenium.remote.RemoteWebDriver;24import org.openqa.selenium.opera.OperaDriver;25import org.openqa.selenium.opera.OperaOptions;26import org.openqa.selenium.remote.RemoteWebDriverBuilder;27import org.openqa.selenium.remote.DesiredCapabilities;28import org.openqa.selenium.remote.RemoteWebDriver;29import org.openqa.selenium.safari.SafariDriver;30import org.openqa.selenium.safari.SafariOptions;31import org.openqa.selenium.remote.RemoteWebDriverBuilder;32import org.openqa.selenium.remote.DesiredCapabilities;33import org.openqa.selenium.remote.RemoteWebDriver;34import org.openqa.selenium.phantomjs.PhantomJSDriver;35import org.openqa.selenium.phantomjs.PhantomJSDriverService;36import org.openqa.selenium.remote.RemoteWebDriverBuilder;37import org.openqa.selenium.remote.DesiredCapabilities;38import org.openqa.selenium.remote.RemoteWebDriver;39import org.openqa.selenium.htmlunit.HtmlUnitDriver;40import org.openqa.selenium.htmlunit.HtmlUnitDriver;41import org.openqa
RemoteWebDriverBuilder
Using AI Code Generation
1[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ selenium ---2[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ selenium ---3[INFO] --- maven-resources-plugin:2u6:testResources (default-testRessurces) @ selenium ---4[INFO] --- maveg-compiler-plugin:3.1:testCompile (default-testCompile) @ selenium ---5[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ selenium ---6[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ selenium ---
RemoteWebDriverBuilder
Using AI Code Generation
1import org.openet() method of RemoteWebDriver class2[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ selenium ---3[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ selenium ---4[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ selenium ---5[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ selenium ---6[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ selenium ---7[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ selenium ---
RemoteWebDriverBuilder
Using AI Code Generation
1import org.openqa.selenium.remote.RemoteWebDriverBuilder;2import org.openqa.selenium.remote.RemoteWebDriverBuilder;3RemoteWebDriverBuilder builder = new RemoteWebDriverBuilder();4builder.setRemoteAddress(url);5builder.setCapabilities(capabilities);6RemoteWebDriver driver = builder.build();
LambdaTest’s Selenium 4 tutorial is covering every aspects of Selenium 4 testing with examples and best practices. Here you will learn basics, such as how to upgrade from Selenium 3 to Selenium 4, to some advanced concepts, such as Relative locators and Selenium Grid 4 for Distributed testing. Also will learn new features of Selenium 4, such as capturing screenshots of specific elements, opening a new tab or window on the browser, and new protocol adoptions.
Upgrading From Selenium 3 To Selenium 4?: In this chapter, learn in detail how to update Selenium 3 to Selenium 4 for Java binding. Also, learn how to upgrade while using different build tools such as Maven or Gradle and get comprehensive guidance for upgrading Selenium.
What’s New In Selenium 4 & What’s Being Deprecated? : Get all information about new implementations in Selenium 4, such as W3S protocol adaption, Optimized Selenium Grid, and Enhanced Selenium IDE. Also, learn what is deprecated for Selenium 4, such as DesiredCapabilites and FindsBy methods, etc.
Selenium 4 With Python: Selenium supports all major languages, such as Python, C#, Ruby, and JavaScript. In this chapter, learn how to install Selenium 4 for Python and the features of Python in Selenium 4, such as Relative locators, Browser manipulation, and Chrom DevTool protocol.
Selenium 4 Is Now W3C Compliant: JSON Wireframe protocol is retiring from Selenium 4, and they are adopting W3C protocol to learn in detail about the advantages and impact of these changes.
How To Use Selenium 4 Relative Locator? : Selenium 4 came with new features such as Relative Locators that allow constructing locators with reference and easily located constructors nearby. Get to know its different use cases with examples.
Selenium Grid 4 Tutorial For Distributed Testing: Selenium Grid 4 allows you to perform tests over different browsers, OS, and device combinations. It also enables parallel execution browser testing, reads up on various features of Selenium Grid 4 and how to download it, and runs a test on Selenium Grid 4 with best practices.
Selenium Video Tutorials: Binge on video tutorials on Selenium by industry experts to get step-by-step direction from automating basic to complex test scenarios with Selenium.
LambdaTest also provides certification for Selenium testing to accelerate your career in Selenium automation testing.
Get 100 minutes of automation test minutes FREE!!