How to use getOriginal method of org.openqa.selenium.support.decorators.Interface Decorated class

Best Selenium code snippet using org.openqa.selenium.support.decorators.Interface Decorated.getOriginal

Source:WebDriverDecorator.java Github

copy

Full Screen

...121 * @Override122 * public void beforeCall(Method method, Object[] args) {123 * String methodName = method.getName();124 * if ("click".equals(methodName) || "sendKeys".equals(methodName)) {125 * wait.until(d -> getOriginal().isDisplayed());126 * }127 * }128 * };129 * }130 * }131 * </code>132 * This class is not a pure decorator, it allows to not only add new behavior133 * but also replace "normal" behavior of a WebDriver or derived objects.134 * <p>135 * Let's suppose you want to use JavaScript-powered clicks instead of normal136 * ones (yes, this allows to interact with invisible elements, it's a bad137 * practice in general but sometimes it's inevitable). This behavior change138 * can be achieved with the following "decorator":139 * <code>140 * public class JavaScriptPoweredDecorator extends WebDriverDecorator {141 * @Override142 * public Decorated<WebElement> createDecorated(WebElement original) {143 * return new DefaultDecorated<>(original, this) {144 * @Override145 * public Object call(Method method, Object[] args) throws Throwable {146 * String methodName = method.getName();147 * if ("click".equals(methodName)) {148 * JavascriptExecutor executor = (JavascriptExecutor) getDecoratedDriver().getOriginal();149 * executor.executeScript("arguments[0].click()", getOriginal());150 * return null;151 * } else {152 * return super.call(method, args);153 * }154 * }155 * };156 * }157 * }158 * </code>159 * It is possible to apply multiple decorators to compose behaviors added160 * by each of them. For example, if you want to log method calls and161 * implicitly wait for elements visibility you can use two decorators:162 * <code>163 * WebDriver original = new FirefoxDriver();164 * WebDriver decorated =165 * new ImplicitlyWaitingDecorator().decorate(166 * new LoggingDecorator().decorate(original));167 * </code>168 */169@Beta170public class WebDriverDecorator {171 private Decorated<WebDriver> decorated;172 public final WebDriver decorate(WebDriver original) {173 Require.nonNull("WebDriver", original);174 decorated = createDecorated(original);175 return createProxy(decorated);176 }177 public Decorated<WebDriver> getDecoratedDriver() {178 return decorated;179 }180 public Decorated<WebDriver> createDecorated(WebDriver driver) {181 return new DefaultDecorated<>(driver, this);182 }183 public Decorated<WebElement> createDecorated(WebElement original) {184 return new DefaultDecorated<>(original, this);185 }186 public Decorated<WebDriver.TargetLocator> createDecorated(WebDriver.TargetLocator original) {187 return new DefaultDecorated<>(original, this);188 }189 public Decorated<WebDriver.Navigation> createDecorated(WebDriver.Navigation original) {190 return new DefaultDecorated<>(original, this);191 }192 public Decorated<WebDriver.Options> createDecorated(WebDriver.Options original) {193 return new DefaultDecorated<>(original, this);194 }195 public Decorated<WebDriver.Timeouts> createDecorated(WebDriver.Timeouts original) {196 return new DefaultDecorated<>(original, this);197 }198 public Decorated<WebDriver.Window> createDecorated(WebDriver.Window original) {199 return new DefaultDecorated<>(original, this);200 }201 public Decorated<Alert> createDecorated(Alert original) {202 return new DefaultDecorated<>(original, this);203 }204 public Decorated<VirtualAuthenticator> createDecorated(VirtualAuthenticator original) {205 return new DefaultDecorated<>(original, this);206 }207 public void beforeCall(Decorated<?> target, Method method, Object[] args) {}208 public Object call(Decorated<?> target, Method method, Object[] args) throws Throwable {209 return decorateResult(method.invoke(target.getOriginal(), args));210 }211 public void afterCall(Decorated<?> target, Method method, Object[] args, Object res) {}212 public Object onError(Decorated<?> target, Method method, Object[] args,213 InvocationTargetException e) throws Throwable214 {215 throw e.getTargetException();216 }217 private Object decorateResult(Object toDecorate) {218 if (toDecorate instanceof WebDriver) {219 return createProxy(getDecoratedDriver());220 }221 if (toDecorate instanceof WebElement) {222 return createProxy(createDecorated((WebElement) toDecorate));223 }224 if (toDecorate instanceof Alert) {225 return createProxy(createDecorated((Alert) toDecorate));226 }227 if (toDecorate instanceof VirtualAuthenticator) {228 return createProxy(createDecorated((VirtualAuthenticator) toDecorate));229 }230 if (toDecorate instanceof WebDriver.Navigation) {231 return createProxy(createDecorated((WebDriver.Navigation) toDecorate));232 }233 if (toDecorate instanceof WebDriver.Options) {234 return createProxy(createDecorated((WebDriver.Options) toDecorate));235 }236 if (toDecorate instanceof WebDriver.TargetLocator) {237 return createProxy(createDecorated((WebDriver.TargetLocator) toDecorate));238 }239 if (toDecorate instanceof WebDriver.Timeouts) {240 return createProxy(createDecorated((WebDriver.Timeouts) toDecorate));241 }242 if (toDecorate instanceof WebDriver.Window) {243 return createProxy(createDecorated((WebDriver.Window) toDecorate));244 }245 if (toDecorate instanceof List) {246 return ((List<?>) toDecorate).stream()247 .map(this::decorateResult)248 .collect(Collectors.toList());249 }250 return toDecorate;251 }252 protected final <Z> Z createProxy(final Decorated<Z> decorated) {253 Set<Class<?>> decoratedInterfaces = extractInterfaces(decorated);254 Set<Class<?>> originalInterfaces = extractInterfaces(decorated.getOriginal());255 final InvocationHandler handler = (proxy, method, args) -> {256 try {257 if (method.getDeclaringClass().equals(Object.class)258 || decoratedInterfaces.contains(method.getDeclaringClass())) {259 return method.invoke(decorated, args);260 }261 if (originalInterfaces.contains(method.getDeclaringClass())) {262 decorated.beforeCall(method, args);263 Object result = decorated.call(method, args);264 decorated.afterCall(method, result, args);265 return result;266 }267 return method.invoke(decorated.getOriginal(), args);268 } catch (InvocationTargetException e) {269 return decorated.onError(method, e, args);270 }271 };272 Set<Class<?>> allInterfaces = new HashSet<>();273 allInterfaces.addAll(decoratedInterfaces);274 allInterfaces.addAll(originalInterfaces);275 Class<?>[] allInterfacesArray = allInterfaces.toArray(new Class<?>[0]);276 return (Z) Proxy.newProxyInstance(277 this.getClass().getClassLoader(), allInterfacesArray, handler);278 }279 static Set<Class<?>> extractInterfaces(final Object object) {280 return extractInterfaces(object.getClass());281 }...

Full Screen

Full Screen

Source:Decorated.java Github

copy

Full Screen

...17package org.openqa.selenium.support.decorators;18import java.lang.reflect.InvocationTargetException;19import java.lang.reflect.Method;20public interface Decorated<T> {21 T getOriginal();22 WebDriverDecorator getDecorator();23 void beforeCall(Method method, Object[] args);24 Object call(Method method, Object[] args) throws Throwable;25 void afterCall(Method method, Object result, Object[] args);26 Object onError(Method method, InvocationTargetException e, Object[] args) throws Throwable;27}...

Full Screen

Full Screen

getOriginal

Using AI Code Generation

copy

Full Screen

1import org.openqa.selenium.By;2import org.openqa.selenium.WebDriver;3import org.openqa.selenium.WebElement;4import org.openqa.selenium.support.PageFactory;5import org.openqa.selenium.support.pagefactory.ElementLocatorFactory;6import org.openqa.selenium.support.pagefactory.FieldDecorator;7import org.openqa.selenium.support.pagefactory.internal.LocatingElementHandler;8import java.lang.reflect.Field;9import java.lang.reflect.InvocationHandler;10import java.lang.reflect.InvocationTargetException;11import java.lang.reflect.Method;12import java.lang.reflect.Proxy;13public class CustomFieldDecorator implements FieldDecorator {14 private final WebDriver driver;15 private final ElementLocatorFactory factory;16 public CustomFieldDecorator(WebDriver driver, ElementLocatorFactory factory) {17 this.driver = driver;18 this.factory = factory;19 }20 public Object decorate(ClassLoader loader, Field field) {21 if (WebElement.class.isAssignableFrom(field.getType())) {22 return proxyForLocator(loader, factory.createLocator(field));23 } else {24 return null;25 }26 }27 private WebElement proxyForLocator(ClassLoader loader, ElementLocator locator) {28 InvocationHandler handler = new LocatingElementHandler(locator);29 WebElement proxy;30 proxy = (WebElement) Proxy.newProxyInstance(loader, new Class[]{WebElement.class}, handler);31 return proxy;32 }33 public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {34 WebDriver driver = new ChromeDriver();35 PageFactory.initElements(new CustomFieldDecorator(driver, new CustomElementLocatorFactory(driver)), new CustomFieldDecoratorTest());36 WebElement element = CustomFieldDecoratorTest.searchBox;37 System.out.println(element.getTagName());38 System.out.println(element.getOriginal().getTagName());39 }40}41class CustomElementLocatorFactory implements ElementLocatorFactory {42 private final WebDriver driver;43 public CustomElementLocatorFactory(WebDriver driver) {44 this.driver = driver;45 }46 public ElementLocator createLocator(Field field) {47 return new CustomElementLocator(driver, field);48 }49}50class CustomElementLocator implements ElementLocator {51 private final WebDriver driver;52 private final Field field;53 public CustomElementLocator(WebDriver driver, Field field) {54 this.driver = driver;55 this.field = field;56 }57 public WebElement findElement() {58 return driver.findElement(By.id("lst-ib"));59 }60 public List<WebElement> findElements() {61 return driver.findElements(By

Full Screen

Full Screen

getOriginal

Using AI Code Generation

copy

Full Screen

1public class DecoratorTest {2 public void test() {3 WebDriver driver = new ChromeDriver();4 WebElement element = driver.findElement(By.name("q"));5 element.sendKeys("selenium");6 element.submit();7 driver.quit();8 }9}10public class DecoratorTest {11 public void test() {12 WebDriver driver = new ChromeDriver();13 WebElement element = driver.findElement(By.name("q"));14 element.sendKeys("selenium");15 element.submit();16 driver.quit();17 }18}19public class DecoratorTest {20 public void test() {21 WebDriver driver = new ChromeDriver();22 WebElement element = driver.findElement(By.name("q"));23 element.sendKeys("selenium");24 element.submit();25 driver.quit();26 }27}28public class DecoratorTest {29 public void test() {30 WebDriver driver = new ChromeDriver();31 WebElement element = driver.findElement(By.name("q"));32 element.sendKeys("selenium");33 element.submit();34 driver.quit();35 }36}37public class DecoratorTest {38 public void test() {39 WebDriver driver = new ChromeDriver();40 WebElement element = driver.findElement(By.name("q"));41 element.sendKeys("selenium");42 element.submit();43 driver.quit();44 }45}46public class DecoratorTest {47 public void test() {48 WebDriver driver = new ChromeDriver();49 WebElement element = driver.findElement(By.name("q"));50 element.sendKeys("selenium");51 element.submit();52 driver.quit();53 }54}55public class DecoratorTest {

Full Screen

Full Screen

getOriginal

Using AI Code Generation

copy

Full Screen

1import org.openqa.selenium.support.decorators.InterfaceDecorated;2import org.openqa.selenium.support.decorators.Original;3import org.openqa.selenium.support.decorators.OriginalMethod;4import org.openqa.selenium.support.decorators.With;5import org.openqa.selenium.support.decorators.Without;6import org.openqa.selenium.support.decorators.WebDriverDecorator;7import org.openqa.selenium.support.decorators.WebDriverDecorators;8import org.openqa.selenium.support.decorators.WebDriverProxy;9import org.openqa.selenium.support.decorators.Without;10import org.openqa.selenium.support.decorators.WebDriverDecorator;11import org.openqa.selenium.support.decorators.WebDriverDecorators;12import org.openqa.selenium.support.decorators.WebDriverProxy;13import org.openqa.selenium.support.decorators.Without;14import org.openqa.selenium.support.decorators.WebDriverDecorator;15import org.openqa.selenium.support.decorators.WebDriverDecorators;16import org.openqa.selenium.support.decorators.WebDriverProxy;17import org.openqa.selenium.support.decorators.Without;18import org.openqa.selenium.support.decorators.WebDriverDecorator;19import org.openqa.selenium.support.decorators.WebDriverDecorators;20import org.openqa.selenium.support.decorators.WebDriverProxy;21import org.openqa.selenium.support.decorators.Without;22import org.openqa.selenium.support.decorators.WebDriverDecorator;23import org.openqa.selenium.support.decorators.WebDriverDecorators;24import org.openqa.selenium.support.decorators.WebDriverProxy;25import org.openqa.selenium.support.decorators.Without;26import org.openqa.selenium.support.decorators.WebDriverDecorator;27import org.openqa.selenium.support.decorators.WebDriverDecorators;28import org.openqa.selenium.support.decorators.WebDriverProxy;29import org.openqa.selenium.support.decorators.Without;30import org.openqa.selenium.support.decorators.WebDriverDecorator;31import org.openqa.selenium.support.decorators.WebDriverDecorators;32import org.openqa.selenium.support.decorators.WebDriverProxy;33import org.openqa.selenium.support.decorators.Without;34import org.openqa.selenium.support.decorators.WebDriverDecorator;35import org.openqa.selenium.support.decorators.WebDriverDecorators;36import org.openqa.selenium.support.decorators.WebDriverProxy;37import org.openqa.selenium.support.decorators.Without;38import org.openqa.selenium.support.decorators.WebDriverDecorator;39import org.openqa.selenium.support.decorators.WebDriverDecorators;40import org.openqa.selenium.support.decorators.WebDriverProxy;41import org.openqa.selenium.support.decorators.Without;42import org.openqa.selenium.support.decorators.WebDriverDecorator;43import org.openqa.selenium.support.decorators.WebDriverDecorators;44import org.openqa.selenium.support.decorators.WebDriverProxy;45import org.openqa.selenium.support.decorators.Without;46import org.openqa.selenium.support.decorators.WebDriverDecorator;47import org.openqa.selenium.support.decorators.WebDriverDecorators;48import org.openqa.selenium.support.decorators.WebDriverProxy;49import org.openqa.selenium.support.decorators.Without;50import org.openqa.selenium.support.decorators.WebDriverDecorator;51import org.openqa.selenium.support.decorators.WebDriverDecorators;52import org.openqa.selenium.support.decorators.WebDriverProxy;53import org.openqa.selenium.support.decorators.Without;54import org.openqa.selenium.support.decorators.WebDriverDecorator

Full Screen

Full Screen

getOriginal

Using AI Code Generation

copy

Full Screen

1import org.openqa.selenium.support.decorators.Decorator;2import org.openqa.selenium.support.decorators.DecoratorBuilder;3import org.openqa.selenium.support.decorators.DecoratorFactory;4import org.openqa.selenium.support.decorators.ImplementedBy;5import org.openqa.selenium.support.decorators.WithTimeout;6import org.openqa.selenium.support.ui.ExpectedConditions;7import org.openqa.selenium.support.ui.WebDriverWait;8import org.openqa.selenium.By;9import org.openqa.selenium.WebDriver;10import org.openqa.selenium.WebElement;11import org.openqa.selenium.chrome.ChromeDriver;12import org.openqa.selenium.support.PageFactory;13public class Demo {14 @ImplementedBy(PageObjectImpl.class)15 public interface PageObject {16 public void clickButton();17 public WebElement getButton();18 public void waitForButton();19 public PageObject getOriginal();20 }21 public static class PageObjectImpl implements PageObject {22 private final WebDriver driver;23 public PageObjectImpl(WebDriver driver) {24 this.driver = driver;25 }26 public void clickButton() {27 getButton().click();28 }29 public WebElement getButton() {30 return driver.findElement(By.id("button"));31 }32 public void waitForButton() {33 new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOf(getButton()));34 }35 public PageObject getOriginal() {36 return this;37 }38 }39 public static void main(String[] args) {40 WebDriver driver = new ChromeDriver();41 Decorator decorator = new DecoratorBuilder().build();42 DecoratorFactory factory = decorator.factoryOf(PageObject.class);43 PageObject pageObject = factory.decorate(driver);44 pageObject.waitForButton();45 pageObject.clickButton();46 PageObject originalPageObject = pageObject.getOriginal();47 originalPageObject.waitForButton();48 originalPageObject.clickButton();49 driver.quit();

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 Interface-Decorated

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful