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

Selenium Grid - Distributed Systems

Selenium grid is powerful and can be easily used with just few lines of code. A Grid is a network of computers which can be heterogeneous and geographically dispersed. A Selenium Grid is how a grid of computers are connected using Selenium and configured to perform a task. A task typically here is running a suite of test cases with different browsers in different platforms across geographically dispersed systems. The test cases can be run in parallel using Selenium. So if we have a grid of three computers the same set of test cases can be run in IE, Firefox and Chrome. To establish this we have three important components. 1) Hub 2) Node 3) Test Script A Hub acts like the server to which requests are sent . The Hub sends to request to various registered nodes. The HUB can be instantiated using command line by using the selenium server standalone jar file. Open command prompt, navigate to the folder which has the selenium server file and use this command. java -jar selen...

Selenium - What Why Who How?

What is Selenium? Selenium is an open source automated   testing   suite for web applications across different browsers and platforms.   It is quite similar to HP Quick Test Pro ( QTP ) only that Selenium focuses on automating web-based applications. Why use Selenium? Selenium is fast and it supports programming in multiple languages like Java, C#,Python and Ruby.  So development of automation scripts can be done in your language of preference. Selenium works well with AJAX testing. Selenium Grid supports execution of test cases in multiple platforms and multiple browsers in remote systems as well.  The support for different programming languages gave selenium the power to harness the capabilities of those programming languages .  For example, using JAVA as the programming language to code selenium scripts enables selenium to use JDBC for database access and testing.All the APIs supported in Java also become the advantage of Selenium like L...

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 ...