Selenium basically automates browsers. You can refer more details about it on the link http://www.seleniumhq.org/. Selenium also emulates user interaction with a web page.
WebDriver is an official W3C Specification and it is basically a method of interacting with a web browser. Initially, with the presence of Selenium RC, one could operate Selenium along with the browser by injecting javascript in order to interact with the elements.
But with the adoption of WebDriver, browsers like Google, Mozilla and Microsoft were released with the ability to be controlled by a hook that Selenium can tap into. This hook enables Selenium to interact with the Web browser the same way a human being does.
In order to explore Selenium API we need to have the following pre-requisites:
Any version of Eclipse is acceptable.
Installing Java is a must
Download Selenium jar from seleniumhq.com
Create a Java project with the name “selenium-api-exploring” and then create a simple Java class comprising of main method as shown below:
package com.javabykiran.api;
public class FirstExample {
public static void main(String[] args) {
// selenium code goes here………
}
}
Add the jar file of selenium in your Java project which contains many classes having byte code which means class files will be there but not the source code i.e. Java file.
Now right click on project >> go to properties >> libraries tab >> click on add external jar >> browse selenium jar ( i.e where you have stored it in your computer).
After that click on “OK”.
Once it has been added to your project it will look as shown below:
“Selenium API” is not developed by “Oracle” and so we need to add the “JAR” file into our project and by default, we only have built-in predefined Java code base in eclipse, for e.g. classes like System, String.
Now we will start to learn by writing a simple selenium code inside main method.
You can use “Ctrl + space” shortcut key provided by Eclipse for auto completion of any existing class names.
But here we will write „Web and “Ctrl + space” key.
While writing “WebDriver” we will observe that package is coming from org.openqa.selenium as shown above and this is the selenium jar which we just added.
Now we will see the source of “WebDriver” as shown in the below screen.
Right click on “WebDriver” and select “Open Declaration” or press F3.
Here we need to browse the source file.
You need to download the Selenium source code here separately in order to see it. You can download it from http://javabykiran.com/selenium/download/
Now you can read the source code which is in readable form.
The above process is just to see the source code of any API.
Remember, we will always use byte code for execution, but in order to explore more we need to read API, so we performed the above procedure.
Below are some of the important selenium interfaces we need to look at.
This is an interface in Java having various methods. In order to see all the methods we use the shortcut key “Ctrl + O” or else in Menu Bar go to Window > Show View > outline as shown below:
Now select any method in “Outline” and start reading and remember what is the use of every method.
get()
getTitle()
getPageSource()
getWindowHandles()
getWindowHandle()
getCurrentUrl()
findElement(By arg0)
findElements(By arg0)
manage()
navigate()
close()
quit()
clear() // If the element is a text entry, this will clear the value.
click() // “Click” this element.
FindElement(By by) // Finds the first WebElement using the given method.
findElements(By by) // Finds all elements within the current context using the given mechanism.
getAttribute(String name) // Gets the value of the given attribute of the element.
getCssValue(String propertyName) // Gets the value of a given CSS property.
getLocation() // Gets the location of the rendered element.
getSize() // Gets the width and height of the rendered element
getTagName() // Gets the tag name of the element.
getText() // Gets the visible text
isDisplayed() // show whether is the element displayed or not
isEnabled() // shows whether is the element currently enabled or not
isSelected() // Determines whether or not the element is selected or not.
sendKeys(String keysToSend) // This method is used to simulate typing into an element which may set its value.
submit() // If the current element is a form or an element within a form, then this will be used to submit it to the remote server.
As we have discussed about adding the selenium standalone jar in the project, is enough for automating any website.
Some points to be noted before going ahead.
Selenium jar and Firefox version should be compatible.
Here we have used selenium-standalone version 52 and Firefox version 42.0
Make sure your Firefox is installed in the program files.
Keep the Firefox updates off by going to Options >> advanced >> update >> never update option.
Add Firebug add-on to Firefox.
Add Firepath add-on to Firefox.
Also add the WebDriver Element Locator add-on to Firefox.
Refer the below given image for all add-ons in the firefox.
For the above example, write the below given code inside main method.
// opens a firefox browser
WebDriver driver=new FirefoxDriver();
// opening of url driver.get("www.gmail.com");
// maximize window driver.manage().window().maximize();
Here findElement is a very important method which returns the “WebElement”. After this we need to call methods of WebElement as shown above from API.
We will now try to locate the username text box in our website, so that the text can be simulated into the text box. See the below given screen:
Observe tag marked with number (4) in the above screen where we will observe that id has a unique identifier for this textbox. This textbox is unique by this id.
Here the id is called as locator type. There are many types of locators like id, xpath, name etc. Here email is locator value which will be different for different textbox/input controls of html.
Now as we see our program performs the following tasks as mentioned below based on the code given below the tasks:
Opening of browser
Entering login details
Click login button
Click logout
Close browser
package com.javabykiran.api;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.seleni- um.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class FirstExample {
public static void main(String[] args) {
// opens a Firefox browser
WebDriver driver = new FirefoxDriver();
// opening of url driver.get(“http://www.javabykiran.com/selenium/demo/”);
//Maximize window driver.manage().window().maximize();
//locate email element
WebElement userEle = driver.findElement(By.id(“email”));
//enter uname into text box userEle.sendKeys(“[email protected]”);
//locate password element
WebElement passwordEle = driver.findElement(By.id(“password”));
//enter password into text box passwordEle.sendKeys(“123456”);
//locate signin button
WebElement signInbuttonEle = driver.findElement(By.xpath(“.//*[@ id=’form’]/div[3]/div/button”));
//click signin button signInbuttonEle.click();
//locate logout button
WebElement logoutButtonEle = driver.findElement(By.xpath(“ht- ml/ body/div/header/nav/div/ul/li/a”));
//click logout button logoutButtonEle.click();
}
}
Full-fledged automation for offline website. Try the below given points while doing automation,
Remove redundancy
This is very important aspect while designing framework.
As a beginner we may write any kind of raw code but later we will discover that all things are redundant.
In order to avoid this just create some methods which will replace the redundant code and this methods can be called as and when and wherever we need it.
Create utility classes.
Zip file creation
Excel read
Screenshot
Folder Structure
automation
utility
common functionality
test cases
configure
data