Some Webdriver API functions in java and its implementation

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:

WebElement el  =  driver.findElement(By.id("ElementID"));
el.click();

2. How to enter text into page element: By using sendkeys() function we enter text in page element such as text field, area etc:

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 drop down by value

Select sel= new Select(driver.findElement(By.id("drop down ID")));
sel. selectByValue( ("Pass your desire Text");
 Select drop down by index

Select sel= new Select(driver.findElement(By.id("drop down ID")));
sel. selectByIndex (0);  // 0 is first index
 Verify dropdown supported multiple options or not

Select sel= new Select(driver.findElement(By.id("drop down ID")));
Boolean status = sel. isMultiple()
Get number of options are available in drop down

Select sel= new Select(driver.findElement(By.id("drop down ID")));
int  totalOption  = sel. getOptions().size()

15 comments:

  1. Great article. Any chance you know how handling multiple dropdown boxes, without id's, on a single page is done?

    ReplyDelete
  2. how to get the selected values for any combo box ?

    ReplyDelete
  3. got the solution --->

    @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);
    }

    ReplyDelete
  4. How to select element if their id Hover effect???

    ReplyDelete
  5. How to select the element if their is Hover Effect??

    ReplyDelete
    Replies
    1. Hover effect is done through Actions

      Delete
    2. can you pls share the code if possible

      Delete
    3. Actions hov = new Actions(driver);
      WebElement el = driver.findElement(By.id('idname'));
      hov.moveToElement(el).build.perform();

      Delete

    4. WebElement el = driver.findElement(By.id("ElementID"));

      Actions builder = new Actions(driver);
      builder.moveToElement(el).build().perform();

      Delete
    5. wow superb article ....very usefull

      Delete
  6. Thanks for very useful information. Please also provide more examples

    ReplyDelete
  7. good one. Thanks for the info

    ReplyDelete
  8. Hi All,

    How select check box for corresponding content(Web element name), If web page is dynamically changing ?

    ReplyDelete

Leave your comments, queries, suggestion I will try to provide solution