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 1st 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 follow 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 that 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 belong 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 click 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… 🙂

2 thoughts on “A small Java script using Selenium Webdriver.

Leave a comment