How to use apply method of org.openqa.selenium.remote.session.ProxyTransform class

Best Selenium code snippet using org.openqa.selenium.remote.session.ProxyTransform.apply

Source:NewAppiumSessionPayload.java Github

copy

Full Screen

...283 // And now W3C284 Stream<Map<String, Object>> w3c = getW3C();285 return Stream.concat(oss, w3c)286 .filter(Objects::nonNull)287 .map(this::applyTransforms)288 .filter(Objects::nonNull)289 .distinct()290 .map(ImmutableCapabilities::new);291 }292 @Override293 public void close() throws IOException {294 backingStore.reset();295 }296 private @Nullable Map<String, Object> getOss() throws IOException {297 CharSource charSource = backingStore.asByteSource().asCharSource(UTF_8);298 try (Reader reader = charSource.openBufferedStream();299 JsonInput input = json.newInput(reader)) {300 input.beginObject();301 while (input.hasNext()) {302 String name = input.nextName();303 if (DESIRED_CAPABILITIES.equals(name)) {304 return input.read(MAP_TYPE);305 }306 input.skipValue();307 }308 }309 return null;310 }311 private Stream<Map<String, Object>> getW3C() throws IOException {312 // If there's an OSS value, generate a stream of capabilities from that using the transforms,313 // then add magic to generate each of the w3c capabilities. For the sake of simplicity, we're314 // going to make the (probably wrong) assumption we can hold all of the firstMatch values and315 // alwaysMatch value in memory at the same time.316 Map<String, Object> oss = convertOssToW3C(getOss());317 Stream<Map<String, Object>> fromOss;318 if (oss != null) {319 Set<String> usedKeys = new HashSet<>();320 // Are there any values we care want to pull out into a mapping of their own?321 List<Map<String, Object>> firsts = adapters.stream()322 .map(adapter -> adapter.apply(oss))323 .filter(Objects::nonNull)324 .filter(map -> !map.isEmpty())325 .map(map ->326 map.entrySet().stream()327 .filter(entry -> entry.getKey() != null)328 .filter(entry -> ACCEPTED_W3C_PATTERNS.test(entry.getKey()))329 .filter(entry -> entry.getValue() != null)330 .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)))331 .peek(map -> usedKeys.addAll(map.keySet()))332 .collect(ImmutableList.toImmutableList());333 if (firsts.isEmpty()) {334 firsts = ImmutableList.of(of());335 }336 // Are there any remaining unused keys?337 Map<String, Object> always = oss.entrySet().stream()338 .filter(entry -> !usedKeys.contains(entry.getKey()))339 .filter(entry -> entry.getValue() != null)340 .collect(toImmutableMap(Map.Entry::getKey, Map.Entry::getValue));341 // Firsts contains at least one entry, always contains everything else. Let's combine them342 // into the stream to form a unified set of capabilities. Woohoo!343 fromOss = firsts.stream()344 .map(first -> ImmutableMap.<String, Object>builder().putAll(always).putAll(first).build())345 .map(this::applyTransforms)346 .map(map -> map.entrySet().stream()347 .filter(entry -> !forceMobileJSONWP || ACCEPTED_W3C_PATTERNS.test(entry.getKey()))348 .map((Function<Map.Entry<String, Object>, Map.Entry<String, Object>>) stringObjectEntry ->349 new Map.Entry<String, Object>() {350 @Override351 public String getKey() {352 String key = stringObjectEntry.getKey();353 if (APPIUM_CAPABILITIES.contains(key) && !forceMobileJSONWP) {354 return APPIUM_PREFIX + key;355 }356 return key;357 }358 @Override359 public Object getValue() {360 return stringObjectEntry.getValue();361 }362 @Override363 public Object setValue(Object value) {364 return stringObjectEntry.setValue(value);365 }366 })367 .collect(toImmutableMap(Map.Entry::getKey, Map.Entry::getValue)))368 .map(map -> map);369 } else {370 fromOss = Stream.of();371 }372 Stream<Map<String, Object>> fromW3c;373 Map<String, Object> alwaysMatch = getAlwaysMatch();374 Collection<Map<String, Object>> firsts = getFirstMatch();375 if (alwaysMatch == null && firsts == null) {376 fromW3c = Stream.of(); // No W3C capabilities.377 } else {378 if (alwaysMatch == null) {379 alwaysMatch = of();380 }381 Map<String, Object> always = alwaysMatch; // Keep the comoiler happy.382 if (firsts == null) {383 firsts = ImmutableList.of(of());384 }385 fromW3c = firsts.stream()386 .map(first -> ImmutableMap.<String, Object>builder().putAll(always).putAll(first).build());387 }388 return Stream.concat(fromOss, fromW3c).distinct();389 }390 private @Nullable Map<String, Object> convertOssToW3C(Map<String, Object> capabilities) {391 if (capabilities == null) {392 return null;393 }394 Map<String, Object> toReturn = new TreeMap<>(capabilities);395 // Platform name396 if (capabilities.containsKey(PLATFORM) && !capabilities.containsKey(PLATFORM_NAME)) {397 toReturn.put(PLATFORM_NAME, String.valueOf(capabilities.get(PLATFORM)));398 }399 return toReturn;400 }401 private @Nullable Map<String, Object> getAlwaysMatch() throws IOException {402 CharSource charSource = backingStore.asByteSource().asCharSource(UTF_8);403 try (Reader reader = charSource.openBufferedStream();404 JsonInput input = json.newInput(reader)) {405 input.beginObject();406 while (input.hasNext()) {407 String name = input.nextName();408 if (CAPABILITIES.equals(name)) {409 input.beginObject();410 while (input.hasNext()) {411 name = input.nextName();412 if (ALWAYS_MATCH.equals(name)) {413 return input.read(MAP_TYPE);414 }415 input.skipValue();416 }417 input.endObject();418 } else {419 input.skipValue();420 }421 }422 }423 return null;424 }425 private @Nullable Collection<Map<String, Object>> getFirstMatch() throws IOException {426 CharSource charSource = backingStore.asByteSource().asCharSource(UTF_8);427 try (Reader reader = charSource.openBufferedStream();428 JsonInput input = json.newInput(reader)) {429 input.beginObject();430 while (input.hasNext()) {431 String name = input.nextName();432 if (CAPABILITIES.equals(name)) {433 input.beginObject();434 while (input.hasNext()) {435 name = input.nextName();436 if (FIRST_MATCH.equals(name)) {437 return input.read(LIST_OF_MAPS_TYPE);438 }439 input.skipValue();440 }441 input.endObject();442 } else {443 input.skipValue();444 }445 }446 }447 return null;448 }449 private Map<String, Object> applyTransforms(Map<String, Object> caps) {450 Queue<Map.Entry<String, Object>> toExamine = new LinkedList<>(caps.entrySet());451 Set<String> seenKeys = new HashSet<>();452 Map<String, Object> toReturn = new TreeMap<>();453 // Take each entry and apply the transforms454 while (!toExamine.isEmpty()) {455 Map.Entry<String, Object> entry = toExamine.remove();456 seenKeys.add(entry.getKey());457 if (entry.getValue() == null) {458 continue;459 }460 for (CapabilityTransform transform : transforms) {461 Collection<Map.Entry<String, Object>> result = transform.apply(entry);462 if (result == null) {463 toReturn.remove(entry.getKey());464 break;465 }466 for (Map.Entry<String, Object> newEntry : result) {467 if (!seenKeys.contains(newEntry.getKey())) {468 toExamine.add(newEntry);469 continue;470 }471 if (newEntry.getKey().equals(entry.getKey())) {472 entry = newEntry;473 }474 toReturn.put(newEntry.getKey(), newEntry.getValue());475 }...

Full Screen

Full Screen

Source:NewSessionPayload.java Github

copy

Full Screen

...256 // And now W3C257 Stream<Map<String, Object>> w3c = getW3C();258 return Stream.concat(oss, w3c)259 .filter(Objects::nonNull)260 .map(this::applyTransforms)261 .filter(Objects::nonNull)262 .distinct()263 .map(ImmutableCapabilities::new);264 }265 public ImmutableSet<Dialect> getDownstreamDialects() {266 return dialects.isEmpty() ? ImmutableSet.of(DEFAULT_DIALECT) : dialects;267 }268 @Override269 public void close() throws IOException {270 backingStore.reset();271 }272 private Map<String, Object> getOss() throws IOException {273 CharSource charSource = backingStore.asByteSource().asCharSource(UTF_8);274 try (Reader reader = charSource.openBufferedStream();275 JsonInput input = json.newInput(reader)) {276 input.beginObject();277 while (input.hasNext()) {278 String name = input.nextName();279 if ("desiredCapabilities".equals(name)) {280 return input.read(MAP_TYPE);281 } else {282 input.skipValue();283 }284 }285 }286 return null;287 }288 private Stream<Map<String, Object>> getW3C() throws IOException {289 // If there's an OSS value, generate a stream of capabilities from that using the transforms,290 // then add magic to generate each of the w3c capabilities. For the sake of simplicity, we're291 // going to make the (probably wrong) assumption we can hold all of the firstMatch values and292 // alwaysMatch value in memory at the same time.293 Map<String, Object> oss = convertOssToW3C(getOss());294 Stream<Map<String, Object>> fromOss;295 if (oss != null) {296 Set<String> usedKeys = new HashSet<>();297 // Are there any values we care want to pull out into a mapping of their own?298 List<Map<String, Object>> firsts = adapters.stream()299 .map(adapter -> adapter.apply(oss))300 .filter(Objects::nonNull)301 .filter(map -> !map.isEmpty())302 .map(map ->303 map.entrySet().stream()304 .filter(entry -> entry.getKey() != null)305 .filter(entry -> ACCEPTED_W3C_PATTERNS.test(entry.getKey()))306 .filter(entry -> entry.getValue() != null)307 .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)))308 .peek(map -> usedKeys.addAll(map.keySet()))309 .collect(ImmutableList.toImmutableList());310 if (firsts.isEmpty()) {311 firsts = ImmutableList.of(ImmutableMap.of());312 }313 // Are there any remaining unused keys?314 Map<String, Object> always = oss.entrySet().stream()315 .filter(entry -> !usedKeys.contains(entry.getKey()))316 .filter(entry -> entry.getValue() != null)317 .collect(ImmutableMap.toImmutableMap(Map.Entry::getKey, Map.Entry::getValue));318 // Firsts contains at least one entry, always contains everything else. Let's combine them319 // into the stream to form a unified set of capabilities. Woohoo!320 fromOss = firsts.stream()321 .map(first -> ImmutableMap.<String, Object>builder().putAll(always).putAll(first).build())322 .map(this::applyTransforms)323 .map(map -> map.entrySet().stream()324 .filter(entry -> ACCEPTED_W3C_PATTERNS.test(entry.getKey()))325 .collect(ImmutableMap.toImmutableMap(Map.Entry::getKey, Map.Entry::getValue)));326 } else {327 fromOss = Stream.of();328 }329 Stream<Map<String, Object>> fromW3c;330 Map<String, Object> alwaysMatch = getAlwaysMatch();331 Collection<Map<String, Object>> firsts = getFirstMatches();332 if (alwaysMatch == null && firsts == null) {333 fromW3c = Stream.of(); // No W3C capabilities.334 } else {335 if (alwaysMatch == null) {336 alwaysMatch = ImmutableMap.of();337 }338 Map<String, Object> always = alwaysMatch; // Keep the comoiler happy.339 if (firsts == null) {340 firsts = ImmutableList.of(ImmutableMap.of());341 }342 fromW3c = firsts.stream()343 .map(first -> ImmutableMap.<String, Object>builder().putAll(always).putAll(first).build());344 }345 return Stream.concat(fromOss, fromW3c).distinct();346 }347 private Map<String, Object> convertOssToW3C(Map<String, Object> capabilities) {348 if (capabilities == null) {349 return null;350 }351 Map<String, Object> toReturn = new TreeMap<>();352 toReturn.putAll(capabilities);353 // Platform name354 if (capabilities.containsKey(PLATFORM) && !capabilities.containsKey(PLATFORM_NAME)) {355 toReturn.put(PLATFORM_NAME, String.valueOf(capabilities.get(PLATFORM)));356 }357 return toReturn;358 }359 private Map<String, Object> getAlwaysMatch() throws IOException {360 CharSource charSource = backingStore.asByteSource().asCharSource(UTF_8);361 try (Reader reader = charSource.openBufferedStream();362 JsonInput input = json.newInput(reader)) {363 input.beginObject();364 while (input.hasNext()) {365 String name = input.nextName();366 if ("capabilities".equals(name)) {367 input.beginObject();368 while (input.hasNext()) {369 name = input.nextName();370 if ("alwaysMatch".equals(name)) {371 return input.read(MAP_TYPE);372 } else {373 input.skipValue();374 }375 }376 input.endObject();377 } else {378 input.skipValue();379 }380 }381 }382 return null;383 }384 private Collection<Map<String, Object>> getFirstMatches() throws IOException {385 CharSource charSource = backingStore.asByteSource().asCharSource(UTF_8);386 try (Reader reader = charSource.openBufferedStream();387 JsonInput input = json.newInput(reader)) {388 input.beginObject();389 while (input.hasNext()) {390 String name = input.nextName();391 if ("capabilities".equals(name)) {392 input.beginObject();393 while (input.hasNext()) {394 name = input.nextName();395 if ("firstMatch".equals(name)) {396 return input.read(LIST_OF_MAPS_TYPE);397 } else {398 input.skipValue();399 }400 }401 input.endObject();402 } else {403 input.skipValue();404 }405 }406 }407 return null;408 }409 private Map<String, Object> applyTransforms(Map<String, Object> caps) {410 Queue<Map.Entry<String, Object>> toExamine = new LinkedList<>();411 toExamine.addAll(caps.entrySet());412 Set<String> seenKeys = new HashSet<>();413 Map<String, Object> toReturn = new TreeMap<>();414 // Take each entry and apply the transforms415 while (!toExamine.isEmpty()) {416 Map.Entry<String, Object> entry = toExamine.remove();417 seenKeys.add(entry.getKey());418 if (entry.getValue() == null) {419 continue;420 }421 for (CapabilityTransform transform : transforms) {422 Collection<Map.Entry<String, Object>> result = transform.apply(entry);423 if (result == null) {424 toReturn.remove(entry.getKey());425 break;426 }427 for (Map.Entry<String, Object> newEntry : result) {428 if (!seenKeys.contains(newEntry.getKey())) {429 toExamine.add(newEntry);430 } else {431 if (newEntry.getKey().equals(entry.getKey())) {432 entry = newEntry;433 }434 toReturn.put(newEntry.getKey(), newEntry.getValue());435 }436 }...

Full Screen

Full Screen

Source:CapabilitiesUtils.java Github

copy

Full Screen

...71 Stream<Map<String, Object>> fromOss;72 Set<String> usedKeys = new HashSet<>();73 // Are there any values we care want to pull out into a mapping of their own?74 List<Map<String, Object>> firsts = adapters.stream()75 .map(adapter -> adapter.apply(oss))76 .filter(Objects::nonNull)77 .filter(map -> !map.isEmpty())78 .map(79 map -> map.entrySet().stream()80 .filter(entry -> entry.getKey() != null)81 .filter(entry -> ACCEPTED_W3C_PATTERNS.test(entry.getKey()))82 .filter(entry -> entry.getValue() != null)83 .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)))84 .peek(map -> usedKeys.addAll(map.keySet()))85 .collect(ImmutableList.toImmutableList());86 if (firsts.isEmpty()) {87 firsts = ImmutableList.of(ImmutableMap.of());88 }89 // Are there any remaining unused keys?90 Map<String, Object> always = oss.entrySet().stream()91 .filter(entry -> !usedKeys.contains(entry.getKey()))92 .filter(entry -> entry.getValue() != null)93 .collect(ImmutableMap.toImmutableMap(Map.Entry::getKey, Map.Entry::getValue));94 // Firsts contains at least one entry, always contains everything else. Let's combine them95 // into the stream to form a unified set of capabilities. Woohoo!96 fromOss = firsts.stream()97 .map(first -> ImmutableMap.<String, Object>builder().putAll(always).putAll(first).build())98 .map(CapabilitiesUtils::applyTransforms)99 .map(map -> map.entrySet().stream()100 .filter(entry -> ACCEPTED_W3C_PATTERNS.test(entry.getKey()))101 .collect(ImmutableMap.toImmutableMap(Map.Entry::getKey, Map.Entry::getValue)));102 return fromOss;103 }104 private static Map<String, Object> convertOssToW3C(Map<String, Object> capabilities) {105 Map<String, Object> toReturn = new TreeMap<>(capabilities);106 // Platform name107 if (capabilities.containsKey(PLATFORM) && !capabilities.containsKey(PLATFORM_NAME)) {108 toReturn.put(PLATFORM_NAME, String.valueOf(capabilities.get(PLATFORM)));109 }110 if (capabilities.containsKey(PROXY)) {111 Map<String, Object> proxyMap = getProxyFromCapabilities(capabilities);112 if (proxyMap.containsKey("noProxy")) {113 Map<String, Object> w3cProxyMap = new HashMap<>(proxyMap);114 Object rawData = proxyMap.get("noProxy");115 if (rawData instanceof String) {116 w3cProxyMap.put("noProxy", Arrays.asList(((String) rawData).split(",\\s*")));117 }118 toReturn.put(CapabilityType.PROXY, w3cProxyMap);119 }120 }121 return toReturn;122 }123 private static Map<String, Object> getProxyFromCapabilities(Map<String, Object> capabilities) {124 Object rawProxy = capabilities.get(CapabilityType.PROXY);125 if (rawProxy instanceof Proxy) {126 return ((Proxy) rawProxy).toJson();127 } else if (rawProxy instanceof Map) {128 //noinspection unchecked129 return (Map<String, Object>) rawProxy;130 } else {131 return new HashMap<>();132 }133 }134 private static Map<String, Object> applyTransforms(Map<String, Object> caps) {135 Queue<Map.Entry<String, Object>> toExamine = new LinkedList<>(caps.entrySet());136 Set<String> seenKeys = new HashSet<>();137 Map<String, Object> toReturn = new TreeMap<>();138 Set<CapabilityTransform> transforms = getCapabilityTransforms();139 // Take each entry and apply the transforms140 while (!toExamine.isEmpty()) {141 Map.Entry<String, Object> entry = toExamine.remove();142 seenKeys.add(entry.getKey());143 if (entry.getValue() == null) {144 continue;145 }146 for (CapabilityTransform transform : transforms) {147 Collection<Map.Entry<String, Object>> result = transform.apply(entry);148 if (result == null) {149 toReturn.remove(entry.getKey());150 break;151 }152 for (Map.Entry<String, Object> newEntry : result) {153 if (!seenKeys.contains(newEntry.getKey())) {154 toExamine.add(newEntry);155 } else {156 if (newEntry.getKey().equals(entry.getKey())) {157 entry = newEntry;158 }159 toReturn.put(newEntry.getKey(), newEntry.getValue());160 }161 }...

Full Screen

Full Screen

apply

Using AI Code Generation

copy

Full Screen

1import org.openqa.selenium.remote.session.ProxyTransform;2import org.openqa.selenium.Proxy;3import java.util.Map;4Map<String, Object> proxyMap = new HashMap<>();5proxyMap.put("proxyType", "manual");6proxyMap.put("httpProxy", "localhost:8080");7proxyMap.put("ftpProxy", "localhost:8080");8proxyMap.put("sslProxy", "localhost:8080");9proxyMap.put("noProxy", "localhost,

Full Screen

Full Screen

apply

Using AI Code Generation

copy

Full Screen

1Proxy proxy = new Proxy();2proxy.setAutodetect(true);3proxy.setHttpProxy("localhost:8080");4proxy.setSslProxy("localhost:8081");5proxy.setFtpProxy("localhost:8082");6proxy.setSocksProxy("localhost:8083");7proxy.setSocksUsername("username");8proxy.setSocksPassword("password");9proxy.setNoProxy("localhost,

Full Screen

Full Screen

apply

Using AI Code Generation

copy

Full Screen

1package com.zetcode;2import java.net.MalformedURLException;3import java.net.URL;4import org.openqa.selenium.Proxy;5import org.openqa.selenium.remote.CapabilityType;6import org.openqa.selenium.remote.DesiredCapabilities;7import org.openqa.selenium.remote.RemoteWebDriver;8import org.openqa.selenium.remote.session.ProxyTransform;9public class SeleniumProxyEx {10 public static void main(String[] args) throws MalformedURLException {11 Proxy proxy = new Proxy();12 proxy.setHttpProxy("proxy.server:8080");13 proxy.setSslProxy("proxy.server:8080");14 DesiredCapabilities capabilities = new DesiredCapabilities();15 capabilities.setCapability(CapabilityType.PROXY, proxy);16 var proxyTransform = new ProxyTransform();17 var newProxy = proxyTransform.apply(proxy, capabilities);18 capabilities.setCapability(CapabilityType.PROXY, newProxy);19 capabilities);20 }21}22package com.zetcode;23import java.net.MalformedURLException;24import java.net.URL;25import org.openqa.selenium.Proxy;26import org.openqa.selenium.remote.CapabilityType;27import org.openqa.selenium.remote.DesiredCapabilities;28import org.openqa.selenium.remote.RemoteWebDriver;29import org.openqa.selenium.remote.session.ProxyTransform;30public class SeleniumProxyEx {31 public static void main(String[] args) throws MalformedURLException {32 Proxy proxy = new Proxy();33 proxy.setHttpProxy("proxy.server:8080

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 method in ProxyTransform

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful