Tutorial: Selenium Automation
Using Selenium WebDriver to test elements on Facebook Login Page
This tutorial would help you to login into your Facebook Account using Selenium WebDriver using
FireFox Browser. The code discussed in this tutorial can be easily extended to design test cases to
test the various UI elements of the Facebook Login Page.
Step 1: Create a new Java Project using Eclipse IDE.
(Tested with Eclipse Standard/SDK, Version: Kepler Release)
Step 2: Create a new Package inside it named testingDemo.
Step 3: Create a new class in the testingDemo package, named SeleniumTestingClass.
Copy the code written below: [or use SeleniumTestingClass.java]
package testingDemo;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.By;
public class SeleniumTestingClass {
public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver","D:\\geckodriver.exe");
WebDriver driver= new FirefoxDriver();
driver.get("http://www.facebook.com");
WebElement emailElement = driver.findElement(By.id("email"));
emailElement.sendKeys("your login id here");
WebElement passwordElement = driver.findElement(By.id("pass"));
passwordElement.sendKeys("your password here");
WebElement loginElement = driver.findElement(By.id("u_0_2"));
loginElement.click();
System.exit(0);
}
Important:
Download 1:
Download the following jar files:
a) selenium-server-standalone-3.6.0.jar
(Link: http://www.seleniumhq.org/download/)
b) client-combined-3.6.0.jar
(Link: http://www.seleniumhq.org/download/)
Add them to your project and add them to the Build Path of the project.
Download 2:
For interfacing with FireFox browser, you need to download the following jar file and
place it on your system:
a) geckodriver.exe
(Link: https://github.com/mozilla/geckodriver/releases)
[Modify the path for this file accordingly in your code]
Tip:
In case you get a compilation error on the following line for the sendKeys() method:
emailElement.sendKeys("your login id here");
Follow these steps in your Eclipse:
1. Right click on your java project and select Build Path -> Click on
Configure Build Path...
2. In project properties window, Click Java Compiler at the left
panel
3. At the right panel, change the Compiler compliance level from 1.4 to 1.7
(Select which is higher version in your eclipse)
4. Lastly click on Apply and OK
Related Explanation:
In order to extract the unique ids of individual UI elements of a webpage, right click on the UI
element and select “Inspect Element”. You will see some HTML code which gets open in a
window at the bottom of the page. You will also see the selected element as being highlighted
in the code. Now right-click on this selected code and select ‘Copy-> Outer HTML’ and paste
on a notepad.
You will get the unique id of this element which can be used in Java code for referring this
element.
Refer figure below: