com.testvagrant.ekam.atoms.mobile.Element Maven / Gradle / Ivy
package com.testvagrant.ekam.atoms.mobile;
import com.google.inject.Inject;
import com.testvagrant.ekam.atoms.MultiPlatformFinder;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.pagefactory.ByChained;
import org.openqa.selenium.support.ui.ExpectedConditions;
import java.time.Duration;
public class Element extends BaseMobileElement {
private WebElement element;
@Inject
public Element(WebDriver driver, By locator) {
super(driver, locator);
}
@Inject
public Element(
WebDriver driver,
MultiPlatformFinder multiPlatformFinder) {
super(driver, multiPlatformFinder);
}
public String getTextValue() {
return getElement().getText();
}
public String getAttributeValue(String attribute) {
return getElement().getAttribute(attribute);
}
public Element click() {
waitUntilPresent();
getElement().click();
return this;
}
public boolean hasAttribute(String attribute) {
String value = getElement().getAttribute(attribute);
return value != null;
}
public boolean isEnabled() {
return getElement().isEnabled();
}
public boolean isPresent(Duration duration) {
try {
waitUntilCondition(ExpectedConditions.presenceOfElementLocated(locator), duration);
return true;
} catch (Exception ex) {
return false;
}
}
public boolean isDisplayed() {
try {
return getElement().isDisplayed();
} catch (Exception ex) {
return false;
}
}
public Element scrollIntoView() {
((JavascriptExecutor) webDriver).executeScript("arguments[0].scrollIntoView(true);", getElement());
return this;
}
public Element waitUntilDisplayed() {
try {
waitUntilCondition(ExpectedConditions.visibilityOfElementLocated(locator));
return this;
} catch (Exception ex) {
throw new RuntimeException(
String.format("Error waiting for element with selector: %s to be displayed.", locator));
}
}
public Element waitUntilInvisible() {
try {
waitUntilCondition(ExpectedConditions.invisibilityOfElementLocated(locator));
return this;
} catch (Exception ex) {
throw new RuntimeException(
String.format("Error waiting for element with selector: %s to be invisible.", locator));
}
}
public Element waitUntilPresent() {
try {
waitUntilCondition(ExpectedConditions.presenceOfElementLocated(locator));
return this;
} catch (Exception ex) {
throw new RuntimeException(
String.format("Error waiting for element presence with selector: %s.", locator));
}
}
public Element waitUntilDisplayed(Duration duration) {
try {
waitUntilCondition(ExpectedConditions.visibilityOfElementLocated(locator), duration);
return this;
} catch (Exception ex) {
throw new RuntimeException(
String.format("Error waiting for element with selector: %s to be displayed.", locator));
}
}
public Element waitUntilInvisible(Duration duration) {
try {
waitUntilCondition(ExpectedConditions.invisibilityOfElementLocated(locator), duration);
return this;
} catch (Exception ex) {
throw new RuntimeException(
String.format("Error waiting for element with selector: %s to be invisible.", locator));
}
}
public Element waitUntilPresent(Duration duration) {
try {
waitUntilCondition(ExpectedConditions.presenceOfElementLocated(locator), duration);
return this;
} catch (Exception ex) {
throw new RuntimeException(
String.format("Error waiting for element presence with selector: %s.", locator));
}
}
public Element waitUntilTextToBePresent(String text) {
try {
waitUntilCondition(ExpectedConditions.textToBePresentInElementLocated(locator, text));
return this;
} catch (Exception ex) {
throw new RuntimeException(
String.format(
"Error waiting for text: '%s' to be present in element with selector: %s",
text, locator));
}
}
public Element waitUntilTextToBePresent() {
try {
wait.until(() -> !getTextValue().trim().isEmpty());
return this;
} catch (Exception ex) {
throw new RuntimeException(
String.format(
"Error waiting for text to be present in element with selector: %s.", locator));
}
}
public Element waitUntilTextToBePresent(String text, Duration duration) {
try {
waitUntilCondition(
ExpectedConditions.textToBePresentInElementLocated(locator, text), duration);
return this;
} catch (Exception ex) {
throw new RuntimeException(
String.format(
"Error waiting for text: '%s' to be present in element with selector: %s",
text, locator));
}
}
public Element waitUntilTextToBePresent(Duration duration) {
try {
wait.atMost(duration).until(() -> !getTextValue().trim().isEmpty());
return this;
} catch (Exception ex) {
throw new RuntimeException(
String.format(
"Error waiting for text to be present in element with selector: %s.", locator));
}
}
public Element waitUntilTextNotToBe(String text, boolean partial) {
try {
wait.until(
() ->
partial
? !getTextValue().toLowerCase().contains(text.toLowerCase())
: !getTextValue().toLowerCase().contentEquals(text.toLowerCase()));
return this;
} catch (Exception ex) {
throw new RuntimeException(
String.format(
"Error waiting for text not to be '%s' in element with selector: %s.",
text, locator));
}
}
public WebElement getElement() {
try {
//TODO: Parametrize wait time.
wait.atMost(Duration.ofSeconds(30)).until(() -> webDriver.findElement(locator) != null);
return webDriver.findElement(locator);
} catch (Exception ex) {
throw new RuntimeException(String.format("Element with selector: %s not found", locator));
}
}
public T find(By selector, Class tClass) {
try {
return tClass
.getDeclaredConstructor(WebDriver.class, By.class)
.newInstance(webDriver, new ByChained(locator, selector));
} catch (Exception ex) {
throw new RuntimeException(String.format("Element with selector: %s not found", locator));
}
}
public T find(MultiPlatformFinder multiPlatformFinder, Class tClass) {
try {
return tClass
.getDeclaredConstructor(WebDriver.class, By.class)
.newInstance(webDriver, new ByChained(locator, buildLocator(multiPlatformFinder)));
} catch (Exception ex) {
throw new RuntimeException(String.format("Element with selector: %s not found", locator));
}
}
public Element find(By selector) {
try {
return new Element(webDriver, new ByChained(locator, selector));
} catch (Exception ex) {
throw new RuntimeException(String.format("Element with selector: %s not found", locator));
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy