com.github.mkolisnyk.sirius.client.ui.controls.Control Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sirius-java-client Show documentation
Show all versions of sirius-java-client Show documentation
Common client library for automated GUI testing
The newest version!
package com.github.mkolisnyk.sirius.client.ui.controls;
import java.awt.Rectangle;
import java.util.HashMap;
import org.junit.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.Point;
import org.openqa.selenium.TimeoutException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import com.github.mkolisnyk.sirius.client.Configuration;
import com.github.mkolisnyk.sirius.client.ui.Page;
import com.github.mkolisnyk.sirius.client.ui.PageFactory;
import com.github.mkolisnyk.sirius.client.ui.ScrollTo;
import com.github.mkolisnyk.sirius.client.ui.SubItem;
import io.appium.java_client.MobileElement;
public class Control {
protected static final long TIMEOUT = Configuration.timeout();
private Page parent;
private By locator;
private String locatorText = "";
private String itemLocatorText = "";
private HashMap subItemsMap;
private String scrollTo;
private ScrollTo scrollDirection;
private String format;
private boolean excludeFromSearch = false;
public Control(Page parentValue, By locatorValue) {
this.parent = parentValue;
this.locator = locatorValue;
this.locatorText = this.locator.toString().replaceFirst("^By\\.(\\S+): ", "");
subItemsMap = new HashMap();
}
public WebDriver getDriver() {
return parent.getDriver();
}
public Page getParent() {
return parent;
}
public By getLocator() {
return locator;
}
public String getLocatorText() {
return locatorText;
}
public String getItemLocatorText() {
return itemLocatorText;
}
public void setItemLocatorText(String subItemLocatorText) {
this.itemLocatorText = subItemLocatorText;
}
public String getScrollTo() {
return scrollTo;
}
public void setScrollTo(String scrollToValue) {
this.scrollTo = scrollToValue;
}
public ScrollTo getScrollDirection() {
return scrollDirection;
}
public void setScrollDirection(ScrollTo scrollDirectionValue) {
this.scrollDirection = scrollDirectionValue;
}
public String getFormat() {
return format;
}
public void setFormat(String formatValue) {
this.format = formatValue;
}
public boolean isExcludeFromSearch() {
return excludeFromSearch;
}
public void setExcludeFromSearch(boolean excludeFromSearchValue) {
this.excludeFromSearch = excludeFromSearchValue;
}
public void addSubItems(SubItem[] items) {
for (SubItem item : items) {
this.subItemsMap.put(item.name(), item);
}
}
public HashMap getSubItemsMap() {
return subItemsMap;
}
public WebElement element() {
return getDriver().findElement(locator);
}
public WebElement element(int index) {
return getDriver().findElements(locator).get(index);
}
public boolean waitUntil(ExpectedCondition> condition, long timeout) {
WebDriverWait wait = new WebDriverWait(getDriver(), timeout);
try {
wait.until(condition);
} catch (TimeoutException e) {
return false;
}
return true;
}
public boolean exists(long timeout) {
this.scrollTo();
return waitUntil(ExpectedConditions.presenceOfElementLocated(locator), timeout);
}
public boolean exists() {
return exists(TIMEOUT);
}
public boolean disappears(long timeout) {
return waitUntil(ExpectedConditions.not(ExpectedConditions.presenceOfElementLocated(locator)), timeout);
}
public boolean disappears() {
return disappears(TIMEOUT);
}
public boolean visible(long timeout) {
return waitUntil(ExpectedConditions.visibilityOfElementLocated(locator), timeout);
}
public boolean visible() {
Assert.assertTrue("Unable to find element: " + this.locator.toString(), exists());
return visible(TIMEOUT);
}
public boolean invisible(long timeout) {
return waitUntil(ExpectedConditions.invisibilityOfElementLocated(locator), timeout);
}
public boolean invisible() {
Assert.assertTrue("Unable to find element: " + this.locator.toString(), exists());
return invisible(TIMEOUT);
}
public boolean enabled(long timeout) {
return waitUntil(ExpectedConditions.elementToBeClickable(locator), timeout);
}
public boolean enabled() {
return enabled(TIMEOUT);
}
public boolean disabled(long timeout) {
return waitUntil(ExpectedConditions.not(ExpectedConditions.elementToBeClickable(locator)), timeout);
}
public boolean disabled() {
return enabled(TIMEOUT);
}
public void click() {
Assert.assertTrue("Unable to find element: " + this.locator.toString(), exists());
this.element().click();
}
public T click(Class pageClass) throws Exception {
this.click();
T page = PageFactory.init(this.getDriver(), pageClass);
Assert.assertTrue(
String.format("The page of %s class didn't appear during specified timeout", pageClass.getName()),
page.isCurrent());
return page;
}
public String getText() {
Assert.assertTrue("Unable to find element with locator: " + this.getLocator(), this.exists());
return this.element().getText();
}
public String getValue() {
return this.getText();
}
public Rectangle getRect() {
this.exists();
Rectangle rect = new Rectangle();
Point location = ((MobileElement) this.element()).getCoordinates().onPage();
Dimension size = this.element().getSize();
rect.x = location.x;
rect.y = location.y;
rect.width = size.width;
rect.height = size.height;
return rect;
}
public void scrollTo() {
if (this.getScrollTo() != null && !this.getScrollTo().trim().equals("")) {
this.getParent().scrollTo(this.getScrollTo(), this.getScrollDirection());
}
}
}