Skip to content
This repository has been archived by the owner on Dec 14, 2019. It is now read-only.

Commit

Permalink
implement composition of Elements
Browse files Browse the repository at this point in the history
  • Loading branch information
titusfortner committed Apr 1, 2019
1 parent 756b0ee commit 9dbc8fe
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 28 deletions.
52 changes: 52 additions & 0 deletions src/test/elements/Element.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package test.elements;

import org.openqa.selenium.*;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class Element {
private WebDriver driver;
private By locator;

public Element(WebDriver driver, By locator) {
this.driver = driver;
this.locator = locator;
}

public void click() {
while (true) {
try {
driver.findElement(locator).click();
return;
} catch (NoSuchElementException e) {
waitForElement(locator);
} catch (ElementNotInteractableException e) {
waitForElementVisible(locator);
}
}
}

public void sendKeys(String string) {
while (true) {
try {
driver.findElement(locator).sendKeys(string);
return;
} catch (NoSuchElementException e) {
waitForElement(locator);
} catch (ElementNotInteractableException e) {
waitForElementVisible(locator);
}
}
}

private void waitForElement(By locator) {
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.presenceOfElementLocated(locator));
}

private void waitForElementVisible(By locator) {
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.visibilityOfElementLocated(locator));
}

}
30 changes: 5 additions & 25 deletions src/test/pages/BasePage.java
Original file line number Diff line number Diff line change
@@ -1,32 +1,12 @@
package test.pages;

import test.elements.Element;
import org.openqa.selenium.*;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

class BasePage {
WebDriver driver;
class BasePage {
WebDriver driver;

void click(By locator) {
while (true) {
try {
driver.findElement(locator).click();
return;
} catch (NoSuchElementException e) {
waitForElement(locator);
} catch (ElementNotInteractableException e) {
waitForElementVisible(locator);
}
Element getElement(By locator) {
return new Element(driver, locator);
}
}

private void waitForElement(By locator) {
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.presenceOfElementLocated(locator));
}

private void waitForElementVisible(By locator) {
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.visibilityOfElementLocated(locator));
}
}
6 changes: 3 additions & 3 deletions src/test/pages/HomePage.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ public HomePage(WebDriver driver) {
}

public void signIn(User user) {
sendKeys(username, user.getUsername());
sendKeys(password, user.getPassword());
click(submit);
getElement(username).sendKeys(user.getUsername());
getElement(password).sendKeys(user.getPassword());
getElement(submit).click();
}


Expand Down

0 comments on commit 9dbc8fe

Please sign in to comment.