Steps to configure Selenium WebDriver with java to develop test scripts.
Pre-conditions: We need below files
Step 1: Download and Install JDK 8
Follow below URL
http://www.oracle.com/technetwork/java/javase/downloads/jdk8- downloads-2133151.html
Select Accept License Agreement radio button
Choose version to download: Windows 86 or x64 based on your OS (jdk-8-windows-i586.exe and jdk-8-windows-x64.exe)
- wait until complete download.
- copy downloaded file and paste in personal folder.
- click on setup to start installation.
- click on next until installation will go to end
- click on close.
- Restart Computer.
Note:
After completion of JDK installation tester can create environment variable for JAVA_HOME in your windows system by following below navigation.
- Click on start
- Right click on my computer
- Click on properties
- Click on Advanced system setting
- Select Advanced tab
- Click on Environment variable button
- Go to user variables
- click on New button
- Type JAVA_HOME and provide path of JDK folder
- click on ok
Follow below image
- Go to system variable
- Select path
- click on Edit button
- Add path of JDK bin folder followed by semicolon(;) at the end of existing path value
- Click on ok
- Click on ok
Follow below image
How to verify java successfully installed or not into your local system
- Open the command prompt.
- Run command "java -version"
It should show your installed java version detail as shown in above image. That means java is installed successfully into your local system and you are ready to use it.
Step 2: Download and launch Eclipse IDE
- IDE stands for Intigrated Development Environment. Eclipse Ide is useful to write our test scripts in java or java based tools like selenium.
- We need to follow below URL to download and launch Eclipse Ide
http://www.eclipse.org/downloads/packages/release/Mars/2
Download the Eclipse Mars, you can choose version Windows 32 bit or 64 bit based on your OS
- wait until complete download.
- copy downloaded file and paste into personal folder and extract
- click on Eclipse folder
- Here click on 'eclipse.exe' file directly. Here no need to install eclipse into your local system.
Step 3: Download Selenium WebDriver Jar Files.
After completion of eclipse Ide launching with the creation of project, package, and class with main(), we need to download selenium webdriver jar file. Follow the below steps :
- Go to google search
- Enter seleniumhq then search it
- Click on first link(https://www.seleniumhq.org/download/)
- Click on download
- Scroll to Selenium Client & WebDriver Language Bindings
- Click on download for java based selenium webdriver
- wait until complete download.
- copy downloaded jar file and paste into personal folder and extract
Step 4: Associate selenium webdriver with eclipse Ide.
- click on 'eclipse.exe' to start eclipse
- First time when you start eclipse software application, it will ask you to select your workspace where your work will be stored as shown in bellow image.
Choose a workspace folder name as D:\workspace and click on ok button. Here You can change the workspace location from 'Switch Workspace' under 'file' menu of eclipse.
Create new project
- Go to File
- New
- Go to Java Project and give your project name 'SeleniumProject'
- Click on finish button.
Create new package
- Right click on source folder(src)
- New
- Package
- give the package name ‘SeleniumTests’
- Click on finish button.
Create New Class
- Right click on package ‘SeleniumTests’
- New Class
- give the class name 'myTestLogin'
- click on Finish button.
Add external jars for that project
- Right click on project 'SeleniumProject'
- go to Java build path
- configure build path
- >select Libraries
- Click on add external JARs button
- Then go your jar file folder path location
- select both .jar files from D:\selenium-java 3.12.0.
- click on open button
- Click on add external JARs button
- Then go your jar file folder path location
- select all .jar files from D:\selenium-java 3.11.0\libs
- click on open button
- click on ok button
Steps to Download and Install firebug Add-on.
Follow my below url to install FireBug
https://addons.mozilla.org/en-US/firefox/addon/firebug/
Click on Add to Firefox button
Click on Install button
After install FireBug then go to
Tools > Web Developer > Firebug > Open Firebug (or)you can use shortcut key ‘F12’
After open the firebug. Right-click on field or element in your application then click on inspect element with firebug option. In HTML code take either id(or)name(or)className etc. from highlighted section.
See below image
Note: Here no need to restart your Firefox after installing Firebug Add-On
Steps to Download and Install firepath Add-on.
Follow my below url to install Firepath add-on
https://addons.mozilla.org/en-US/firefox/addon/firepath/
Click on Add to Firefox button
Click on Install button
Click on Restart Now
After install firepath then go to
Tools > Web Developer > Firebug > Open Firebug (or)you can use shortcut key ‘F12’
You could find ‘FirePath’ as shown below image
Selenium WebDriver First Program :
TestSteps:
- Open the firefox browser
- Navigate the application url
- Get the Title of the WebPage
- Print the title of the webpage
- Verify Title of the WebPage
- Enter the username
- Enter the password
- Clicking On Login Button
- Identify and get the Dashboard Text
- Print the Dashboard Text
- To verify whether the welcome page successfully opened or not
- Clicking On Logout Button
- Close the current Firefox Browser
Selenium Code:
package seleniumProject;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class SampleClass {
public static void main(String[] args)
throws InterruptedException {
// open the firefox browser
WebDriver driver = new FirefoxDriver();
// navigate the application url
driver.get("http://www.kosmiktechnologies.com/seleniumLiveProject/eKart/admin/");
//Get the Title of the WebPage String title=driver.getTitle();
//print the title of the webpage
System.out.println(title);
//Verify Title of the WebPage
if (title.equals("Administration")) {
System.out.println("title verified successfully");
} else {
System.out.println("title not verified successfully");
}
// Enter the username
driver.findElement(By.name("username")).sendKeys ("Kadmin");
// Enter the password
driver.findElement(By.name("password")).sendKeys ("K@admin");
// Clicking On Login Button
driver.findElement(By.xpath("//*[@id='content']/ div/div/div/div/div[2]/form/div[3]/button")).cli ck();
// Identify and get the Dashboard Text
String text = driver.findElement(By.xpath("//*[@id='content']/ div[1]/div/h1")).getText();
// Print the Dashboard Text System.out.println(text);
// To verify whether the welcome page successfully opened or not
if (text.equals("Dashboard")) {
System.out.println("Welcome page verified successfully");
} else {
System.out.println("Welcome page not verified successfully");
}
// Clicking On Logout Button
driver.findElement(By.xpath("//*[@id='header']/u l/li[3]/a")).click();
// Close the current Firefox Browser driver.close();
}
}
SELENIUM | BROWSER COMMANDS :
1. get(): This will be used to navigate the application url
Syntax : driver.get("appURL”);
2. getTitle(): This will be used to get the title of the current Webpage.
Syntax : driver.getTitle();
3. findElement(): By using this we can identify single web element On a webpage.
Ex : Button, checkbox,radio button etc.
Syntax : driver.findElement(By.Locator("Locator-Value"));
4. findElements() : By using this we can identify list of webelements On a webpage.
Ex :
a. Suppose if you want to identify dropdown values at a time then we use findElements method.
b. Suppose if you want to identify list of frames in a webpage then we use findElements method.
c. Suppose if you want to identify list of links in a webpage then we use findElements method.
Syntax :
List total_Links = driver.findElements(By.tagName("a"));
5. sendKeys() : If you want to give the value to the input field(textbox,text area) then we use sendkeys method.
Syntax :
driver.findElement(By.locator("locator-value")).sendKeys("text");
6. getText() : If you want to get the particular text in a webpage then we use getText method.
Syntax :
String PageText=driver.findElement(By.Locator(“Locator- value”)).getText();
7. click(): Is there any click operation in your application then we use click().
Syntax :
driver.findElement(By.Locator("Locator-value")).click();
8.close() : This will be used to close the current browser window.
Syntax : driver.close();
Important Interview Questions :
1. How to Identify WebElement in selenium webdriver?
A )By using firebug and firepath we can Identify WebElement.
2. Can we use firebug and firepath for chrome and Ie ?
No,
A )Suppose if u want to identify the WebElement In chrome and ie here no need to install firebug and firepath. Becoz chrome and ie have its won developer tool to identify the webElement.
3, What is the default package of selenium?
A) org.openqa.selenium
4. What is the difference between findElement() and findElements()?
findElement(): By using this we can identify single webelement On a webpage.
Ex: Button, checkbox, radio button etc.
Syntax : driver.findElement(By.Locator("Locator-Value"));
TestSteps :
-
-
-
- Open the Firefox browser
- navigate the application URL
- Enter the username
- Close the current Firefox Browser
-
-
Selenium Program :
package seleniumProject;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class findElementMethod {
public static void main(String[] args)
throws InterruptedException {
// open the firefox browser
WebDriver driver = new FirefoxDriver();
// navigate the application url
driver.get("http://www.kosmiktechnologies.com/seleniumLiveProject/eKart/admin/");
// Enter the username
driver.findElement(By.name("username")).sendKeys ("Kadmin");
// Close the current Firefox Browser driver.close();
}
}
findElements(): By using this we can identify list of web elements On a webpage.
Ex :
a. Suppose if you want to identify dropdown values at a time then we use findElements method.
b. Suppose if you want to identify list of frames in a webpage then we use findElements method.
c. Suppose if you want to identify list of links in a webpage then we use findElements method.
Syntax :
List total_Links = driver.findElements(By.tagName("a"));
TestSteps
-
-
- Open the firefox browser
- navigate the application URL
- Identify all the links and store into total_Links variable
- Print the total Links on a webpage
- Close the current Firefox Browser
-
Selenium Program :
package seleniumProject;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import
org.openqa.selenium.firefox.FirefoxDriver;
public class findElementsMethod {
public static void main(String[] args)
throws InterruptedException {
// open the firefox browser
WebDriver driver = new FirefoxDriver();
// navigate the application url
driver.get("http://www.kosmiktechnologies.com/seleniumLiveProject/eKart/admin/");
//Identify all the links and store into total_Links variable
List total_Links = driver.findElements(By.tagName("a"));
//Print the total Links on a webpage System.out.println(total_Links.size());
// Close the current Firefox Browser driver.close();
}
}
5. How to verify particular text on a webpage using selenium webdriver? (Or)
How to verify successful message on a webpage using selenium webdriver?
TestSteps :
-
-
- Open the Firefox browser
- navigate the application URL
- Enter the username
- Enter the password
- Clicking On Login Button
- Identify and get the Dashboard Text
- Print the Dashboard Text
- To verify whether the welcome page successfully opened or not
- Clicking On Logout Button
- Close the current Firefox Browser
-
Selenium Program :
package seleniumProject;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import
org.openqa.selenium.firefox.FirefoxDriver;
public class SampleClass {
public static void main(String[] args)
throws InterruptedException {
// open the firefox browser
WebDriver driver = new FirefoxDriver();
// navigate the application url
driver.get("http://www.kosmiktechnologies.com/se leniumLiveProject/eKart/admin/");
// Enter the username
driver.findElement(By.name("username")).sendKeys ("Kadmin");
// Enter the password
driver.findElement(By.name("password")).sendKeys ("K@admin");
// Clicking On Login Button
driver.findElement(By.xpath("//*[@id='content']/
div/div/div/div/div[2]/form/div[3]/button")).cli ck();
// Identify and get the Dashboard Text
String text = driver.findElement(By.xpath("//*[@id='content']/ div[1]/div/h1")).getText();
// Print the Dashboard Text
System.out.println(text);
// To verify whether the welcome page successfully opened or not
if (text.equals("Dashboard")) {
System.out.println("Text matched");
} else {
System.out.println("Text not matched");
}
// Clicking On Logout Button
driver.findElement(By.xpath("//*[@id='header']/u l/li[3]/a")).click();
// Close the current Firefox Browser driver.close();
}
}
Syntax :
If(Actual_value.equals(Expected_value))
{
//Execute the code if condition is true
}else
{
//Execute the code if condition is false
}
Actual_value : Take the text from Testing Application.
Expected_value : client requirement
6. Difference between close() and quit() in selenium webdriver ?
Driver.close() : By using this we can close the current browser window.
I.e Single window on which driver is having focus that will be closed only
Driver.quit(): Every associated browser will be closed
I.e closed all the windows opened by the driver.
7. What is the purpose of public static void main() in java?
public:
Since, main() should be available to JVM. It should be declared as public. If we don’t use main() as a public then it doesn’t make itself available to JVM and JVM can not execute it.
Static:
We should be call the main() without creating an object,such methods are called static methods and we should be declared as static.
Void :
If main() it does not written any value then you have to put void(no value) before the main().
main():
If main() is not written in the java program then JVM will not execute the program.main() is the starting point for JVM to start execution of java.
What are the pre-requisites to run the selenium webdriver?
1. JDK 8
2. Eclipse IDE
3. Selenium JAR file
4. Testing Application
5. Browser (Firefox 53.0)
6. Firebug
7. Firepath
Click here: How to handle frame in selenium webdriver using java?
How to verify multiple checkboxes in a webpage using selenium?
Selenium Training in Hyderabad
kosmik technologies is the best institute for selenium training in Hyderabad. we offer classroom and selenium online training courses by certified faculty. Selenium is an open source tool for web application testing. This tool primarily developed in JavaScript and browser technologies and hence supports all major browsers on all the platforms. Selenium is one of the best tool available in the market for a web application.