|
4 | 4 |
|
5 | 5 | # <img src="https://raw.githubusercontent.com/SeleniumHQ/seleniumhq.github.io/690acbad7b4bf4656f116274809765db64e6ccf7/website_and_docs/static/images/logos/webdriver.svg" height=24 /> Elements for Selenium
|
6 | 6 |
|
| 7 | +Define Selenium HTML elements as Java classes, similar to Selenium's ``@FindBy`` annotation. |
| 8 | + |
| 9 | +Also contains a ton of other predefined utility, for example: |
| 10 | +* Automatically scrolling elements that perform operations into the view |
| 11 | +* Safe click: When an element is detected as stale a JavaScript click is executed instead |
| 12 | +* Option to globally wait until the page finished loading |
| 13 | +* Waiting for some time until the element is present (``waitUntil``) |
| 14 | + |
| 15 | +Overall this improves: |
| 16 | +* Less [flaky](https://www.browserstack.com/test-reporting-and-analytics/features/test-reporting/what-is-flaky-test) tests |
| 17 | +* Abstraction of elements in an object oriented way |
| 18 | + |
| 19 | +## Usage |
| 20 | + |
| 21 | +1. Define the elements you want to access |
| 22 | + ```java |
| 23 | + @FindBySelector(tagName = "body") |
| 24 | + abstract class BodyElement implements ImprovedWebElement { |
| 25 | + public MyElement myElement() { |
| 26 | + return waitForFirst(MyElement.class); |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + @FindBySelector(id = "abc") |
| 31 | + abstract class MyElement implements ImprovedWebElement { |
| 32 | + } |
| 33 | + ``` |
| 34 | +2. Utilize the predefined methods and classes to get/access the elements in a test |
| 35 | + ```java |
| 36 | + class MyWebDriverTest implements CanFindElements { |
| 37 | + WebDriver webDriver; |
| 38 | + |
| 39 | + @BeforeEach |
| 40 | + void beforeEach() { |
| 41 | + webDriver = createWebDriver(); |
| 42 | + CustomizableRemoteWebElementInstaller.install( |
| 43 | + webDriver, |
| 44 | + () -> new ImprovedRemoteWebElement( |
| 45 | + "return document.readyState == 'complete';") |
| 46 | + ); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + void test() { |
| 51 | + MyElement myElement = waitForFirst(BodyElement.class).myElement(); |
| 52 | + assertEquals("Hello world", myElement.getText()); |
| 53 | + |
| 54 | + // Or alternatively |
| 55 | + ElementInstantiatorInstance.instance().find(webDriver, BodyElement.class); |
| 56 | + } |
| 57 | + |
| 58 | + @AfterEach |
| 59 | + void afterEach() { |
| 60 | + // Stop webDriver here... |
| 61 | + webDriver = null; |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public WebDriver getWebDriver() { |
| 66 | + return webDriver; |
| 67 | + } |
| 68 | + } |
| 69 | + ``` |
7 | 70 |
|
8 | 71 | ## Installation
|
9 | 72 | [Installation guide for the latest release](https://github.com/xdev-software/selenium-elements/releases/latest#Installation)
|
|
0 commit comments