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

Best Selenium code snippet using org.openqa.selenium.support.decorators.DefaultDecorated.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:DefaultDecorated.java Github

copy

Full Screen

...23 public DefaultDecorated(final T original, final WebDriverDecorator decorator) {24 this.original = original;25 this.decorator = decorator;26 }27 public final T getOriginal() {28 return original;29 }30 public final WebDriverDecorator getDecorator() {31 return decorator;32 }33 @Override34 public void beforeCall(Method method, Object[] args) {35 getDecorator().beforeCall(this, method, args);36 }37 @Override38 public Object call(Method method, Object[] args) throws Throwable {39 return getDecorator().call(this, method, args);40 }41 @Override42 public void afterCall(Method method, Object result, Object[] args) {43 getDecorator().afterCall(this, method, args, result);44 }45 @Override46 public Object onError(Method method, InvocationTargetException e, Object[] args) throws Throwable {47 return getDecorator().onError(this, method, args, e);48 }49 @Override50 public String toString() {51 return String.format("Decorated {%s}", original);52 }53 @Override54 public boolean equals(Object o) {55 if (this == o) return true;56 if (o instanceof Decorated) {57 Decorated<?> that = (Decorated<?>) o;58 return original.equals(that.getOriginal());59 } else {60 return this.original.equals(o);61 }62 }63 @Override64 public int hashCode() {65 return original.hashCode();66 }67}...

Full Screen

Full Screen

Source:WaitHelper.java Github

copy

Full Screen

...31 @Override32 public void beforeCall(Method method, Object[] args) {33 String methodName = method.getName();34 if ("click".equals(methodName) || "sendKeys".equals(methodName)) {35 wait.until(d -> getOriginal().isDisplayed());36 }37 }38 };39 }40 }

Full Screen

Full Screen

getOriginal

Using AI Code Generation

copy

Full Screen

1import java.util.List;2import java.util.concurrent.TimeUnit;3import org.openqa.selenium.By;4import org.openqa.selenium.WebDriver;5import org.openqa.selenium.WebElement;6import org.openqa.selenium.chrome.ChromeDriver;7import org.openqa.selenium.support.PageFactory;8import org.openqa.selenium.support.pagefactory.ElementLocatorFactory;9import org.openqa.selenium.support.pagefactory.FieldDecorator;10import org.openqa.selenium.support.pagefactory.internal.LocatingElementListHandler;11import org.openqa.selenium.support.pagefactory.internal.LocatingElementHandler;12public class PageFactoryDecoratorDemo {13 public static void main(String[] args) {14 System.setProperty("webdriver.chrome.driver", "C:\\Users\\kumar\\Downloads\\chromedriver_win32\\chromedriver.exe");15 WebDriver driver = new ChromeDriver();16 driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);17 GoogleSearchPage page = PageFactory.initElements(driver, GoogleSearchPage.class);18 page.searchBox.sendKeys("Selenium");19 page.searchButton.click();20 List<WebElement> searchList = page.searchList;21 for(WebElement element : searchList){22 System.out.println(element.getText());23 }24 driver.quit();25 }26}27class GoogleSearchPage{28 @FindAll({@FindBy(how = How.ID, using = "lst-ib")})29 public WebElement searchBox;30 @FindAll({@FindBy(how = How.NAME, using = "btnK")})31 public WebElement searchButton;32 public List<WebElement> searchList;33 public GoogleSearchPage(WebDriver driver) {34 PageFactory.initElements(new DefaultElementDecorator(new AjaxElementLocatorFactory(driver, 10)), this);35 }36}37class DefaultElementDecorator implements FieldDecorator{38 private final ElementLocatorFactory factory;39 public DefaultElementDecorator(ElementLocatorFactory factory) {40 this.factory = factory;41 }42 public Object decorate(ClassLoader loader, Field field) {43 if(!WebElement.class.isAssignableFrom(field.getType()) && !isDecoratableList(field)){44 return null;

Full Screen

Full Screen

getOriginal

Using AI Code Generation

copy

Full Screen

1package org.openqa.selenium.support.decorators;2import java.lang.reflect.InvocationHandler;3import java.lang.reflect.Method;4import java.lang.reflect.Proxy;5import org.openqa.selenium.WebDriver;6import org.openqa.selenium.WebDriver.TargetLocator;7import org.openqa.selenium.WebElement;8public class DefaultDecorated implements Decorated {9 private final Object original;10 private final Class<?>[] interfaces;11 public DefaultDecorated(Object original, Class<?>[] interfaces) {12 this.original = original;13 this.interfaces = interfaces;14 }15 public Object getOriginal() {16 return original;17 }18 public Object getDecorated() {19 return Proxy.newProxyInstance(20 original.getClass().getClassLoader(),21 new InvocationHandler() {22 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {23 return method.invoke(original, args);24 }25 });26 }27}28package org.openqa.selenium.support.decorators;29import java.lang.reflect.InvocationHandler;30import java.lang.reflect.Method;31import java.lang.reflect.Proxy;32import org.openqa.selenium.WebDriver;33import org.openqa.selenium.WebDriver.TargetLocator;34import org.openqa.selenium.WebElement;35public class DefaultDecorated implements Decorated {36 private final Object original;37 private final Class<?>[] interfaces;38 public DefaultDecorated(Object original, Class<?>[] interfaces) {39 this.original = original;40 this.interfaces = interfaces;41 }42 public Object getOriginal() {43 return original;44 }45 public Object getDecorated() {46 return Proxy.newProxyInstance(47 original.getClass().getClassLoader(),48 new InvocationHandler() {49 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {50 return method.invoke(original, args);51 }52 });53 }54}55package org.openqa.selenium.support.decorators;56import org.openqa.selenium.WebDriver;57import org.openqa.selenium.WebDriver.TargetLocator;58import org.openqa.selenium.WebElement;59public interface Decorated {60 public Object getOriginal();61 public Object getDecorated();62}63package org.openqa.selenium.support.decorators;64import org.openqa.selenium.WebDriver;65import org.openqa.selenium.WebDriver.TargetLocator;66import org.openqa.selenium.WebElement;67public interface Decorated {68 public Object getOriginal();69 public Object getDecorated();70}71package org.openqa.selenium.support.decorators;72import org.openqa.selenium.WebDriver;73import org.openqa.selenium.WebDriver.TargetLocator;74import org.openqa.selenium.WebElement;75public interface Decorated {76 public Object getOriginal();77 public Object getDecorated();78}79package org.openqa.selenium.support.decorators;80import org.openqa.selenium.WebDriver

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.FindBy;5import org.openqa.selenium.support.PageFactory;6public class PageObject {7 private WebDriver driver;8 @FindBy(id = "id")9 private WebElement element;10 public PageObject(WebDriver driver) {11 this.driver = driver;12 PageFactory.initElements(driver, this);13 }14 public WebElement getElement() {15 return element;16 }17 public WebElement getElementByLocator() {18 return driver.findElement(By.id("id"));19 }20 public void clickElement() {21 element.click();22 }23 public void clickElementByLocator() {24 driver.findElement(By.id("id")).click();25 }26 public String getElementText() {27 return element.getText();28 }29 public String getElementTextByLocator() {30 return driver.findElement(By.id("id")).getText();31 }32}33public class Test {34 public static void main(String[] args) throws Exception {35 WebDriver driver = new ChromeDriver();36 PageObject pageObject = new PageObject(driver);37 WebElement element = pageObject.getElement();38 WebElement elementByLocator = pageObject.getElementByLocator();

Full Screen

Full Screen

getOriginal

Using AI Code Generation

copy

Full Screen

1public class OriginalObjectTest {2 public static void main(String[] args) {3 WebDriver driver = new FirefoxDriver();4 SearchContext searchContext = new DefaultDecorated(driver);5 SearchContext original = searchContext.getOriginal();6 System.out.println(original.getClass().getName());7 driver.quit();8 }9}10[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ OriginalObjectTest ---11[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!12[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ OriginalObjectTest ---13[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ OriginalObjectTest ---14[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!15[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ OriginalObjectTest ---16[INFO] --- maven-surefire-plugin:2.18.1:test (default-test) @ OriginalObjectTest ---17[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ OriginalObjectTest ---

Full Screen

Full Screen

getOriginal

Using AI Code Generation

copy

Full Screen

1import org.openqa.selenium.By;2import org.openqa.selenium.WebElement;3import org.openqa.selenium.support.FindBy;4import org.openqa.selenium.support.PageFactory;5import org.openqa.selenium.support.pagefactory.DefaultElementLocatorFactory;6import org.openqa.selenium.support.pagefactory.DefaultFieldDecorator;7import org.openqa.selenium.support.pagefactory.ElementLocatorFactory;8import org.openqa.selenium.support.ui.ExpectedConditions;9import org.openqa.selenium.support.ui.WebDriverWait;10import org.testng.annotations.Test;11public class GetOriginalWebElement extends BaseTest {12 private WebElement googleLink;13 public void test() {14 PageFactory.initElements(new CustomFieldDecorator(driver), this);15 googleLink.click();16 }17 private class CustomFieldDecorator extends DefaultFieldDecorator {18 public CustomFieldDecorator(org.openqa.selenium.WebDriver driver) {19 super(new DefaultElementLocatorFactory(driver));20 }21 public Object decorate(ClassLoader loader, Field field) {22 if (WebElement.class.isAssignableFrom(field.getType())) {23 return new CustomWebElement(super.decorate(loader, field));24 }25 return super.decorate(loader, field);26 }27 }28 private class CustomWebElement implements WebElement {29 private final WebElement element;30 public CustomWebElement(WebElement element) {31 this.element = element;32 }33 public void click() {34 element.getWrappedElement().click();35 }36 }37}38import org.openqa.selenium.By;39import org.openqa.selenium.WebElement;40import org.openqa.selenium.support.FindBy;41import org.openqa.selenium.support.PageFactory;42import org.openqa.selenium.support.pagefactory.DefaultElementLocatorFactory;43import org.openqa.selenium.support.page

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.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful