Following are the some API functions and it implementation
1. How to click on element: By using click() function we perform click operation on page element:
2. How to enter text into page element: By using sendkeys() function we enter text in page element such as text field, area etc:
3. How to count total number rows in table:
4. How to get page title: We use getTitle() function to get title of page.
5. How to get page source of webpage: we use getPageSource() function to get page source.
6. How to get current url : we use driver.getCurrentUrl() function to get current url
7. How to assert text of webpage: we get attribute by using getText () method.
8. How to get element attribute: we get attribute by using getAttribute() method.
9. How to double click on element: we use Action class object doubleClick() method to perform this.
10. How to perform drag and drop: we will use Action class object doubleClick method to perform this.
11. How to execute java script code: To execute java script code we use JavascriptExecutor class.
12. How to capture screen shot: we use TakesScreenshot interface to get page screen shot.
13. How to maximize window: we use windowMaximize() to maximize window.
14. How to automate check boxes: using below code we can click on check box and verifying check box is selected etc.
15. How to automate radio button: : using below code we can click on check box and verifying check box is selected etc.
16. How to handle drop down: by using select class we can automate dropdown following are the some main operation which we perform on drop down list.
By using selectByVisibleText() method of Select class we can select visible text of dropdown.
Select drop down by value
Select drop down by index
Verify dropdown supported multiple options or not
Get number of options are available in drop down
1. How to click on element: By using click() function we perform click operation on page element:
WebElement el = driver.findElement(By.id("ElementID"));
el.click();
WebElement el = driver.findElement(By.id("ElementID"));
el.clear(); //use to clear input
field
el. sendKeys (“User name”);
3. How to count total number rows in table:
List rows =
driver.findElements(By.className("//table[@id='tableID']/tr"));
int totalRow = rows.size();
4. How to get page title: We use getTitle() function to get title of page.
String title = driver.getTitle()
5. How to get page source of webpage: we use getPageSource() function to get page source.
String pagesource =
driver.getPageSource()
6. How to get current url : we use driver.getCurrentUrl() function to get current url
String currentURL = driver.getCurrentUrl()
7. How to assert text of webpage: we get attribute by using getText () method.
WebElement el = driver.findElement(By.id("ElementID"));
//get test from element and stored in
text variable
String text = el.getText();
//assert text from expected
Assert.assertEquals("Element
Text", text);
8. How to get element attribute: we get attribute by using getAttribute() method.
WebElement el =
driver.findElement(By.id("ElementID"));
//get test from element and stored in
text variable
String attributeValue = el. getAttribute("AttributeName")
();
//assert text from expected
Assert.assertEquals("Attribute
Value", attributeValue);
9. How to double click on element: we use Action class object doubleClick() method to perform this.
WebElement el =
driver.findElement(By.id("ElementID"));
Actions builder = new
Actions(driver);
builder.doubleClick(el).build().perform();
10. How to perform drag and drop: we will use Action class object doubleClick method to perform this.
WebElement source =
driver.findElement(By.id("Source ElementID"));
WebElement destination =
driver.findElement(By.id("Taget ElementID"));
Actions builder = new
Actions(driver);
builder.dragAndDrop(source, destination
).perform();
11. How to execute java script code: To execute java script code we use JavascriptExecutor class.
JavascriptExecutor js =
(JavascriptExecutor) driver;
String title = (String)
js.executeScript("pass your java scripts");
12. How to capture screen shot: we use TakesScreenshot interface to get page screen shot.
File file=
((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(file, new
File("c:\\name.png"));
13. How to maximize window: we use windowMaximize() to maximize window.
driver.manage().window().maximize();
14. How to automate check boxes: using below code we can click on check box and verifying check box is selected etc.
WebElement el =
driver.findElement(By.id("element Id"));
//to perform check operation
el.click()
//verfiy to check box it return true
if selected else false
el.isSelected()
15. How to automate radio button: : using below code we can click on check box and verifying check box is selected etc.
WebElement el =
driver.findElement(By.id("Radio button id"));
//to perform check operation
el.click()
//verfiy to radio button is check it
return true if selected else false
el.isSelected()
16. How to handle drop down: by using select class we can automate dropdown following are the some main operation which we perform on drop down list.
By using selectByVisibleText() method of Select class we can select visible text of dropdown.
Select sel= new
Select(driver.findElement(By.id("drop down ID")));
sel.selectByVisibleText("Pass
your desire Text");
Select sel= new
Select(driver.findElement(By.id("drop down ID")));
sel. selectByValue( ("Pass your
desire Text");
Select sel= new
Select(driver.findElement(By.id("drop down ID")));
sel. selectByIndex (0); // 0 is first index
Select sel= new
Select(driver.findElement(By.id("drop down ID")));
Boolean status = sel. isMultiple()
Select sel= new
Select(driver.findElement(By.id("drop down ID")));
int totalOption = sel. getOptions().size()
Great article. Any chance you know how handling multiple dropdown boxes, without id's, on a single page is done?
ReplyDeleteAwesome Post
ReplyDeletehow to get the selected values for any combo box ?
ReplyDeletegot the solution --->
ReplyDelete@Test
public void Login() {
static String URL = "https://www.facebook.com/";
driver.get(URL);
String xpathExpression = "//select[@name='birthday_month']";
WebElement ele = driver.findElement(By.xpath(xpathExpression));
Select select = new Select(ele);
select.selectByVisibleText("Jan");
String s = select.getFirstSelectedOption().getText();
Assert.assertEquals("Jan", s);
}
Thanks for your input...
DeleteHow to select element if their id Hover effect???
ReplyDeleteHow to select the element if their is Hover Effect??
ReplyDeleteHover effect is done through Actions
Deletecan you pls share the code if possible
DeleteActions hov = new Actions(driver);
DeleteWebElement el = driver.findElement(By.id('idname'));
hov.moveToElement(el).build.perform();
DeleteWebElement el = driver.findElement(By.id("ElementID"));
Actions builder = new Actions(driver);
builder.moveToElement(el).build().perform();
wow superb article ....very usefull
DeleteThanks for very useful information. Please also provide more examples
ReplyDeletegood one. Thanks for the info
ReplyDeleteHi All,
ReplyDeleteHow select check box for corresponding content(Web element name), If web page is dynamically changing ?