Selenium User Interactions

Selenium User Interactions

Table of Contents

Selenium Webdrivers will be the most frequently used tool that are available in the selenium tool set. It is also important to understand how to use the selenium to interact with the webapps. In the module, we have to interact the actions that are against those GUI objects like:

1.Text Box Interactions

We may put values to text box using the ‘sendkeys’ method. We can also retrieve text from the text box using the getattribute(“Value”)command.

selenium_ide_177

For example:

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.*;

import org.openqa.selenium.firefox.FirefoxDriver;

public class webdriverdemo {

   public static void main(String[] args) throws InterruptedException {  //This is the main function

      WebDriver driver = new FirefoxDriver();

      // It will put a Implicit wait, Will wait for 10 seconds before throwing exception

      driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

      //To  Launch website

      driver.navigate().to(“http://www.calculator.net/percent-calculator.html”);

      // To Maximize the browser

      driver.manage().window().maximize();

      //  when we enter value 10 in the first number of the percent Calculator

      driver.findElement(By.id(“cpar1”)).sendKeys(“10”);

      Thread.sleep(5000);

      // text box from the application

      String result = driver.findElement(By.id(“cpar1”)).getAttribute(“value”);

      //It Prints a Log In message to the screen

      system.out.println(” The Result   ” + result);

      // Close the Browser.

      driver.close();

   }

}

Output

selenium_ide_183

2.Radio Button Selection

We can select Radio button by using ‘Click’ method and also unselect using the same ‘click ‘method. Consider an example, how we interact with the radio button using https://www.calculator.net/mortgage-payoff-calculator.html. It can also check if a radio button is selected.

selenium_ide_178

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.*;

import org.openqa.selenium.firefox.FirefoxDriver;

public class webdriverdemo {

   public static void main(String[] args) throws InterruptedException {  //It will show an exception

      WebDriver driver = new FirefoxDriver();

      //It will put an Implicit wait, Will wait for 10 seconds before throwing exception

      driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

      // To Launch website

      driver.navigate().to(“http://www.calculator.net/mortgage-payoff-calculator.html”);

      driver.manage().window().maximize();

      // To Click on Radio Button

      driver.findElement(By.id(“ccpayoff1”)).click();

      system.out.println(“The Output IsSelected ” +

         driver.findElement(By.id(“ccpayoff1”)).isSelected());

      system.out.println(“The Output IsEnabled ” +

         driver.findElement(By.id(“ccpayoff1”)).isEnabled());

      system.out.println(“The Output IsDisplayed ” +

         driver.findElement(By.id(“ccpayoff1”)).isDisplayed());

      //Close the Browser.

      driver.close();

   }

}

Output

selenium_ide_179

3.Synchronization

Here to synchronise between the script execution and application we need to wait after performing appropriate actions.

Thread.Sleep

Thread.Sleep is a static wait and it is not good way to use in the script without conditions.

Thread.Sleep(1000);

Explicit Waits

Explicit waits for a certain condition to occur before proceeding further. It will be mainly used when we want to click or may act an object once it is visible.

WebDriver driver = new FirefoxDriver ();

driver.get (“Enter an URL”S);

WebElement DynamicElement = 

   (new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated (By.id (“DynamicElement”)));

Implicit wait

Implicit Wait is mainly used in cases where the Web driver will not locate the object immediately because of its non availability. The Webdriver will wait for specified implicit wait time and also not try to find the element again during the specified time period.

When the specified time limit is crossed the webdriver will try to search the element will be once again for one last time.It proceeds with execution upon failure it throws exception.

WebDriver driver = new FirefoxDriver();

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

driver.get(“Enter an URL”);

WebElement DynamicElement = driver.findElement(By.id(“DynamicElement”));

FluentWait

A FluentWait instance may explain the maximum amount of time to wait for condition to take place as well as the frequency with which check the existence of an object condition.

Wait wait = 

   new FluentWait(driver).withTimeout(60, SECONDS).pollingEvery(10, SECONDS).ignoring(NoSuchElementException.class);

   WebElement dynamicelement = wait.until(new Function<webdriver,webElement>() {

   public WebElement apply(WebDriver driver) {

      return driver.findElement(By.id(“dynamicelement”));

   }

});

  1. Checkbox Interaction

We can select check box by using the ‘click’ method and uncheck by using the same ‘click’ method. Consider to interact with a check box using can https://www.calculator.net/mortgage-calculator.html. We can also see if checkbox is visible.

selenium_ide_180


public class webdriverdemo {

   public static void main(String[] args) throws InterruptedException {//throws an exception

      WebDriver driver = new FirefoxDriver();

      It will put a Implicit wait, Will wait for 10 seconds before throwing exception

      driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

      //Launch website

      driver.navigate().to(“http://www.calculator.net/mortgage-calculator.html”);

      driver.manage().window().maximize();

      //Click on check Box

      driver.findElement(By.id(“caddoptional”)).click();

      system.out.println(“The Output  IsSelected ” +

         driver.findElement(By.id(“caddoptional”)).isSelected());      

      system.out.println(“The Output  IsEnabled ” +

         driver.findElement(By.id(“caddoptional”)).isEnabled());

      system.out.println(“The Output  IsDisplayed ” +

         driver.findElement(By.id(“caddoptional”)).isDisplayed());

      driver.close();

   }

Output

selenium_ide_181

Questions:

  1.  What is Selenium User Interactions?
  2.  What is Explicit and Implicit interactions explain with example?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share this article
Subscribe
By pressing the Subscribe button, you confirm that you have read our Privacy Policy.
Need a Free Demo Class?
Join H2K Infosys IT Online Training
Enroll Free demo class