Saturday 15 October 2016

Selenium APIs to perform different Actions

In the tutorial we are going to learn one of the important thing about Selenium Web Driver without this you will not be able to start writing your Selenium Test Cases. In our Web application we have various web elements to access such as text boxes, radio buttons, check boxes, drop downs, multi-select dropdowns etc. Let us see how to access these web elements using selenium web driver + java.

1. Entering Values into Text box -
Entering value into input text box is very simple using selenium is very simple first locate the text box using its id,class name or xpath and then use sendKeys() method to enter values. The below line of code will enter "myname" string into textbox with id username.

Example - driver.findElement("username")).sendKeys("myname");

2. Deleting Value from textbox-
For clearing value from text box selenium provides clear() method.

Example - driver.findElement("username")).clear();

3. Selecting value with Radio Button -
We need to use click() method to toggle the radio button value.

Example -  driver.findElement(By.xpath("//input[text()='option1']")).click();

4. Check box select/deselect -
Like radio button check box toggling is done using click() method. In the below example first we locate the check box and then perform click action on it and then display the status as TRUE if the check box is selected and FALSE if check box is deselected.

Example - WebElemnt checkbox=driver.findElement("check_box"));

5. Clicking on Link Text -
To click on link text click() method is used and for selected link text linkText() or partialLinkText() is used .

Example - driver.findEment(By.linkText("Click Here")).click();

6. Drop Down Access -
1. Import package
2. Declare the drop down web element as instance of select class.
Select dropdown = new Select (driver.findElement("CityList")))
3. We can use any of the Select class methods to perform actions on the drop down

If you find this helpful then please share with your friends and post your questions and feedback in the comments section below. Add me via google+ or follow me on facebook to get latest post updates.



Post a Comment