Click here to Skip to main content
13,191,642 members (63,720 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

163.4K views
11 bookmarked
Posted 24 Oct 2014

A Small Java Script Using Selenium Webdriver

, 24 Oct 2014
Rate this:
Please Sign up or sign in to vote.
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:

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)

Share

About the Author

Sipra Ray
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

You may also be interested in...

Pro
Pro

Comments and Discussions

 
QuestionAfter giving login button xpath, not able to click on Login button Pin
Member 1314756223-Apr-17 20:34
memberMember 1314756223-Apr-17 20:34 
QuestionFacing an Error Pin
Member 128326784-Nov-16 3:18
memberMember 128326784-Nov-16 3:18 
AnswerRe: Facing an Error Pin
Sourabh989018-Feb-17 18:44
memberSourabh989018-Feb-17 18:44 
QuestionRegarding WebDriver libraray Pin
Member 1232287511-Feb-16 22:59
memberMember 1232287511-Feb-16 22:59 
AnswerRe: Regarding WebDriver libraray Pin
Sourabh989018-Feb-17 19:59
memberSourabh989018-Feb-17 19:59 
Questionhow to open particular mail in gmail Pin
Member 1206042114-Oct-15 23:27
memberMember 1206042114-Oct-15 23:27 
QuestionSkype Pin
Nắng Hồng21-Sep-15 18:59
memberNắ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
memberlearner1259-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
memberSourabh989013-Aug-15 1:35 
Answergmail login and logout program Pin
sachit007recreated4-Oct-15 22:59
membersachit007recreated4-Oct-15 22:59 
GeneralRe: gmail login and logout program Pin
Member 121951709-Dec-15 17:18
memberMember 121951709-Dec-15 17:18 
QuestionUnable to logout from Gmail Using your script Pin
Swati Bhatt1-Apr-15 22:48
memberSwati Bhatt1-Apr-15 22:48 
AnswerRe: Unable to logout from Gmail Using your script Pin
Member 118899245-Aug-15 22:43
memberMember 118899245-Aug-15 22:43 
GeneralMy vote of 4 Pin
Member 1078158230-Dec-14 3:51
memberMember 1078158230-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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.171017.2 | Last Updated 25 Oct 2014
Article Copyright 2014 by Sipra Ray
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid