Last week we stated to present an automation testing framework by using: java, webdriver, intellij idea, junit, maven, log4j, hudson. This week we will continue creating a class that will contain functions in order to work with controls.
ControlUtils
In this chapter we will write functions for working with controls and also how to use them.
FluentWait
Before starting we need a way to wait an element on the page until it will be displayed. We may either use an implicit wait, by waiting a number of seconds or either waiting the element until is displayed on the page.
- implicit wait
public static void wait_time(long fWait){
try {
Thread.sleep(fWait);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Use it like this:
wait_time(1000); --> this will wait 1 second
- explicit wait
public static WebElement fluentWait(final By locator){
long timeStart = System.currentTimeMillis();
Wait<WebDriver> wait = new FluentWait<WebDriver>(selenium)
.withTimeout(30, TimeUnit.SECONDS)
.pollingEvery(5, TimeUnit.SECONDS)
.ignoring(NoSuchElementException.class);
WebElement foo = wait.until(
new Function<WebDriver, WebElement>() {
public WebElement apply(WebDriver driver) {
return driver.findElement(locator);
}
}
);
long timeEnd = System.currentTimeMillis();
logger.info("Element waited for: " + (timeEnd - timeStart) + " ms.");
return foo;
};
Functions for Controls
- get_text
public static String get_text(By byValue) {
WebElement element = fluentWait(byValue);
String elementText = null;
elementText = element.getText();
return elementText;
}
This function will get the control's text and can be used like this:
String submitButton = ControlUtils.get_text(By.id("loginBtn"));
- verify text
public static boolean verify_text(By byValue, String fText) {
String fTextOut = fluentWait(byValue).getText();
if (fTextOut.equals(fText)) {
return true;
} else {
return false;
}
}
This function will return true or false and will check if the control's text is correct. You can use it like this.boolean submitButton = ControlUtils.verify_text(By.id("loginBtn"), "Sign in");
Assert.assertEquals("Submit button text is not correct", true, submitButton);
- element_present
public static boolean element_present(By byValue) {
if (fluentWait(byValue).isDisplayed()) {
return true;
} else {
return false;
}
}
This function will return true or false and will check if the control is present or not on the page. You can use it like this.
Assert.assertEquals("Delete button is not present", true, ControlUtils.element_present(By.id("btnLeaveDelete")));
public static void click_element(By byValue)
{
WebElement element = fluentWait(byValue);
element.click();
}
This function will click an element on the page. Use it like this:ControlUtils.click_element(By.id("loginBtn"));
- double_click_element
public static void double_click_element(By byValue){ WebElement elementToDoubleClick = fluentWait(byValue); Actions action=new Actions(selenium); action.doubleClick(elementToDoubleClick); action.perform(); }
This is used to double-click an element. Usage:
ControlUtils.double_click_element(By.id("loginBtn"));
- get_attribute
public static String get_attribute(By byValue, String fAttribute) {
return fluentWait(byValue).getAttribute(fAttribute);
}
The function will get a specific attribute from a control. Usage:String startDateText = ControlUtils.get_attribute(By.id("inputleaveStartDate"), "value");
- is_attribute_presentpublic static boolean is_attribute_Present(WebElement element, String attribute)
{
Boolean result = false;
try {
String value = element.getAttribute(attribute);
if (value != null){
result = true;
}
} catch (Exception e) {}
return result;
}
This will check if an attribute of a control is present or not. Imagine for example that you have to test if a control is or not disabled. Usage:
WebElement disapproveButton = fluentWait(By.id("btnLeaveDisapprove"));
Assert.assertEquals("Disapprove button is not disabled", true, ControlUtils.isAttributePresent(disapproveButton, "disabled"));
- send_keys
public static void send_keys(By byValue, String fText)
{
WebElement element = fluentWait(byValue);
element.sendKeys(fText);
}
This function will write some text in a control. Usage:
ControlUtils.send_keys(By.id("inputleaveStartDate"), "01-01-2014");
- refresh_pagepublic static void refresh_page() {
Actions actionObject = new Actions(selenium);
actionObject.keyDown(Keys.CONTROL).sendKeys(Keys.F5).perform();
}
If you need to refresh the page use this function.
- functions for selection controls
// Clear all selected entries. This is only valid when the SELECT supports * multiple selectionspublic static void select_deselect_all(By byValue, String fxPath){
Select selectBox = new Select(fluentWait(byValue));
selectBox.deselectAll();
}
//Select none the option at the given index. This is done by examining the * "index" attribute of an element, and not merely by countingpublic static void select_deselect_by_index(By byValue, int index) {
Select selectBox = new Select(fluentWait(byValue));
selectBox.deselectByIndex(index); }
//Select none all options that have a value matching the argument. */public static void select_deselect_by_value(By byValue, String value) {
Select selectBox = new Select(fluentWait(byValue));
selectBox.deselectByValue(value);
}
//Select none all options that display text matching the argumentpublic static void select_deselect_by_visible_text(By byValue, String matchingArgument) {
Select selectBox = new Select(fluentWait(byValue));
selectBox.deselectByVisibleText(matchingArgument);
}
//Gets all selected options belonging to this select tag.public static String select_get_all_selected_options(By byValue) {
Select selectBox = new Select(fluentWait(byValue));
List<WebElement> selectOptions = selectBox.getAllSelectedOptions();
String selectText = "";
String cumulatedSelectText = "";
for (WebElement temp : selectOptions) {
selectText=temp.getText();
cumulatedSelectText= cumulatedSelectText + " " + selectText;
System.out.println("cumulatedSelectText: " + cumulatedSelectText);
}
return cumulatedSelectText;
}
//Gets the first selected option in this select tag (or the currently * selected option in a normal selectpublic static String select_get_first_selected_option(By byValue) {
Select selectBox = null;
selectBox = new Select(fluentWait(byValue));
System.out.println(selectBox.getFirstSelectedOption().getText());
return selectBox.getFirstSelectedOption().getText();
}
//Gets all options belonging to this select tagpublic static void select_get_options(By byValue) {
Select selectBox = new Select(fluentWait(byValue));
List<WebElement> selectOptions = selectBox.getOptions();
for (WebElement temp : selectOptions) {
System.out.println("getText" + temp.getText());
}
}
//Whether this select element support selecting multiple options at the * same time? This is done by checking the value of the "multiple" * attribute.public static void select_is_multiple(By byValue) {
Select selectBox = new Select(fluentWait(byValue));
System.out.println(selectBox.isMultiple());
}
//Select all options that have a value matching the argument.public static void select_select_by_value(By byValue, String value) {
Select selectBox = new Select(fluentWait(byValue));
selectBox.selectByValue(value);
}
//Select the option at the given indexpublic static void select_select_by_index(By byValue, int index) {
Select selectBox = new Select(fluentWait(byValue));
selectBox.selectByIndex(index);
}
//Select all options that display text matching the argumentpublic static void select_select_by_visible_text(By byValue, String matchingArgument) {
Select selectBox = new Select(fluentWait(byValue));
selectBox.selectByVisibleText(matchingArgument);
}
Next week we will present how to install and set java, webdriver, intellij idea, junit, maven, log4j, hudson. I hope you liked this article. Happy testing and... make it green, becomes a dream :).
No comments:
Post a Comment