Overview
Selenium is a suite of softwares’, each serves to different testing needs of an organization. It has 4 components.
- Selenium Integrated Development Environment (IDE)
- Selenium Remote Control (RC)
- Selenium WebDriver
- Selenium Grid
Introduction to Selenium Webdriver:
WebDriver is a web automation framework. It permits you to execute your tests against completely different browsers, not just Firefox (unlike Selenium IDE).
WebDriver also allows you to use a programming language for creation of your test scripts (not attainable in Selenium IDE).
- You can now perform conditional operations like if-then-else or switch-case
- You can also use looping like do-while.
Following programming languages are supported by WebDriver
- Java
- .Net
- PHP
- Python
- Perl
- Ruby
Download and Installation:
Step 4 – Configure Eclipse IDE with WebDriver:
Create a new project by clicking on File > New > Java Project. Name the project as “demo project”.
A new pop-up window will get opened, enter details as follow :
- Project Name
- Location to save project
- Select an execution JRE
- Select layout project option
- Click on Finish button
In this step,
- Right-click on the newly created project and
- Select New > Package, and name that package as “demo package”.
A pop-up window will get opened to name the package.
- Enter the name of the package
- Click on Finish button
Create a new Java class under new package by right-clicking on it and then selecting- New > Class, and then name it as “MyClass”.
Now add selenium JARs to Java Build Path
In this step,
- Right-click on “demo project” and select Properties.
- On the Properties dialog, click on “Java Build Path”.
- Click on the Libraries tab, and then
- Click on “Add External JARs button.”
When you click on “Add External JARs..” A pop-up window will get opened. Select all the JAR files you want to add.
Select all jar files which are inside the lib folder.
Select jar files which are outside lib folder
Add all the JAR files which are inside and outside the “libs” folder.
How to identify Web Element:
To locate elements in Webdriver, we can use “findElement(By.locator())” method.
Locators are the HTML properties of a web element by which Selenium locates the web element on which it needs to perform the action.
Selenium WebDriver Commands:
Opening a URL:-
Using Get method
- Selenium has driver.get() method which is used for navigating to a web page by passing the string URL as parameter.
- Syntax: driver.get(“http://google.com”);
Clicking on web element :-
The click() method in Selenium is used for performing the click operation on web elements.
//Clicking an element directly driver.findElement(By.id("button1")).click(); //Or by creating a WebElement first and then applying click() operation WebElement submitButton = driver.findElement(By.id("button2")); submitButton.click();
Writing in a textbox :-
The sendKeys() method is used for writing in a textbox or any element of text input type.
//Creating a textbox webElement WebElement element = driver.findElement(By.name("q")); //Using sendKeys to write in the textbox element.sendKeys("ArtOfTesting!");
Clearing text in a textbox :-
The clear() method is used to clear the text written in a textbox or any web element of text input type.
driver.findElement(By.name("q")).clear();
Fetching text written over any web element :-
We have getText() method in selenium webDriver for fetching text written over an element.
driver.findElement(By.id("element123")).getText();
Navigating backwards in a browser :-
Selenium provides navigate().back() command for moving backwards within the browser’s history.
driver.navigate().back();
Navigating forward in a browser :-
Selenium provides navigate().forward() command for moving forward in a browser.
driver.navigate().forward();
Refreshing the browser :-
- There are multiple ways for refreshing a page in Selenium WebDriver-
- Using driver.navigate().refresh() command
- Using sendKeys(Keys.F5) on any textbox on the webpage
- Using driver.get(“URL”) with current URL
- Using driver.navigate().to(“URL”) with current URL
//Refreshing browser using navigate().refresh() driver.navigate().refresh(); //By pressing F5 key on any textbox element driver.findElement(By.id("id123")).sendKeys(Keys.F5); //By reopening the current URL using get() method driver.get("http"//artoftesting.com"); //By reopening the current URL using navigate() method driver.navigate().to("http://www.artoftesting.com");
Closing the browser :-
Selenium has two commands for closing browsers: close() and quite(). The driver.close() command is used for closing the browser having focus. Whereas, the driver.quite command is used for closing all the browser instances open.
//To close the current browser instance driver.close(); //To close all the open browser instances driver.quit();
Basic Script:
package demo; import org.openqa.selenium.*; import org.openqa.selenium.chrome.*; public class firstscript { public static void main(String[] args) { System.setProperty("webdriver.chrome.driver","/home/yudiz/eclipse-workspace/facebook/lib/chrome driver/chromedriver"); driver = new ChromeDriver(); driver.get("https://www.facebook.com/"); driver.quit(); } }
Conclusion:
You might find script writing as lengthy as writing test cases but my friend, the scripts are reusable! You don’t need new scripts all the time, even if the version of the OS on the device changes. It allows you to redo the test exactly the same, without forgetting any steps. In the end you will have a better quality software which will be released earlier, with less problems and having less resources used. And it’s FUN after all! Happy Testing to You!