How to use DriverServiceSessionFactory class of org.openqa.selenium.grid.node.config package

Best Selenium code snippet using org.openqa.selenium.grid.node.config.DriverServiceSessionFactory

Source:DriverServiceSessionFactoryTest.java Github

copy

Full Screen

...45import static org.mockito.Mockito.verify;46import static org.mockito.Mockito.verifyNoInteractions;47import static org.mockito.Mockito.verifyNoMoreInteractions;48import static org.mockito.Mockito.when;49public class DriverServiceSessionFactoryTest {50 private Tracer tracer;51 private HttpClient.Factory clientFactory;52 private DriverService.Builder builder;53 private DriverService driverService;54 @Before55 public void setUp() throws MalformedURLException {56 tracer = DefaultTestTracer.createTracer();57 clientFactory = mock(HttpClient.Factory.class);58 driverService = mock(DriverService.class);59 when(driverService.getUrl()).thenReturn(new URL("http://localhost:1234/"));60 builder = mock(DriverService.Builder.class);61 when(builder.build()).thenReturn(driverService);62 }63 @Test64 public void itDelegatesCapabilitiesTestingToPredicate() {65 DriverServiceSessionFactory factory = factoryFor("chrome", builder);66 assertThat(factory.test(toPayload("chrome"))).isTrue();67 assertThat(factory.test(toPayload("firefox"))).isFalse();68 verifyNoInteractions(builder);69 }70 @Test71 public void shouldNotInstantiateSessionIfNoDialectSpecifiedInARequest() {72 DriverServiceSessionFactory factory = factoryFor("chrome", builder);73 Optional<ActiveSession> session = factory.apply(new CreateSessionRequest(74 ImmutableSet.of(), toPayload("chrome"), ImmutableMap.of()));75 assertThat(session).isEmpty();76 verifyNoInteractions(builder);77 }78 @Test79 public void shouldNotInstantiateSessionIfCapabilitiesDoNotMatch() {80 DriverServiceSessionFactory factory = factoryFor("chrome", builder);81 Optional<ActiveSession> session = factory.apply(new CreateSessionRequest(82 ImmutableSet.of(Dialect.W3C), toPayload("firefox"), ImmutableMap.of()));83 assertThat(session).isEmpty();84 verifyNoInteractions(builder);85 }86 @Test87 public void shouldNotInstantiateSessionIfBuilderCanNotBuildService() {88 when(builder.build()).thenThrow(new WebDriverException());89 DriverServiceSessionFactory factory = factoryFor("chrome", builder);90 Optional<ActiveSession> session = factory.apply(new CreateSessionRequest(91 ImmutableSet.of(Dialect.W3C), toPayload("chrome"), ImmutableMap.of()));92 assertThat(session).isEmpty();93 verify(builder, times(1)).build();94 verifyNoMoreInteractions(builder);95 }96 @Test97 public void shouldNotInstantiateSessionIfRemoteEndReturnsInvalidResponse() throws IOException {98 HttpClient httpClient = mock(HttpClient.class);99 when(httpClient.execute(any(HttpRequest.class))).thenReturn(100 new HttpResponse().setStatus(200).setContent(() -> new ByteArrayInputStream(101 "Hello, world!".getBytes())));102 when(clientFactory.createClient(any(URL.class))).thenReturn(httpClient);103 DriverServiceSessionFactory factory = factoryFor("chrome", builder);104 Optional<ActiveSession> session = factory.apply(new CreateSessionRequest(105 ImmutableSet.of(Dialect.W3C), toPayload("chrome"), ImmutableMap.of()));106 assertThat(session).isEmpty();107 verify(builder, times(1)).build();108 verifyNoMoreInteractions(builder);109 verify(driverService, times(1)).start();110 verify(driverService, atLeastOnce()).getUrl();111 verify(driverService, times(1)).stop();112 verifyNoMoreInteractions(driverService);113 }114 @Test115 public void shouldInstantiateSessionIfEverythingIsOK() throws IOException {116 HttpClient httpClient = mock(HttpClient.class);117 when(httpClient.execute(any(HttpRequest.class))).thenReturn(118 new HttpResponse().setStatus(200).setContent(() -> new ByteArrayInputStream(119 "{ \"value\": { \"sessionId\": \"1\", \"capabilities\": {} } }".getBytes())));120 when(clientFactory.createClient(any(URL.class))).thenReturn(httpClient);121 DriverServiceSessionFactory factory = factoryFor("chrome", builder);122 Optional<ActiveSession> session = factory.apply(new CreateSessionRequest(123 ImmutableSet.of(Dialect.W3C), toPayload("chrome"), ImmutableMap.of()));124 assertThat(session).isNotEmpty();125 verify(builder, times(1)).build();126 verifyNoMoreInteractions(builder);127 verify(driverService, times(1)).start();128 verify(driverService, atLeastOnce()).getUrl();129 verifyNoMoreInteractions(driverService);130 }131 private DriverServiceSessionFactory factoryFor(String browser, DriverService.Builder builder) {132 Predicate<Capabilities> predicate = c -> c.getBrowserName().equals(browser);133 return new DriverServiceSessionFactory(tracer, clientFactory, predicate, builder);134 }135 private Capabilities toPayload(String browserName) {136 return new ImmutableCapabilities("browserName", browserName);137 }138}...

Full Screen

Full Screen

Source:LocalNodeFactory.java Github

copy

Full Screen

...22import org.openqa.selenium.grid.docker.DockerOptions;23import org.openqa.selenium.grid.log.LoggingOptions;24import org.openqa.selenium.grid.node.Node;25import org.openqa.selenium.grid.node.SessionFactory;26import org.openqa.selenium.grid.node.config.DriverServiceSessionFactory;27import org.openqa.selenium.grid.node.config.NodeOptions;28import org.openqa.selenium.grid.server.BaseServerOptions;29import org.openqa.selenium.grid.server.EventBusOptions;30import org.openqa.selenium.grid.server.NetworkOptions;31import org.openqa.selenium.remote.http.HttpClient;32import org.openqa.selenium.remote.service.DriverService;33import org.openqa.selenium.remote.tracing.Tracer;34import java.util.ArrayList;35import java.util.Collection;36import java.util.List;37import java.util.ServiceLoader;38public class LocalNodeFactory {39 public static Node create(Config config) {40 LoggingOptions loggingOptions = new LoggingOptions(config);41 EventBusOptions eventOptions = new EventBusOptions(config);42 BaseServerOptions serverOptions = new BaseServerOptions(config);43 NodeOptions nodeOptions = new NodeOptions(config);44 NetworkOptions networkOptions = new NetworkOptions(config);45 Tracer tracer = loggingOptions.getTracer();46 HttpClient.Factory clientFactory = networkOptions.getHttpClientFactory(tracer);47 LocalNode.Builder builder = LocalNode.builder(48 tracer,49 eventOptions.getEventBus(),50 serverOptions.getExternalUri(),51 nodeOptions.getPublicGridUri().orElseGet(serverOptions::getExternalUri),52 serverOptions.getRegistrationSecret());53 List<DriverService.Builder<?, ?>> builders = new ArrayList<>();54 ServiceLoader.load(DriverService.Builder.class).forEach(builders::add);55 nodeOptions.getSessionFactories(info -> createSessionFactory(tracer, clientFactory, builders, info))56 .forEach((caps, factories) -> factories.forEach(factory -> builder.add(caps, factory)));57 new DockerOptions(config).getDockerSessionFactories(tracer, clientFactory)58 .forEach((caps, factories) -> factories.forEach(factory -> builder.add(caps, factory)));59 return builder.build();60 }61 private static Collection<SessionFactory> createSessionFactory(62 Tracer tracer,63 HttpClient.Factory clientFactory,64 List<DriverService.Builder<?, ?>> builders,65 WebDriverInfo info) {66 ImmutableList.Builder<SessionFactory> toReturn = ImmutableList.builder();67 Capabilities caps = info.getCanonicalCapabilities();68 builders.stream()69 .filter(builder -> builder.score(caps) > 0)70 .forEach(builder -> {71 DriverService.Builder<?, ?> freePortBuilder = builder.usingAnyFreePort();72 toReturn.add(new DriverServiceSessionFactory(73 tracer,74 clientFactory,75 c -> freePortBuilder.score(c) > 0,76 freePortBuilder));77 });78 return toReturn.build();79 }80}...

Full Screen

Full Screen

Source:DriverServiceSessionFactory.java Github

copy

Full Screen

...33import java.util.Objects;34import java.util.Optional;35import java.util.Set;36import java.util.function.Predicate;37public class DriverServiceSessionFactory implements SessionFactory {38 private final HttpClient.Factory clientFactory;39 private final Predicate<Capabilities> predicate;40 private final DriverService.Builder builder;41 public DriverServiceSessionFactory(42 HttpClient.Factory clientFactory,43 Predicate<Capabilities> predicate,44 DriverService.Builder builder) {45 this.clientFactory = Objects.requireNonNull(clientFactory);46 this.predicate = Objects.requireNonNull(predicate);47 this.builder = Objects.requireNonNull(builder);48 }49 @Override50 public boolean test(Capabilities capabilities) {51 return predicate.test(capabilities);52 }53 @Override54 public Optional<ActiveSession> apply(CreateSessionRequest sessionRequest) {55 if (sessionRequest.getDownstreamDialects().isEmpty()) {...

Full Screen

Full Screen

Source:NodeOptions.java Github

copy

Full Screen

...60 DriverService.Builder freePortBuilder = builder.usingAnyFreePort();61 for (int i = 0; i < info.getMaximumSimultaneousSessions(); i++) {62 node.add(63 caps,64 new DriverServiceSessionFactory(65 clientFactory, c -> freePortBuilder.score(c) > 0,66 freePortBuilder));67 }68 });69 });70 }71}...

Full Screen

Full Screen

Selenium 4 Tutorial:

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

Chapters:

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

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

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

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

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

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

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

Selenium 101 certifications:

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

Run Selenium automation tests on LambdaTest cloud grid

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

Most used methods in DriverServiceSessionFactory

Test Your Web Or Mobile Apps On 3000+ Browsers

Signup for free

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful