Best FluentLenium code snippet using org.fluentlenium.test.annotations.AnnotationsComponentEventsTest.Component
Source:AnnotationsComponentEventsTest.java
1package org.fluentlenium.test.annotations;2import org.fluentlenium.core.FluentControl;3import org.fluentlenium.core.components.ComponentInstantiator;4import org.fluentlenium.core.domain.FluentList;5import org.fluentlenium.core.domain.FluentWebElement;6import org.fluentlenium.core.events.annotations.AfterClickOn;7import org.fluentlenium.core.events.annotations.AfterFindBy;8import org.fluentlenium.core.events.annotations.BeforeClickOn;9import org.fluentlenium.core.events.annotations.BeforeFindBy;10import org.junit.jupiter.api.Test;11import org.openqa.selenium.By;12import org.openqa.selenium.WebElement;13import org.openqa.selenium.internal.WrapsElement;14import java.util.ArrayList;15import java.util.List;16import static org.assertj.core.api.Assertions.assertThat;17public class AnnotationsComponentEventsTest extends AnnotationsComponentEventsTestSubClass {18 private final List<WebElement> beforeClick = new ArrayList<>();19 public static class Component extends FluentWebElement {20 private int beforeClick;21 private int afterClick;22 private final List<By> beforeFindBy = new ArrayList<>();23 private final List<By> afterFindBy = new ArrayList<>();24 public Component(WebElement webElement, FluentControl fluentControl, ComponentInstantiator instantiator) {25 super(webElement, fluentControl, instantiator);26 }27 @BeforeClickOn28 public void beforeClickOn() {29 assertThat(afterClick).isEqualTo(beforeClick);30 beforeClick++;31 }32 @AfterClickOn33 public void afterClickOn() {34 assertThat(beforeClick).isEqualTo(afterClick + 1);35 afterClick++;36 }37 @BeforeFindBy38 public void beforeFindBy(By by) {39 beforeFindBy.add(by);40 }41 @AfterFindBy42 public void afterFindBy(By by) {43 assertThat(beforeFindBy).hasSize(afterFindBy.size() + 1);44 afterFindBy.add(by);45 }46 }47 @Test48 void clickOnFirst() {49 goTo(DEFAULT_URL);50 Component button = el("button").as(Component.class);51 button.click();52 Component otherButton = el("button").as(Component.class);53 assertThat(button.beforeClick).isEqualTo(1);54 assertThat(button.afterClick).isEqualTo(1);55 assertThat(otherButton.beforeClick).isEqualTo(0);56 assertThat(otherButton.afterClick).isEqualTo(0);57 assertThat(beforeClick).containsExactly(unwrapElement(button.getElement()));58 assertThat(afterClick).containsExactly(unwrapElement(button.getElement()));59 }60 private WebElement unwrapElement(WebElement element) {61 if (element instanceof WrapsElement) {62 WebElement wrappedElement = ((WrapsElement) element).getWrappedElement();63 if (wrappedElement != element && wrappedElement != null) { // NOPMD CompareObjectsWithEquals64 return unwrapElement(wrappedElement);65 }66 }67 return element;68 }69 @Test70 void clickOn() {71 goTo(DEFAULT_URL);72 FluentList<Component> buttons = $("button").as(Component.class);73 buttons.click();74 FluentList<Component> otherButtons = $("button").as(Component.class);75 for (Component button : buttons) {76 assertThat(button.beforeClick).isEqualTo(1);77 assertThat(button.afterClick).isEqualTo(1);78 }79 for (Component button : otherButtons) {80 assertThat(button.beforeClick).isEqualTo(0);81 assertThat(button.afterClick).isEqualTo(0);82 }83 List<WebElement> elements = new ArrayList<>();84 for (Component button : buttons) {85 elements.add(unwrapElement(button.getElement()));86 }87 assertThat(beforeClick).containsExactlyElementsOf(elements);88 assertThat(afterClick).containsExactlyElementsOf(elements);89 }90 @BeforeClickOn91 private void beforeClickOn(FluentWebElement element) { // NOPMD UnusedPrivateMethod92 beforeClick.add(element.getElement());93 }94 @Test95 void findBy() {96 goTo(DEFAULT_URL);97 Component htmlComponent = el("html").as(Component.class);98 htmlComponent.el("button").present();99 Component otherHtmlComponent = el("html").as(Component.class);100 otherHtmlComponent.present();101 assertThat(htmlComponent.beforeFindBy).hasSize(1);102 assertThat(htmlComponent.afterFindBy).hasSize(1);103 assertThat(htmlComponent.beforeFindBy).containsExactly(By.cssSelector("button"));104 assertThat(htmlComponent.afterFindBy).containsExactly(By.cssSelector("button"));105 assertThat(otherHtmlComponent.beforeFindBy).isEmpty();106 assertThat(otherHtmlComponent.afterFindBy).isEmpty();107 }108}...
Component
Using AI Code Generation
1import org.fluentlenium.adapter.FluentTest;2import org.fluentlenium.adapter.junit.FluentTestRunner;3import org.fluentlenium.core.annotation.Page;4import org.fluentlenium.core.hook.wait.Wait;5import org.junit.Test;6import org.junit.runner.RunWith;7import org.openqa.selenium.WebDriver;8import org.openqa.selenium.htmlunit.HtmlUnitDriver;9@RunWith(FluentTestRunner.class)10public class AnnotationsComponentEventsTest extends FluentTest {11 private AnnotationsComponentEventsPage page;12 public WebDriver getDefaultDriver() {13 return new HtmlUnitDriver();14 }15 public void testComponentEvents() {16 page.go();17 page.isAt();18 page.checkEvents();19 }20}21package org.fluentlenium.test.annotations;22import org.fluentlenium.core.FluentPage;23import org.fluentlenium.core.hook.wait.Wait;24import org.fluentlenium.core.inject.FluentInjector;25import org.fluentlenium.core.inject.Page;26import org.fluentlenium.test.components.AnnotationsComponent;27import org.openqa.selenium.WebDriver;28public class AnnotationsComponentEventsPage extends FluentPage {29 private AnnotationsComponent component;30 public String getUrl() {31 }32 public void isAt() {33 assertThat(title()).contains("FluentLenium");34 }35 public void checkEvents() {
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!