Site icon TestingDocs.com

WebDriver Actions in Selenium

Overview

In this article we will discuss about commonly used WebDriver Actions used in Selenium Webdriver automation.

WebDriver Actions

click() action

One of the most commonly used WebDriver action is click action. We can perform the action with .click( ) action.We will wrap the WebDriver click method to custom method with logging functionality to console details of the action success or failure .Below is the snippet for .click( ) action:

 

public void click(By locator, String webelementName)
            throws Throwable {
        boolean clickstatus = false;
        try {
            driver.findElement(locator).click();
            clickstatus = true;
        } 
        catch (Exception e) 
          {
                    System.out.println(e.getMessage());
          } 
          finally {
            if (!clickstatus) {
            	System.out.println("Click Fail on" + webelementName);
                
            } else if (clickstatus) {
            	System.out.println("Click Success on : " + webelementName);

            }
        }
    }


 

Example Usage 

click(By.name(“btnG”), “SearchButton”);

click(By.id(“lst-ib”, “SearchBox”);

Type action

2. We often enter test data into textboxes in our automation scripts. We would see below how to write a method to enter testdata into textbox and log the details. WebDriver uses .sendKeys() method .

The below method enter the data specified by the By. locator .We will log an entry in the console as shown in the below snippet.

public void type(By locator, String data, String webelementName)
      throws Throwable {
    boolean status = false;
    try {
      driver.findElement(locator).clear();
      driver.findElement(locator).sendKeys(data);
      status = true;

    } 
                  catch (Exception e) {	}
                  finally {
      if (!status) {
        System.out.println("Type failed on : " + webelementName);
        
      } else if (status) {
        System.out.println("Type Success on : " + webelementName);
      }
    }
    
  }


 

3. Now we will see how to check an element is present or not on the webpage.
If the element is present / or not we will log an entry in the console as shown in the
below snippet.

 

public void isElementPresent(By by, String webElemantName) 
throws Throwable {
    boolean status = false;
    try {
      driver.findElement(by);
      status = true;
      
    } catch (Exception e) 
                   {
      System.out.println(e.getMessage());			
    } 
                finally {
      if (!status) {
        System.out.println("Failure: Element not present on the webpage");
      } else if (status) {
        System.out.println("Success : Element present" + webElemantName);
      }

    }
  }

 

Mouse Actions in WebDriver Automation.

In this section we will discuss about different operations that may be performed using mouse in WebDriver automation.

Mouse Over operation

Mouse Over operation on the web element. Actions class has methods to perform different mouse operation. Below is sample method to demonstrate mouseover on a web element.

public void mouseover(By locator, String webElemantName) 
throws Throwable {
    boolean status = false;
    try {
            	WebElement welement = driver.findElement(locator);
      new Actions(driver).moveToElement(welement).build().perform();
      status  = true;
      
    } catch (Exception e) {

      
    } finally {
      if (!status ) {
        System.out.println(" Fail MouseOver is not perform :" 
+ webElemantName);

      } else if (status ) {

        System.out.println("Success MouseOver Action performed: "
 + webElemantName);
      }
    }
  }

 

Drag and Drop mouse action .

We need source and target web element locators to perform this mouse action . Dragging from the source and dropping it in the target element.

Below code snippet illustrates the mouse drag and drop action. You may replace it with your logging statements or System.out.println() statements to print on the console.

 

public void draganddrop(By source, By target, String webelementName) 
throws  Throwable {
    boolean dragstatus = false;
    try {
      WebElement from = driver.findElement(source);
      WebElement to = driver.findElement(target);
      new Actions(driver).dragAndDrop(from, to).perform();
      dragstatus = true;
      
    } catch (Exception e) {

      
    } finally {
      if (!dragstatus) {
        System.out.println("Fail : DragAndDrop is not performed: " 
+ webelementName);

      } else if (dragstatus) {

        System.out.println("Success : DragAndDrop is performed "
 + webelementName);
      }
    }
  }

 

Selenium Tutorial on this website:

https://www.testingdocs.com/selenium-webdriver-tutorial/

More information on Selenium, visit official website:

https://www.selenium.dev/

Exit mobile version