How to use createWebElement method of org.fluentlenium.core.proxy.LocatorProxies class

Best FluentLenium code snippet using org.fluentlenium.core.proxy.LocatorProxies.createWebElement

Source:ProxiesTest.java Github

copy

Full Screen

...41 when(element2.findElement(By.cssSelector("#element3"))).thenReturn(element3);42 }43 @Test44 public void testElementIsLazy() {45 WebElement elementProxy1 = LocatorProxies.createWebElement(new ElementLocator() {46 @Override47 public WebElement findElement() {48 return driver.findElement(By.cssSelector("#element1"));49 }50 @Override51 public List<WebElement> findElements() {52 return singletonList(findElement());53 }54 });55 verifyZeroInteractions(driver);56 verifyZeroInteractions(element1);57 elementProxy1.click();58 verify(element1).click();59 }60 @Test61 public void testElementChainIsLazy() {62 WebElement element1Proxy = LocatorProxies.createWebElement(new ElementLocator() {63 @Override64 public WebElement findElement() {65 return driver.findElement(By.cssSelector("#element1"));66 }67 @Override68 public List<WebElement> findElements() {69 return singletonList(findElement());70 }71 });72 WebElement element2Proxy = LocatorProxies.createWebElement(new ElementLocator() {73 @Override74 public WebElement findElement() {75 return element1Proxy.findElement(By.cssSelector("#element2"));76 }77 @Override78 public List<WebElement> findElements() {79 return singletonList(findElement());80 }81 });82 WebElement element3Proxy = LocatorProxies.createWebElement(new ElementLocator() {83 @Override84 public WebElement findElement() {85 return element2Proxy.findElement(By.cssSelector("#element3"));86 }87 @Override88 public List<WebElement> findElements() {89 return singletonList(findElement());90 }91 });92 verifyZeroInteractions(driver);93 verifyZeroInteractions(element1);94 verifyZeroInteractions(element2);95 verifyZeroInteractions(element3);96 element3Proxy.click();97 verify(driver).findElement(By.cssSelector("#element1"));98 verify(element1).findElement(By.cssSelector("#element2"));99 verify(element2).findElement(By.cssSelector("#element3"));100 }101 @Test102 public void testAlreadyLoadedElementsShouldBeLoaded() {103 WebElement webElement = LocatorProxies.createWebElement(element1);104 assertThat(LocatorProxies.loaded(webElement)).isTrue();105 assertThat(((WrapsElement) webElement).getWrappedElement()).isSameAs(element1);106 }107 @Test108 public void testAlreadyLoadedElementListShouldBeLoaded() {109 List<WebElement> webElementList = LocatorProxies.createWebElementList(Arrays.asList(element1, element2, element3));110 assertThat(LocatorProxies.loaded(webElementList)).isTrue();111 assertThat(((WrapsElements) webElementList).getWrappedElements())112 .containsExactlyInAnyOrder(element1, element2, element3);113 }114 @Test115 public void testNullElementShouldThrowNoSuchElementException() {116 assertThatThrownBy(() -> LocatorProxies.createWebElement((WebElement) null))117 .isExactlyInstanceOf(NoSuchElementException.class);118 WebElement proxy = LocatorProxies.createWebElement(new ElementLocator() {119 @Override120 public WebElement findElement() {121 return null;122 }123 @Override124 public List<WebElement> findElements() {125 return null;126 }127 });128 assertThatThrownBy(() -> LocatorProxies.now(proxy)).isExactlyInstanceOf(NoSuchElementException.class);129 }130 @Test131 public void testNullElementListShouldNotThrowException() {132 List<WebElement> webElementList = LocatorProxies.createWebElementList((List<WebElement>) null);133 assertThat(webElementList).isEmpty();134 List<WebElement> webElementList2 = LocatorProxies.createWebElementList(new ElementLocator() {135 @Override136 public WebElement findElement() {137 return null;138 }139 @Override140 public List<WebElement> findElements() {141 return null;142 }143 });144 assertThat(webElementList2).isEmpty();145 }146 @Test147 public void testEmptyElementListShouldNotThrowException() {148 List<WebElement> webElementList = LocatorProxies.createWebElementList(Collections.emptyList());149 assertThat(webElementList).isEmpty();150 List<WebElement> webElementList2 = LocatorProxies.createWebElementList(new ElementLocator() {151 @Override152 public WebElement findElement() {153 return null;154 }155 @Override156 public List<WebElement> findElements() {157 return Collections.emptyList();158 }159 });160 assertThat(webElementList2).isEmpty();161 }162 @Test163 public void testToString() {164 when(element1.toString()).thenReturn("element1");165 ElementLocator locator = mock(ElementLocator.class);166 when(locator.findElement()).thenReturn(element1);167 when(locator.toString()).thenReturn("element1-locator");168 WebElement webElement = LocatorProxies.createWebElement(locator);169 assertThat(webElement.toString()).isEqualTo("element1-locator (Lazy Element)");170 assertThat(LocatorProxies.loaded(webElement)).isFalse();171 LocatorProxies.now(webElement);172 assertThat(webElement.toString()).isEqualTo("element1-locator (" + element1.toString() + ")");173 }174 @Test175 public void testHashcode() {176 ElementLocator locator = mock(ElementLocator.class);177 when(locator.findElement()).thenReturn(element1);178 WebElement webElement = LocatorProxies.createWebElement(locator);179 assertThat(webElement.hashCode()).isEqualTo(2048 + locator.hashCode());180 assertThat(LocatorProxies.loaded(webElement)).isFalse();181 LocatorProxies.now(webElement);182 assertThat(webElement.hashCode()).isEqualTo(element1.hashCode());183 }184 @Test185 public void testEquals() {186 ElementLocator locator = mock(ElementLocator.class);187 when(locator.findElement()).thenReturn(element1);188 WebElement webElement = LocatorProxies.createWebElement(locator);189 WebElement sameWebElement = LocatorProxies.createWebElement(locator);190 assertThat(webElement).isEqualTo(sameWebElement);191 ElementLocator otherLocator = mock(ElementLocator.class);192 when(otherLocator.findElement()).thenReturn(element2);193 WebElement otherWebElement = LocatorProxies.createWebElement(otherLocator);194 assertThat(webElement).isNotEqualTo(otherWebElement);195 assertThat(LocatorProxies.loaded(webElement)).isFalse();196 assertThat(LocatorProxies.loaded(sameWebElement)).isFalse();197 assertThat(LocatorProxies.loaded(otherWebElement)).isFalse();198 LocatorProxies.now(webElement);199 assertThat(webElement).isEqualTo(sameWebElement);200 assertThat(LocatorProxies.loaded(webElement)).isTrue();201 assertThat(LocatorProxies.loaded(sameWebElement)).isTrue();202 assertThat(LocatorProxies.loaded(otherWebElement)).isFalse();203 LocatorProxies.reset(webElement);204 LocatorProxies.reset(sameWebElement);205 LocatorProxies.reset(otherWebElement);206 assertThat(LocatorProxies.loaded(webElement)).isFalse();207 assertThat(LocatorProxies.loaded(sameWebElement)).isFalse();208 assertThat(LocatorProxies.loaded(otherWebElement)).isFalse();209 LocatorProxies.now(webElement);210 assertThat(sameWebElement).isEqualTo(webElement);211 assertThat(LocatorProxies.loaded(webElement)).isTrue();212 assertThat(LocatorProxies.loaded(sameWebElement)).isTrue();213 assertThat(LocatorProxies.loaded(otherWebElement)).isFalse();214 assertThat(webElement).isNotEqualTo(otherWebElement);215 assertThat(LocatorProxies.loaded(otherWebElement)).isTrue();216 }217 @Test218 public void testIsPresent() {219 ElementLocator locator = mock(ElementLocator.class);220 when(locator.findElement()).thenReturn(element1);221 when(locator.findElements()).thenReturn(Arrays.asList(element1, element2, element3));222 WebElement webElement = LocatorProxies.createWebElement(locator);223 ElementLocator otherLocator = mock(ElementLocator.class);224 WebElement otherWebElement = LocatorProxies.createWebElement(otherLocator);225 assertThat(LocatorProxies.loaded(webElement)).isFalse();226 assertThat(LocatorProxies.present(webElement)).isTrue();227 assertThat(LocatorProxies.loaded(webElement)).isTrue();228 assertThat(LocatorProxies.loaded(otherWebElement)).isFalse();229 assertThat(LocatorProxies.present(otherWebElement)).isFalse();230 assertThat(LocatorProxies.loaded(otherWebElement)).isFalse();231 List<WebElement> webElementList = LocatorProxies.createWebElementList(locator);232 assertThat(LocatorProxies.loaded(webElementList)).isFalse();233 assertThat(LocatorProxies.present(webElementList)).isTrue();234 assertThat(LocatorProxies.loaded(webElementList)).isTrue();235 when(locator.findElements()).thenReturn(Collections.emptyList());236 LocatorProxies.reset(webElementList);237 assertThat(LocatorProxies.loaded(webElementList)).isFalse();238 assertThat(LocatorProxies.present(webElementList)).isFalse();239 assertThat(LocatorProxies.loaded(webElementList)).isTrue();240 }241 @Test242 public void testLocatorGetter() {243 ElementLocator locator = mock(ElementLocator.class);244 WebElement webElement = LocatorProxies.createWebElement(locator);245 LocatorHandler locatorHandler = LocatorProxies.getLocatorHandler(webElement);246 assertThat(locatorHandler.getLocator()).isSameAs(locator);247 }248 @Test249 public void testFirst() {250 ElementLocator locator = mock(ElementLocator.class);251 when(locator.findElements()).thenReturn(Arrays.asList(element1, element2, element3));252 List<WebElement> webElementList = LocatorProxies.createWebElementList(locator);253 WebElement first = LocatorProxies.first(webElementList);254 assertThat(LocatorProxies.loaded(first)).isFalse();255 assertThat(first).isEqualTo(element1);256 }257 @Test258 public void testLast() {259 ElementLocator locator = mock(ElementLocator.class);260 when(locator.findElements()).thenReturn(Arrays.asList(element1, element2, element3));261 List<WebElement> webElementList = LocatorProxies.createWebElementList(locator);262 WebElement last = LocatorProxies.last(webElementList);263 assertThat(LocatorProxies.loaded(last)).isFalse();264 assertThat(last).isEqualTo(element3);265 }266 @Test267 public void testIndex() {268 ElementLocator locator = mock(ElementLocator.class);269 when(locator.findElements()).thenReturn(Arrays.asList(element1, element2, element3));270 List<WebElement> webElementList = LocatorProxies.createWebElementList(locator);271 WebElement atIndex = LocatorProxies.index(webElementList, 1);272 assertThat(LocatorProxies.loaded(atIndex)).isFalse();273 assertThat(atIndex).isEqualTo(element2);274 }275 @Test276 public void testStateElement() {277 ElementLocator locator = mock(ElementLocator.class);278 when(locator.findElement()).thenReturn(element1);279 WebElement webElement = LocatorProxies.createWebElement(locator);280 assertThat(LocatorProxies.present(webElement)).isTrue();281 webElement.isEnabled();282 when(element1.isEnabled()).thenThrow(StaleElementReferenceException.class);283 assertThat(LocatorProxies.present(webElement)).isFalse();284 reset(element1);285 when(element1.isEnabled()).thenThrow(StaleElementReferenceException.class);286 assertThatThrownBy(webElement::isEnabled).isExactlyInstanceOf(StaleElementReferenceException.class);287 verify(element1, times(6)).isEnabled();288 }289}...

Full Screen

Full Screen

Source:LocatorProxies.java Github

copy

Full Screen

...165 *166 * @param element existing element167 * @return proxy168 */169 public static WebElement createWebElement(WebElement element) {170 return createWebElement(new ElementInstanceLocator(element));171 }172 /**173 * Create a WebElement proxy from an element supplier.174 *175 * @param elementSupplier element supplier176 * @return proxy177 */178 public static WebElement createWebElement(Supplier<WebElement> elementSupplier) {179 return createWebElement(new ElementSupplierLocator(elementSupplier));180 }181 /**182 * Create a WebElement proxy from an element locator.183 *184 * @param locator element locator185 * @return proxy186 */187 public static WebElement createWebElement(ElementLocator locator) {188 ComponentHandler handler = new ComponentHandler(locator);189 WebElement proxy = (WebElement) Proxy.newProxyInstance(locator.getClass().getClassLoader(),190 new Class[] {WebElement.class, Locatable.class, WrapsElement.class}, handler);191 handler.setProxy(proxy);192 return proxy;193 }194 /**195 * Create a list of WebElement proxies from an existing element list.196 *197 * @param elements existing element list198 * @return proxy199 */200 public static List<WebElement> createWebElementList(List<WebElement> elements) {201 return createWebElementList(new ElementListInstanceLocator(elements));202 }203 /**204 * Create a list of WebElement proxies from a supplier of element list.205 *206 * @param elementsSupplier supplier of element list207 * @return proxy208 */209 public static List<WebElement> createWebElementList(Supplier<List<WebElement>> elementsSupplier) {210 return createWebElementList(new ElementListSupplierLocator(elementsSupplier));211 }212 /**213 * Create a list of WebElement proxies from a locator of element list.214 *215 * @param locator locator of element list216 * @return proxy217 */218 public static List<WebElement> createWebElementList(ElementLocator locator) {219 ListHandler handler = new ListHandler(locator);220 List<WebElement> proxy = (List<WebElement>) Proxy221 .newProxyInstance(locator.getClass().getClassLoader(), new Class[] {List.class, WrapsElements.class}, handler);222 handler.setProxy(proxy);223 return proxy;224 }225 /**226 * Apply this hook list.227 *228 * @param proxy proxy object229 * @param hookChainBuilder hook chain builder230 * @param hookDefinitions hook definitions231 */232 public static void setHooks(Object proxy, HookChainBuilder hookChainBuilder, List<HookDefinition<?>> hookDefinitions) {233 LocatorHandler<?> componentHandler = getLocatorHandler(proxy);234 componentHandler.setHooks(hookChainBuilder, hookDefinitions);235 }236 /**237 * Creates a proxy element matching the first element of the list.238 *239 * @param proxy list of element240 * @return proxy element matching the first element241 */242 public static WebElement first(List<WebElement> proxy) {243 LocatorHandler locatorHandler = getLocatorHandler(proxy);244 return createWebElement(new FirstElementLocator(locatorHandler.getLocator()));245 }246 /**247 * Creates a proxy element matching the last element of the list.248 *249 * @param proxy list of element250 * @return proxy element matching the last element251 */252 public static WebElement last(List<WebElement> proxy) {253 LocatorHandler locatorHandler = getLocatorHandler(proxy);254 return createWebElement(new LastElementLocator(locatorHandler.getLocator()));255 }256 /**257 * Creates a proxy element matching the n-th element of the list.258 *259 * @param proxy list of element260 * @param index index to match261 * @return proxy element matching the n-th element262 */263 public static WebElement index(List<WebElement> proxy, int index) {264 LocatorHandler locatorHandler = getLocatorHandler(proxy);265 return createWebElement(new AtIndexElementLocator(locatorHandler.getLocator(), index));266 }267}...

Full Screen

Full Screen

Source:ProxyHookTest.java Github

copy

Full Screen

...46 }47 @Test48 public void testHooksOnElement() {49 Mockito.when(locator.findElement()).thenReturn(element1);50 WebElement proxy = LocatorProxies.createWebElement(locator);51 LocatorProxies.now(proxy);52 List<HookDefinition<?>> hooks = new ArrayList<>();53 HookDefinition hookDefinition = new HookDefinition<>(TestHook.class);54 hooks.add(hookDefinition);55 ElementLocator hookLocator = LocatorProxies.getLocatorHandler(proxy).getHookLocator();56 WebElement hookElement = (WebElement) LocatorProxies.getLocatorHandler(proxy).getInvocationTarget(null);57 Assertions.assertThat(hookLocator).isSameAs(locator);58 Assertions.assertThat(hookElement).isSameAs(element1);59 LocatorProxies.setHooks(proxy, hookChainBuilder, hooks);60 hookLocator = LocatorProxies.getLocatorHandler(proxy).getHookLocator();61 hookElement = (WebElement) LocatorProxies.getLocatorHandler(proxy).getInvocationTarget(null);62 Assertions.assertThat(hookLocator).isExactlyInstanceOf(TestHook.class);63 Assertions.assertThat(hookElement).isExactlyInstanceOf(TestHook.class);64 }65 @Test66 public void testHooksOnElementList() {67 Mockito.when(locator.findElements()).thenReturn(Arrays.asList(element1, element2, element3));68 List<WebElement> proxy = LocatorProxies.createWebElementList(locator);69 LocatorProxies.now(proxy);70 List<HookDefinition<?>> hooks = new ArrayList<>();71 HookDefinition hookDefinition = new HookDefinition<>(TestHook.class);72 hooks.add(hookDefinition);73 ElementLocator hookLocator = LocatorProxies.getLocatorHandler(proxy).getHookLocator();74 List<WebElement> hookElements = (List<WebElement>) LocatorProxies.getLocatorHandler(proxy).getInvocationTarget(null);75 Assertions.assertThat(hookLocator).isSameAs(locator);76 Assertions.assertThat(LocatorProxies.getLocatorHandler(hookElements.get(0)).getInvocationTarget(null)).isSameAs(element1);77 LocatorProxies.reset(proxy);78 LocatorProxies.setHooks(proxy, hookChainBuilder, hooks);79 LocatorProxies.now(proxy);80 hookLocator = LocatorProxies.getLocatorHandler(proxy).getHookLocator();81 hookElements = (List<WebElement>) LocatorProxies.getLocatorHandler(proxy).getInvocationTarget(null);82 Assertions.assertThat(hookLocator).isExactlyInstanceOf(TestHook.class);...

Full Screen

Full Screen

createWebElement

Using AI Code Generation

copy

Full Screen

1package com.example;2import org.fluentlenium.adapter.junit.FluentTest;3import org.fluentlenium.core.proxy.LocatorProxies;4import org.junit.Test;5import org.openqa.selenium.By;6import org.openqa.selenium.WebDriver;7import org.openqa.selenium.WebElement;8import org.openqa.selenium.htmlunit.HtmlUnitDriver;9public class CreateWebElementTest extends FluentTest {10 public WebDriver getDefaultDriver() {11 return new HtmlUnitDriver();12 }13 public void testCreateWebElement() {14 WebElement searchBox = LocatorProxies.createWebElement(By.name("q"));15 searchBox.sendKeys("FluentLenium");16 searchBox.submit();17 }18}

Full Screen

Full Screen

createWebElement

Using AI Code Generation

copy

Full Screen

1package org.fluentlenium.core.proxy;2import org.openqa.selenium.By;3import org.openqa.selenium.WebElement;4import org.openqa.selenium.support.pagefactory.ElementLocator;5public class LocatorProxies {6 public static WebElement createWebElement(ElementLocator locator) {7 return new LocatingWebElement(locator);8 }9}10package org.fluentlenium.core.proxy;11import org.openqa.selenium.By;12import org.openqa.selenium.WebElement;13import org.openqa.selenium.support.pagefactory.ElementLocator;14public class LocatorProxies {15 public static WebElement createWebElement(ElementLocator locator) {16 return new LocatingWebElement(locator);17 }18}19package org.fluentlenium.core.proxy;20import org.openqa.selenium.By;21import org.openqa.selenium.WebElement;22import org.openqa.selenium.support.pagefactory.ElementLocator;23public class LocatorProxies {24 public static WebElement createWebElement(ElementLocator locator) {25 return new LocatingWebElement(locator);26 }27}28package org.fluentlenium.core.proxy;29import org.openqa.selenium.By;30import org.openqa.selenium.WebElement;31import org.openqa.selenium.support.pagefactory.ElementLocator;32public class LocatorProxies {33 public static WebElement createWebElement(ElementLocator locator) {34 return new LocatingWebElement(locator);35 }36}37package org.fluentlenium.core.proxy;38import org.openqa.selenium.By;39import org.openqa.selenium.WebElement;40import org.openqa.selenium.support.pagefactory.ElementLocator;41public class LocatorProxies {42 public static WebElement createWebElement(ElementLocator locator) {43 return new LocatingWebElement(locator);44 }45}46package org.fluentlenium.core.proxy;47import org.openqa.selenium.By;48import org.openqa.selenium.WebElement;49import org.openqa.selenium.support.pagefactory.ElementLocator;50public class LocatorProxies {51 public static WebElement createWebElement(ElementLocator locator) {52 return new LocatingWebElement(locator);

Full Screen

Full Screen

createWebElement

Using AI Code Generation

copy

Full Screen

1import org.fluentlenium.core.proxy.LocatorProxies;2import org.fluentlenium.core.proxy.WebElementLocator;3import org.openqa.selenium.By;4import org.openqa.selenium.WebElement;5public class createWebElement {6 public static void main(String[] args) {7 WebElement element = LocatorProxies.createWebElement(new WebElementLocator(By.id("id")));8 System.out.println(element.toString());9 }10}

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run FluentLenium 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