Some other helpful Selenium methods

Here are a few other helpful functions for use of Selenium testing scripts as you often need to click links, fill in fields, and submit forms.


import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
/**
*
* @param driver
* @param name
* @return
*/
public static WebElement findElementByName(final WebDriver driver, final String name){
final By el = By.name(name);
final WebElement wel = driver.findElement(el);
return wel;
}
/**
*
* @param driver
* @param name
* @param value
*/
public static void sendKeysByFieldName(final WebDriver driver, final String name, final String value){
final WebElement wel = findElementByName(driver, name);
wel.sendKeys(value);
}
/**
*
* @param driver
* @param xpath
*/
public static void clickByXpath(final WebDriver driver, final String xpath){
final By el = By.xpath(xpath);
//LOGGER.info("el is {}", el);
final WebElement wel = driver.findElement(el);
wel.click();
}
/**
*
* @param driver
* @param linktext
*/
public static void waitToClickLinkText(final WebDriver driver, final String linktext){
final WebDriverWait wait = new WebDriverWait(driver, 10);
final By el = By.linkText(linktext);
wait.until(ExpectedConditions.elementToBeClickable(el));
final WebElement wel = driver.findElement(el);
wel.click();
}
/**
*
* @param driver
* @param text
* @return
*/
public boolean pageContainsText(final WebDriver driver, final String text){
final String xpathExpression = "//*[contains(text(),'" + text + "')]";
final List<WebElement> list = driver.findElements(By.xpath(xpathExpression));
return list.size() > 0;
}