Click here to Skip to main content
15,885,933 members
Articles / Web Development

A Small Java Script Using Selenium Webdriver

Rate me:
Please Sign up or sign in to vote.
4.75/5 (8 votes)
24 Oct 2014CPOL2 min read 293.3K   10   17
A small Java script using Selenium Webdriver

In the previous post, we had discussed about the installation of Java, and configuration of Eclipse with selenium WebDriver. Now here, we will be creating a small Java script. All beginners will first want to open browser and to automate it. So here, we will be doing that.

First of all, we will write the scenario of what we will be doing here. Here, we will Login to Gmail account and will automate the below scenarios.

  1. Open a Firefox browser
  2. Navigate to the URL
  3. Maximize the window
  4. Enter the User Name and Password
  5. Sign-In to the Gmail Account
  6. Click on the Compose Button
  7. Sign-Out from the gmail account
  8. Close the browser

Now we will automate the above scenario. In the previous post, I had discussed about creating a Java Project, Package, Class. And how to import the JAR files into the project. Here also, we have followed the same thing. For example, we will create a Project called “Gmail” , Package as “login”, Class as “Login1″. Now, we will write the code as below:

Java
package login;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Login1 {
public static void main(String[] args) {
// Create a new instance of the Firefox driver
WebDriver driver = new FirefoxDriver();
//  Wait For Page To Load
// Put a Implicit wait, this means that any search for elements on the page
could take the time the implicit wait is set for before throwing exception 
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
// Navigate to URL
driver.get("https://mail.google.com/");
// Maximize the window.
driver.manage().window().maximize();
// Enter UserName
driver.findElement(By.id("Email")).sendKeys(" YOUR USER NAME");
// Enter Password
driver.findElement(By.id("Passwd")).sendKeys("YOUR PASSWORD");
// Wait For Page To Load
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
// Click on 'Sign In' button
driver.findElement(By.id("signIn")).click();
//Click on Compose Mail.
driver.findElement(By.xpath("//div[@class='z0']/div")).click();
// Click on the image icon present in the top right navigational Bar
driver.findElement(By.xpath("//div[@class='gb_1 gb_3a gb_nc gb_e']/div/a")).click();
//Click on 'Logout' Button
driver.findElement(By.xpath("//*[@id='gb_71']")).click();
//Close the browser.
driver.close();
}
}
  • After writing this script, you will see some RED lines under some element like this->

    1

  • By hovering over the Bulb symbol present in the left side, you can know which type of error it is.

    2

  • You just need to hover the mouse pointer on that particular element or click on that particular element and press [Ctrl+Space] on the key board. Many suggestions will be listed there and you have to select the appropriate package for that element. To know which Class belongs to which Package, click on http://selenium.googlecode.com/git/docs/api/java/index.html. See below for the images in details.

    3

    4

    5

  • Now run the script by right clicking on the class ->Run As -> Java Application.

    OR

  • Click on the Run Icon present in the Top Navigational Bar.

    6

    Now, you will see the browser will automatically open and will perform the desired task as mentioned above.

So in this article, we discussed about automating the Gmail Login and Sign-out functionality using WebDriver.

Hope this helps beginners like me… :)

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Tester / Quality Assurance
India India
I am Sipra Ray, ISTQB certified - Quality Analyst at Mindfire Solutions.
An extrovert by nature ,I like to interact with people and share with all what I learn.
I would love to share and answer queries and gain knowledge from forums like Code Project
Follow up with my blogs at:- siprabugtracker.wordpress.com

Comments and Discussions

 
Questionregarding find_element in selenium Pin
Member 140823918-Dec-18 10:01
Member 140823918-Dec-18 10:01 
QuestionSelenium installation Pin
User 1351931130-Nov-17 20:16
User 1351931130-Nov-17 20:16 
QuestionAfter giving login button xpath, not able to click on Login button Pin
Member 1314756223-Apr-17 20:34
Member 1314756223-Apr-17 20:34 
QuestionFacing an Error Pin
Member 128326784-Nov-16 3:18
Member 128326784-Nov-16 3:18 
AnswerRe: Facing an Error Pin
Sourabh989018-Feb-17 18:44
Sourabh989018-Feb-17 18:44 
QuestionRegarding WebDriver libraray Pin
Member 1232287511-Feb-16 22:59
Member 1232287511-Feb-16 22:59 
AnswerRe: Regarding WebDriver libraray Pin
Sourabh989018-Feb-17 19:59
Sourabh989018-Feb-17 19:59 
Questionhow to open particular mail in gmail Pin
Member 1206042114-Oct-15 23:27
Member 1206042114-Oct-15 23:27 
QuestionSkype Pin
Nắng Hồng21-Sep-15 18:59
Nắng Hồng21-Sep-15 18:59 
Questionprogram not able to find the "Passwd" element id in the Gmail Login program and giving exception Pin
learner1259-Aug-15 1:02
learner1259-Aug-15 1:02 
AnswerRe: program not able to find the "Passwd" element id in the Gmail Login program and giving exception Pin
Sourabh989013-Aug-15 1:35
Sourabh989013-Aug-15 1:35 
Answergmail login and logout program Pin
sachit007recreated4-Oct-15 22:59
sachit007recreated4-Oct-15 22:59 
GeneralRe: gmail login and logout program Pin
Member 121951709-Dec-15 17:18
Member 121951709-Dec-15 17:18 
QuestionUnable to logout from Gmail Using your script Pin
Swati Bhatt1-Apr-15 22:48
Swati Bhatt1-Apr-15 22:48 
AnswerRe: Unable to logout from Gmail Using your script Pin
Member 118899245-Aug-15 22:43
Member 118899245-Aug-15 22:43 
GeneralMy vote of 4 Pin
Jay Bardeleben30-Dec-14 3:51
professionalJay Bardeleben30-Dec-14 3:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.