Wednesday 25 November 2015

Selenium WebDriver : Basic User Actions

Let us have a look at some basic user actions such as :


  1. Open Browser
  2. Open URL
  3. Sending data to Text Field
  4. Mouse Click
  5. Select Drop-down Option




Open Browser :

This is the first step towards writing Test Case Automation Script (for Web Applications). There are different ways to Open different browsers programmatically. Here we will discuss about three browsers : Mozilla Firefox, Internet Explorer, Google Chrome.

We need to download required Driver Servers before opening a Web Browser in Selenium WebDriver Script :


To download Driver Servers of other browsers, click here



Mozilla Firefox

If Selenium JARS version < 3.0 :

WebDriver driver = new FirefoxDriver();


If Selenium JARS version >= 3.0 :

File geckoDriver = new File("path to \\geckoDriverServer.exe"); System.setProperty("webdriver.gecko.driver", geckoDriver.getAbsolutePath() );
WebDriver driver = new FirefoxDriver();

 

Internet Explorer

File IEDriver = new File("path to \\IEDriverServer.exe");
System.setProperty("webdriver.ie.driver", IEDriver.getAbsolutePath() );
WebDriver driver = new InternetExplorerDriver();



Google Chrome
 
System.setProperty("webdriver.chrome.driver", "path to \\chromedriver.exe");
WebDriver driver = new ChromeDriver();




WebDriver is the interface included in the Selenium JARs package. It contains various methods to perform different user actions.





Open URL :

Following line of code can be used to open a URL in any browser mentioned above :

driver.get(URL String);

Example :
driver.get("https://www.google.co.in/");

This opens the required URL (or Website) in the browser initiated programmatically.






Sending data to Text Field :

This is one of the most frequently used User Action during Automation Script development. It is used while logging into a site, filling form data, etc.
Following line of code is used to fill data in a Text Field :

driver.findElement(By.cssSelector(locator)).sendKeys(data);

locator - This is the locator used to locate Web Element using one of the locating methods.
data - This is the String that we want to pass to the Text Field.






Mouse Click :

This is another frequently used User Action used to log in, Save form data, select drop-down option, etc.
Following line of code is used for a Mouse Click :

driver.findElement(By.cssSelector(locator)).click();

Here locator can be any Web Element such as a button, drop-down option or any element that a user can click.






Select Drop-down Option :

Selenium provides us with some inbuilt functions to select an option from drop-down menu.

Select option = new Select(driver.findElement(By.id(locator)));
option.selectByVisibleText(Drop down Text);

                         or
option.selectByIndex(Index Value);
                         or
option.selectByValue(Value Attribute)


Example : Let us consider this HTML code snippet

<html>
<body>
<select id = "designation">
          <option value = "MD">MD</option>
          <option value = "prog"> Programmer </option>
          <option value = "CEO"> CEO </option>
</option>
</select>
<body>
</html>


To select Programmer, we can use any of the below methods :

Select option = new Select(driver.findElement(By.id(locator)));
option.selectByVisibleText("Programmer");
                    or
option.selectByIndex(1);
                    or
option.selectByValue("prog")


 


These are some of the User Actions used during Automation Script Development.


We will discuss about some advanced User Actions in upcoming posts.
Happy Learning.

YouTube Channel : Click Here

No comments:

Post a Comment