Best Selenium code snippet using org.openqa.selenium.Interface By.Remotable
Source:AugmenterTest.java  
1// Licensed to the Software Freedom Conservancy (SFC) under one2// or more contributor license agreements.  See the NOTICE file3// distributed with this work for additional information4// regarding copyright ownership.  The SFC licenses this file5// to you under the Apache License, Version 2.0 (the6// "License"); you may not use this file except in compliance7// with the License.  You may obtain a copy of the License at8//9//   http://www.apache.org/licenses/LICENSE-2.010//11// Unless required by applicable law or agreed to in writing,12// software distributed under the License is distributed on an13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY14// KIND, either express or implied.  See the License for the15// specific language governing permissions and limitations16// under the License.17package org.openqa.selenium.remote;18import com.google.common.collect.ImmutableMap;19import org.junit.Test;20import org.junit.experimental.categories.Category;21import org.openqa.selenium.By;22import org.openqa.selenium.Capabilities;23import org.openqa.selenium.HasCapabilities;24import org.openqa.selenium.ImmutableCapabilities;25import org.openqa.selenium.NoSuchElementException;26import org.openqa.selenium.Rotatable;27import org.openqa.selenium.ScreenOrientation;28import org.openqa.selenium.SearchContext;29import org.openqa.selenium.WebDriver;30import org.openqa.selenium.WebElement;31import org.openqa.selenium.firefox.FirefoxOptions;32import org.openqa.selenium.html5.LocationContext;33import org.openqa.selenium.testing.UnitTests;34import java.util.ArrayList;35import java.util.Collections;36import java.util.HashMap;37import java.util.List;38import java.util.Map;39import static org.assertj.core.api.Assertions.assertThat;40import static org.assertj.core.api.Assertions.assertThatExceptionOfType;41import static org.mockito.Mockito.mock;42import static org.openqa.selenium.remote.DriverCommand.FIND_ELEMENT;43@Category(UnitTests.class)44public class AugmenterTest {45  private Augmenter getAugmenter() {46    return new Augmenter();47  }48  @Test49  public void shouldAugmentRotatable() {50    final Capabilities caps = new ImmutableCapabilities(CapabilityType.ROTATABLE, true);51    WebDriver driver = new RemoteWebDriver(new StubExecutor(caps), caps);52    WebDriver returned = getAugmenter().augment(driver);53    assertThat(returned).isNotSameAs(driver);54    assertThat(returned).isInstanceOf(Rotatable.class);55  }56  @Test57  public void shouldAugmentLocationContext() {58    final Capabilities caps = new ImmutableCapabilities(CapabilityType.SUPPORTS_LOCATION_CONTEXT, true);59    WebDriver driver = new RemoteWebDriver(new StubExecutor(caps), caps);60    WebDriver returned = getAugmenter().augment(driver);61    assertThat(returned).isNotSameAs(driver);62    assertThat(returned).isInstanceOf(LocationContext.class);63  }64  @Test65  public void shouldAddInterfaceFromCapabilityIfNecessary() {66    final Capabilities caps = new ImmutableCapabilities("magic.numbers", true);67    WebDriver driver = new RemoteWebDriver(new StubExecutor(caps), caps);68    WebDriver returned = getAugmenter()69      .addDriverAugmentation("magic.numbers", HasMagicNumbers.class, (c, exe) -> () -> 42)70      .augment(driver);71    assertThat(returned).isNotSameAs(driver);72    assertThat(returned).isInstanceOf(HasMagicNumbers.class);73  }74  @Test75  public void shouldNotAddInterfaceWhenBooleanValueForItIsFalse() {76    Capabilities caps = new ImmutableCapabilities("magic.numbers", false);77    WebDriver driver = new RemoteWebDriver(new StubExecutor(caps), caps);78    WebDriver returned = getAugmenter()79      .addDriverAugmentation("magic.numbers", HasMagicNumbers.class, (c, exe) -> () -> 42)80      .augment(driver);81    assertThat(returned).isSameAs(driver);82    assertThat(returned).isNotInstanceOf(HasMagicNumbers.class);83  }84  @Test85  public void shouldDelegateToHandlerIfAdded() {86    Capabilities caps = new ImmutableCapabilities("foo", true);87    WebDriver driver = new RemoteWebDriver(new StubExecutor(caps), caps);88    WebDriver returned = getAugmenter()89      .addDriverAugmentation(90        "foo",91        MyInterface.class,92        (c, exe) -> () -> "Hello World")93      .augment(driver);94    String text = ((MyInterface) returned).getHelloWorld();95    assertThat(text).isEqualTo("Hello World");96  }97  @Test98  public void shouldDelegateUnmatchedMethodCallsToDriverImplementation() {99    Capabilities caps = new ImmutableCapabilities("magic.numbers", true);100    StubExecutor stubExecutor = new StubExecutor(caps);101    stubExecutor.expect(DriverCommand.GET_TITLE, new HashMap<>(), "Title");102    WebDriver driver = new RemoteWebDriver(stubExecutor, caps);103    WebDriver returned = getAugmenter()104      .addDriverAugmentation(105        "magic.numbers",106        HasMagicNumbers.class,107        (c, exe) -> () -> 42)108      .augment(driver);109    assertThat(returned.getTitle()).isEqualTo("Title");110  }111  @Test112  public void proxyShouldNotAppearInStackTraces() {113    // This will force the class to be enhanced114    final Capabilities caps = new ImmutableCapabilities("magic.numbers", true);115    DetonatingDriver driver = new DetonatingDriver();116    driver.setCapabilities(caps);117    WebDriver returned = getAugmenter()118      .addDriverAugmentation(119        "magic.numbers",120        HasMagicNumbers.class,121        (c, exe) -> () -> 42)122      .augment(driver);123    assertThatExceptionOfType(NoSuchElementException.class)124      .isThrownBy(() -> returned.findElement(By.id("ignored")));125  }126  @Test127  public void shouldCopyFieldsFromTemplateInstanceIntoChildInstance() {128    ChildRemoteDriver driver = new ChildRemoteDriver();129    HasMagicNumbers holder = (HasMagicNumbers) getAugmenter().augment(driver);130    assertThat(holder.getMagicNumber()).isEqualTo(3);131  }132  @Test133  public void shouldNotChokeOnFinalFields() {134    WithFinals withFinals = new WithFinals();135    getAugmenter().augment(withFinals);136  }137  @Test138  public void shouldAllowReflexiveCalls() {139    Capabilities caps = new ImmutableCapabilities("find by magic", true);140    StubExecutor executor = new StubExecutor(caps);141    final WebElement element = mock(WebElement.class);142    executor.expect(143      FIND_ELEMENT,144      ImmutableMap.of("using", "magic", "value", "cheese"),145      element);146    WebDriver driver = new RemoteWebDriver(executor, caps);147    WebDriver returned = getAugmenter()148      .addDriverAugmentation(149        "find by magic",150        FindByMagic.class,151        (c, exe) -> magicWord -> element)152      .augment(driver);153    // No exception is a Good Thing154    WebElement seen = returned.findElement(new ByMagic("cheese"));155    assertThat(seen).isSameAs(element);156  }157  private static class ByMagic extends By {158    private final String magicWord;159    public ByMagic(String magicWord) {160      this.magicWord = magicWord;161    }162    @Override163    public List<WebElement> findElements(SearchContext context) {164      return Collections.singletonList(((FindByMagic) context).findByMagic(magicWord));165    }166  }167  public interface FindByMagic {168    WebElement findByMagic(String magicWord);169  }170  @Test171  public void shouldBeAbleToAugmentMultipleTimes() {172    Capabilities caps = new ImmutableCapabilities("rotatable", true, "magic.numbers", true);173    StubExecutor stubExecutor = new StubExecutor(caps);174    stubExecutor.expect(DriverCommand.GET_SCREEN_ORIENTATION,175      Collections.emptyMap(),176      ScreenOrientation.PORTRAIT.name());177    RemoteWebDriver driver = new RemoteWebDriver(stubExecutor, caps);178    WebDriver augmented = getAugmenter().augment(driver);179    assertThat(driver).isNotSameAs(augmented);180    assertThat(augmented).isInstanceOf(Rotatable.class);181    assertThat(augmented).isNotInstanceOf(HasMagicNumbers.class);182    WebDriver augmentedAgain = getAugmenter()183      .addDriverAugmentation(184        "magic.numbers",185        HasMagicNumbers.class,186        (c, exe) -> () -> 42)187      .augment(augmented);188    assertThat(augmented).isNotSameAs(augmentedAgain);189    assertThat(augmentedAgain).isInstanceOf(Rotatable.class);190    assertThat(augmentedAgain).isInstanceOf(HasMagicNumbers.class);191    ((Rotatable) augmentedAgain).getOrientation();  // Should not throw.192    assertThat(((HasCapabilities) augmentedAgain).getCapabilities())193      .isSameAs(driver.getCapabilities());194  }195  protected static class StubExecutor implements CommandExecutor {196    private final Capabilities capabilities;197    private final List<Data> expected = new ArrayList<>();198    protected StubExecutor(Capabilities capabilities) {199      this.capabilities = capabilities;200    }201    @Override202    public Response execute(Command command) {203      if (DriverCommand.NEW_SESSION.equals(command.getName())) {204        Response response = new Response(new SessionId("foo"));205        response.setValue(capabilities.asMap());206        return response;207      }208      for (Data possibleMatch : expected) {209        if (possibleMatch.commandName.equals(command.getName()) &&210          possibleMatch.args.equals(command.getParameters())) {211          Response response = new Response(new SessionId("foo"));212          response.setValue(possibleMatch.returnValue);213          return response;214        }215      }216      throw new AssertionError("Unexpected method invocation: " + command);217    }218    public void expect(String commandName, Map<String, ?> args, Object returnValue) {219      expected.add(new Data(commandName, args, returnValue));220    }221    private static class Data {222      public String commandName;223      public Map<String, ?> args;224      public Object returnValue;225      public Data(String commandName, Map<String, ?> args, Object returnValue) {226        this.commandName = commandName;227        this.args = args;228        this.returnValue = returnValue;229      }230    }231  }232  public interface MyInterface {233    String getHelloWorld();234  }235  public static class DetonatingDriver extends RemoteWebDriver {236    private Capabilities caps;237    public void setCapabilities(Capabilities caps) {238      this.caps = caps;239    }240    @Override241    public Capabilities getCapabilities() {242      return caps;243    }244    @Override245    public WebElement findElement(By locator) {246      if (locator instanceof By.Remotable) {247        if ("id".equals(((By.Remotable) locator).getRemoteParameters().using())) {248          throw new NoSuchElementException("Boom");249        }250      }251      return null;252    }253  }254  public interface HasMagicNumbers {255    int getMagicNumber();256  }257  public static class ChildRemoteDriver extends RemoteWebDriver implements HasMagicNumbers {258    private int magicNumber = 3;259    @Override260    public Capabilities getCapabilities() {261      return new FirefoxOptions();262    }263    @Override264    public int getMagicNumber() {265      return magicNumber;266    }267  }268  public static class WithFinals extends RemoteWebDriver {269    public final String finalField = "FINAL";270    @Override271    public Capabilities getCapabilities() {272      return new ImmutableCapabilities();273    }274  }275}...Interface By.Remotable
Using AI Code Generation
1import org.openqa.selenium.By;2import org.openqa.selenium.WebDriver;3import org.openqa.selenium.WebElement;4import org.openqa.selenium.chrome.ChromeDriver;5import org.openqa.selenium.support.ui.Select;6import org.openqa.selenium.support.ui.WebDriverWait;7import org.openqa.selenium.support.ui.ExpectedConditions;8import org.openqa.selenium.support.ui.ExpectedCondition;9import org.openqa.selenium.JavascriptExecutor;10import org.openqa.selenium.support.ui.WebDriverWait;11import org.openqa.selenium.support.ui.ExpectedConditions;12import org.openqa.selenium.support.ui.ExpectedCondition;13import org.openqa.selenium.By;14import org.openqa.selenium.WebDriver;15import org.openqa.selenium.WebElement;16import org.openqa.selenium.chrome.ChromeDriver;17import org.openqa.selenium.support.ui.Select;18import org.openqa.selenium.support.ui.WebDriverWait;19import org.openqa.selenium.support.ui.ExpectedConditions;20import org.openqa.selenium.support.ui.ExpectedCondition;21import org.openqa.selenium.JavascriptExecutor;22import org.openqa.selenium.support.ui.WebDriverWait;23import org.openqa.selenium.support.ui.ExpectedConditions;24import org.openqa.selenium.support.ui.ExpectedCondition;25import org.openqa.selenium.By;26import org.openqa.selenium.WebDriver;27import org.openqa.selenium.WebElement;28import org.openqa.selenium.chrome.ChromeDriver;29import org.openqa.selenium.support.ui.Select;30import org.openqa.selenium.support.ui.WebDriverWait;31import org.openqa.selenium.support.ui.ExpectedConditions;32import org.openqa.selenium.support.ui.ExpectedCondition;33import org.openqa.selenium.JavascriptExecutor;34import org.openqa.selenium.support.ui.WebDriverWait;35import org.openqa.selenium.support.ui.ExpectedConditions;36import org.openqa.selenium.support.ui.ExpectedCondition;37import org.openqa.selenium.By;38import org.openqa.selenium.WebDriver;39import org.openqa.selenium.WebElement;40import org.openqa.selenium.chrome.ChromeDriver;41import org.openqa.selenium.support.ui.Select;42import org.openqa.selenium.support.ui.WebDriverWait;43import org.openqa.selenium.support.ui.ExpectedConditions;44import org.openqa.selenium.support.ui.ExpectedCondition;45import org.openqa.selenium.JavascriptExecutor;46import org.openqa.selenium.support.ui.WebDriverWait;47import org.openqa.selenium.support.ui.ExpectedConditions;48import org.openqa.selenium.support.ui.ExpectedCondition;49import org.openqa.selenium.By;50import org.openqa.selenium.WebDriver;51import org.openqa.selenium.WebElement;52import org.openqa.selenium.chrome.ChromeDriver;53import org.openqa.selenium.support.ui.Select;54import org.openqa.selenium.support.ui.WebDriverWait;55import org.openqa.selenium.support.ui.ExpectedConditions;56import org.openqa.selenium.support.ui.ExpectedCondition;57import org.openqa.selenium.JavascriptExecutor;58import org.openqa.selenium.support.ui.WebDriverWait;59import org.openqa.selenium.support.ui.ExpectedConditions;60import org.openqa.selenium.support.ui.ExpectedInterface By.Remotable
Using AI Code Generation
1import org.openqa.selenium.By;2import org.openqa.selenium.WebDriver;3import org.openqa.selenium.WebElement;4import org.openqa.selenium.chrome.ChromeDriver;5import org.openqa.selenium.support.ui.ExpectedConditions;6import org.openqa.selenium.support.ui.WebDriverWait;7import java.util.concurrent.TimeUnit;8import java.util.*;9import java.util.List;10import java.util.Iterator;11import java.util.Date;12import java.text.DateFormat;13import java.text.SimpleDateFormat;14import java.io.File;15import java.io.IOException;16import java.io.FileWriter;17import org.openqa.selenium.support.ui.Select;18import java.io.BufferedWriter;19import java.io.PrintWriter;20import java.io.FileOutputStream;21import java.io.OutputStreamWriter;22import java.io.BufferedReader;23import java.io.InputStreamReader;24import java.io.FileInputStream;25import java.io.InputStream;26import java.net.URL;27import java.net.HttpURLConnection;28import java.net.MalformedURLException;29import java.util.HashMap;30import java.util.Map;31import java.util.Map.Entry;32import java.util.Set;33import java.util.ArrayList;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!!
