Skip to main content

Form - User Input Automation


Text boxes


WebElement t=driver.findElement(By.id(“username”));
t.sendkeys(“test name”);


Buttons

WebElement t=driver.findElement(By.id(“createbutton”));
t.click();


DropDown Boxes 

WebElement select = driver.findElement(By.tagName("select"));
List<WebElement> allOptions = select.findElements(By.tagName("option"));
for (WebElement option : allOptions) {
System.out.println(String.format("Value is: %s", option.getAttribute("value")));
option.click();
}

This will find the first “SELECT” element on the page, and cycle through each of its OPTIONs in turn, printing out their values, and selecting each in turn. As you will notice, this isn’t the most efficient way of dealing with SELECT elements. WebDriver’s support classes include one called “Select”, which provides useful methods for interacting with these

Select oSelection = new Select(driver.findElement(By.tagName("select")));
oSelection.deselectAll(); //All options under the select tag are deselected
oSelection.deselectByIndex(index); //Option with mentioned index is deselected
oSelection.deselectByValue(value);//Option with mentioned value is deselected
oSelection.deselectByVisibleText(text);//Option with mentioned text is deselected
select.selectByVisibleText("Edam");//Option with text Edam is selected
select.selectByIndex(index);//Option with mentioned index is selected
select.selectByValue(value);//Option with mentioned value is selected
select.getOptions().size();//returns the number of options in select element
select.getOptions().get(i).getText();//returns the text of the option with index i

Submit Form

Once you’ve finished filling out the form, you probably want to submit it. One way to do this would be to find the “submit” button and click it:

Element.submit();

RadioButton\Checkbox

Radiobuttons and Checkboxes can be handled in the same way. They are identified using the name attribute. The difference between the two is you can select more than one checkbox but you can select only one radio button.

List oRadioButton = driver.findElements(By.name("nameofradiobutton"));

// Create a boolean variable which will hold the value (True/False)
boolean bValue = false;
// This statement will return True, in case of first Radio button is selected
bValue = oRadioButton.get(0).isSelected();
// This will check that if the bValue is True means if the first radio button is selected
if(bValue = true){
// This will select Second radio button, if the first radio button is selected by default
oRadioButton.get(1).click();
System.out.println(oRadioButton.get(i).getAttribute("value"));
}else{
// If the first radio button is not selected by default, the first will be selected
oRadioButton.get(0).click();
}


Actions-Drag and Drop

Here’s an example of using the Actions class to perform a drag and drop. Native events are required to be enabled.

WebElement element = driver.findElement(By.name("source"));

WebElement target = driver.findElement(By.name("target"));

(new Actions(driver)).dragAndDrop(element, target).perform();

Actions class supports many actions like moveToElement(),contextClick(),moveByOffset(x,y),click(),doubleclick(),clickAndHold(),release() ,keyDown() and keyUp()also.

build

public Action build()

Generates a composite action containing all actions so far, ready to be performed (and resets the internal builder state, so subsequent calls to build() will contain fresh sequences).

Returns:

the composite action
Example:
Actions builder = new Actions(driver); builder.clickAndHold(listItems.get(1)).clickAndHold(listItems.get(2)) .click();
Action selectMultiple = builder.build(); 
selectMultiple.perform();

perform

public void perform()

A convenience method for performing the actions without calling build() first.
Example:
(new Actions(driver)).dragAndDrop(element, target).perform();

WebTable


A webtable is made of table, th, tr ,td values.
Use xpath to point to the table. Xpath can be obtained using developer tools like firebug. Handling tables can be done in three parts. To handle tables you need to understand the concepts of xpath very well.
Part 1 – Location of the table in the webpage </html/body/div[1]/div[2]/div/div[2]/article/div/>

Part 2 – Table body (data) starts from here <table/tbody/>

Part 3 – It says table row 2 and table column 1 <tr[2]/td[1]>

driver.findElement(By.xpath("/html/body/div[1]/div[2]/div/div[2]/article/div/table/tbody/tr[2]/td[1]")).getText();

For dynamically selecting row and column, use variables.

driver.findElement(By.xpath("/html/body/div[1]/div[2]/div/div[2]/article/div/table/tbody/tr[“+ srow +”]/td[“ +scol+ ”]")).getText();


String sColValue = "Licensing";

//First loop will traverse through columns

for (int i=1;i<=noofcolumns;i++){

String sValue = null;

sValue = driver.findElement(By.xpath(".//*[@id='post-2924']/div/table/tbody/tr[1]/th["+i+"]")).getText();

if(sValue.equalsIgnoreCase(sColValue)){

// If the sValue match with the description, it will initiate one more inner loop for all the columns of 'i' row
for (int j=1;j<=2;j++){

String sRowValue= driver.findElement(By.xpath(".//*[@id='post-2924']/div/table/tbody/tr["+j+"]/td["+i+"]")).getText();
System.out.println(sRowValue);
}
break;
}
}

Finding number of rows and columns in a webtable
int rowCount=driver.findElements(By.xpath("//table[@id='DataTable']/tbody/tr")).size();
int columnCount=driver.findElements(By.xpath("//table[@id='DataTable']/tbody/tr/td")).size();

The drop down menus or special keyboard actions can be done using Actions class. File upload is not handled in selenium by default. You need AutoIT , another tool to automate that part. We can see it later. 

Comments

Popular posts from this blog

How to Install Selenium and execute your first program

You can install and test Selenium using the following steps. All files can be downloaded from  http://www.seleniumhq.org/download/   Download Selenium Server jar file   Download client library java jar file   Download InternetExplorer Driver file   Install eclipse workspace. (download eclipse. Unzip folder. Find eclipse.exe application and open it) Create a new java project with 1,2 as references Copy 3 to workspace For IE, go to internet options->security and change the security level so that it’s the same for all zones. Write sample java program Below program opens google in IE and searches for selenium test and closes it. // All the imports happen automatically when you use eclipse ide and add the downloaded jars in the project references package test; import org.openqa.selenium.Alert; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.ie...

Transition from Test Automation Engineer to Test Engineer

What's the difference? Test Automation Engineer works on automating the Regression Test Suite. Test Engineer ensures the product can be released to the customer. With my industry experience, I have realized, that, as part of automation engineer your job duties don't stop with creating Automation tests, Frameworks and running them. With more tests being automated, your responsibilities not only include running the complete Regression suite but occasionally validation and verification of the product. There are different documents/sites defining different types of testing and where their usage, however,but finding bugs is a different skill. Though we are testers by role, our analytical minds work like developers as we develop scripts too. We could be biased. When something doesn't work, we look for a workaround and think it's no big deal. This attitude is a deal breaker!Instead use your skills to analyse and break the code. Here I am going to list some of my ideas ...