Skip to content

Instantly share code, notes, and snippets.

@tjmaher
Created July 29, 2015 02:23
Show Gist options
  • Select an option

  • Save tjmaher/9894bd667b21756bc572 to your computer and use it in GitHub Desktop.

Select an option

Save tjmaher/9894bd667b21756bc572 to your computer and use it in GitHub Desktop.
package utils;
import org.openqa.selenium.*;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.TestException;
public abstract class CommonUtils {
public WebDriver _driver;
public WebDriverWait wait;
public Actions actions;
public CommonUtils() {
_driver = DriverUtils.getFirefoxDriver();
}
public void sendKeys(By selector, String value) {
WebElement element = getElement(selector);
clearField(element);
try {
element.sendKeys(value);
} catch (Exception e) {
throw new TestException(String.format("Error in sending [%s] to the following element: [%s]", value, selector.toString()));
}
}
public void clearField(WebElement element) {
try {
element.clear();
} catch (Exception e) {
System.out.print(String.format("The following element could not be cleared: [%s]", element.getText()));
}
}
public void click(By selector) {
WebElement element = getElement(selector);
waitForElementToBeClickable(selector);
try {
element.click();
} catch (Exception e) {
throw new TestException(String.format("The following element is not clickable: [%s]", selector));
}
}
public void waitForElementToBeClickable(By selector ) {
try {
wait = new WebDriverWait(_driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(selector));
} catch (Exception e) {
throw new TestException("The following element is not clickable: " + selector);
}
}
public void waitForElementToBeVisible(By selector) {
try {
wait = new WebDriverWait(_driver, 10);
wait.until(ExpectedConditions.presenceOfElementLocated(selector));
} catch (Exception e) {
throw new NoSuchElementException(String.format("The following element was not visible within [%s] seconds: %s ", "10".toString(), selector));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment