Web driver script for Gmail Login Test with valid and invalid test data.

In this article I will be writing script for gmail login using various test Cases.

Test scenario-

CASE – 1 [ Enter Correct user Name and Password ]

  • Check whether Email field exist or not, if exit then write correct email Id
  • Check whether Password field exist or not,if exit then write correct password
  • Check if sign-in button exist or not, if exist then click on sign-in.
  • Check if login was proper or not by comparing the text of Primary tab.
  • Click on the account link containing Sign-Out button and click on Sign-Out button.
  • Check whether Signout was proper or not.

CASE – 2 [ Both Email and Password Fields are blank. ]

  • In the Email field & Password field, don’t  enter any data.
  •  click on sign-in.

CASE – 3 [ Email field is filled and Password field is blank. ]

  • Enter correct Email Id in Email field.
  • In the Password field, don’t  enter any data.
  •  click on sign-in.

CASE – 4 [ Email field is blank and Password field is filled ]

  • Enter correct Password in Password field.
  • In the Email field, don’t  enter any data.
  •  click on sign-in.

CASE – 5 [ Email and Password are entered wrong ]

  • Enter wrong Email Id in Email field.
  • Enter wrong Password in password field.
  •  click on sign-in.

CASE – 6 [ Email is wrong and Password is correct ]

  • Enter Incorrect Email in Email field.
  • Enter correct Password in Password field.
  •  click on sign-in.

CASE – 7 [ Email is correct and Password is wrong ]

  • Enter correct Email Id in Email field.
  • Enter incorrect Password in Password field.
  •  click on sign-in.
  • Close the browser.

Now we will write the code as below.

package package1;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Gmail {
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    // Initialize WebDriver
    WebDriver driver = new FirefoxDriver();

    // Maximize Window
   driver.manage().window().maximize();
  
    // Wait For Page To Load
    driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);

   //Navigate to Google webstites
   driver.get("https://www.gmail.com");

 
/*CASE- 1. Both User name and Password are entered correctly. 
Check whether Email field exists or not */
    try
        {
          WebElement a1 = driver.findElement(By.xpath("//*[@id='Email']"));
          System.out.println("---------Email field exists --------------\n-----------------------");
          a1.sendKeys("ENTER CORRECT MAIL ID");
         }
    catch(Throwable e)
         {
         System.out.println("Email field not found: " + e.getMessage());
         }

    //Check whether Password field exists or not
     try
        {
	 WebElement password = driver.findElement(By.xpath("//*[@id='Passwd']"));
         System.out.println("----------Password field exits ------------\n-----------------------");
         password.sendKeys("ENTER CORRECT PASSWORD");
        }
    catch(Throwable e)
        {
	 System.out.println("Password field not found: " + e.getMessage());
        }

     //Asserting the Sign In button exists or not and clicking it
    try
       {
	WebElement button = driver.findElement(By.id("signIn"));
        System.out.println("-------Sign In button exists----------\n-----------------------");
       //To uncheck the "Check sign in" checkbox
       WebElement check_stay_sign_in = driver.findElement(By.xpath("//*[@id='PersistentCookie']"));
        check_stay_sign_in.click();   
	button.click();
        }
    catch(Throwable e)
        {
	System.out.println("Sign In button not found: "+ e.getMessage());
        }
    //Check if login was proper or not
    try
        {
	WebElement GmailText = driver.findElement(By.xpath("//*[@id=':36']"));
        String text = GmailText.getText();
        if(text.equals("Primary"))
       {
	System.out.println("----------Sucessful Login -------\n-----------------------");
       }else
       {
	System.out.println("----------Login Failure ----------\n-----------------------");
	}
       }
     catch(Throwable e)
        {
	 System.out.println("Inbox link not found: "+e.getMessage());
        }
    //===
    //Asserting and click on the Account link which contain Signout button.
     try
        {
	WebElement person = driver.findElement(By.xpath("//*[@id='gb']/div[1]/div[1]/div[2]/div[5]/div[1]/a/span"));
        System.out.println("--------The Account link containing Signout button exists ---------\n-----------------------");
	person.click();
       }
    catch(Throwable e)
       {
	System.out.println("Link for the drop-down not found: "+e.getMessage());
        }


    //Asserting and clicking on the Signout button.
    try
       {	
	WebElement signout = driver.findElement(By.xpath("//*[@id='gb_71']"));
        System.out.println("--------Sign out button exists  ---------\n-----------------------");
	signout.click();
        }
    catch(Throwable e)
        {
	System.out.println("Sign out button not found: "+e.getMessage());
        }

    //Check whether Signout was proper or not.
    try
       {	
       WebElement GmailText = driver.findElement(By.xpath("//div[@class = 'banner']/h1"));
       String text = GmailText.getText();
       if(text.equals("One account. All of Google."))
       {
       System.out.println("----------Sign out was successful -------");
       }
     else
       {
        System.out.println("----------Sign out wasn't successful ----------");
	}
        }

    catch(Throwable e)
        {
	System.out.println("Sign out link not found: "+e.getMessage());
        }


    // CASE- 2. Both Email and Password Fields are blank.
    try
        {
	WebElement button = driver.findElement(By.id("signIn"));
button.click();			        
WebElement GmailText = driver.findElement(By.xpath("//*[@id=':36']"));
String text = GmailText.getText();
if(text.equals("Primary"))
{
   System.out.println("----------Sucessful Login -------");
}
else
{
	System.out.println("----------Login Failure ----------");
		}
		
}
catch(Throwable e)
{
		System.out.println("Error! Email and Password fields are blank. \n----------------------- ");
System.out.println("Element not found: "+e.getMessage() + "\n-----------------------");
  }


// CASE- 3. Email field is filled and Password field is blank
try
{		
		WebElement email = driver.findElement(By.id("Email"));
email.sendKeys("abcd123@gmail.com");
WebElement button = driver.findElement(By.id("signIn"));
button.click();


WebElement GmailText = driver.findElement(By.xpath("//*[@id=':36']"));
String text = GmailText.getText();
if(text.equals("Primary"))
{
System.out.println("----------Sucessful Login -------\n-----------------------");
}
else
{
System.out.println("----------Login Failure ----------\n-----------------------");
		}
		
 }
catch(Throwable e)
 {
		System.out.println("Error! Password field is blank. \n-----------------------");
System.out.println("Element not found: "+e.getMessage() + "\n-----------------------");
 }	

driver.findElement(By.id("Email")).clear();			// Clearing the Email field


// CASE- 4. Email field is blank and Password field is filled

try
{		
		WebElement password = driver.findElement(By.id("Passwd"));
password.sendKeys("ENTER PASSWORD");
WebElement button = driver.findElement(By.id("signIn"));
button.click();


WebElement GmailText = driver.findElement(By.xpath("//*[@id=':36']"));
String text = GmailText.getText();
if(text.equals("Primary"))
{
System.out.println("----------Sucessful Login -------");
}
else
{
System.out.println("----------Login Failure ----------");
		}
			
}
catch(Throwable e)
{
    	System.out.println("Error! Email field is blank. \n-----------------------");
System.out.println("Element not found: "+e.getMessage() + "\n-----------------------");
 }
  
driver.findElement(By.id("Passwd")).clear();			// Clearing the Password field


//CASE- 5. Email and Password are entered wrong 	

try
{
		 WebElement email = driver.findElement(By.id("Email"));
 email.sendKeys("ENTER INCORRECT MAIL ID");
 WebElement password = driver.findElement(By.id("Passwd"));
 password.sendKeys("ENTER INCORRECT PASSWORD");
 WebElement button = driver.findElement(By.id("signIn"));
 button.click();
 
 WebElement GmailText = driver.findElement(By.xpath("//*[@id=':36']"));
 String text = GmailText.getText();
 if(text.equals("Primary"))
 {
 System.out.println("----------Sucessful Login -------");
 }
 else
 {
 System.out.println("----------Login Failure ----------");
		 }
		 
  }
catch(Throwable e)
{
	
	  System.out.println("Error! Incorrect Email and Password. \n-----------------------");		  
  System.out.println("Element not found: "+e.getMessage() + "\n-----------------------");
}
 
driver.findElement(By.id("Email")).clear();			// Clearing the Email field
driver.findElement(By.id("Passwd")).clear();			// Clearing the Password field
		
	
 // CASE- 6. Email is wrong and Password is correct 	
 try 
 {
	 
		WebElement email = driver.findElement(By.id("Email"));
email.sendKeys("ENTER INCORRECT EMAIL ID");
WebElement password = driver.findElement(By.id("Passwd"));
password.sendKeys("ENTER CORRECT PASSWORD");
WebElement button = driver.findElement(By.id("signIn"));
button.click();
WebElement Inbox = driver.findElement(By.xpath("//*[@id=':53']/div/div[1]/span/a"));
if(Inbox != null) 
{
System.out.println("Sucessful Login \n -----------------");
} 
else 
{
System.out.println("Login Failure");
		}
  } 
 catch(Throwable e) 
 {
	  
	  System.out.println("Error! Incorrect Email. \n-----------------------");
  System.out.println("Element not found: "+e.getMessage() + "\n-----------------------");
  }
 
  driver.findElement(By.id("Email")).clear();			// Clearing the Email field
  driver.findElement(By.id("Passwd")).clear();			// Clearing the Password field
  
  
//CASE- 7. Email is correct and Password is wrong 	
 try
 {
		 WebElement email = driver.findElement(By.id("Email"));
 email.sendKeys("ENTER CORRECT EMAIL ID");
 WebElement password = driver.findElement(By.id("Passwd"));
 password.sendKeys("ENTER INCORRECT PASSWORD");
 WebElement button = driver.findElement(By.id("signIn"));
	 button.click();
	 
 
	 WebElement GmailText = driver.findElement(By.xpath("//*[@id=':36']"));
 String text = GmailText.getText();
 if(text.equals("Primary"))
 {
 System.out.println("----------Sucessful Login -------");
 } 
 else
 {
 System.out.println("----------Login Failure ----------");
			}
  }
 catch(Throwable e)
 {
	   System.out.println("Error! Incorrect Password. \n-----------------------");			  
   System.out.println("Element not found: "+e.getMessage() + "\n-----------------------");
  }

  driver.findElement(By.id("Email")).clear();			// Clearing the Email field
  driver.findElement(By.id("Passwd")).clear();			// Clearing the Password field

//closing current driver window	
		driver.close();
		
	}

}

In the console bar, the output will be seen like this-

2

 

Hope this helps beginners.

Thanks for reading.

Locating Elements on a web page

In my previous article I have written about Browsers Commands.

In a webpage there are different Web Elements present like Buttons, Inputs boxes, Links, Drop-down lists, Check-boxes, Radio buttons, Plain texts and other items.To interact with the above elements we need to locate them first. Selenium provides number of Locators to locate the elements.

The different types of locator are-

  • Locating By Id
  • Locating By Name
  • Locating By Class Name
  • Locating by Tag Name
  • Locating Hyperlinks by LinkText
  • Locating By Xpath
  • Locating By CSS

Locating elements on a webpage can be done using the findElement and findElements method.

findElement() –

  • This method will find the first element within the current page using the given “locating mechanism”.
  • It returns a single WebElement.
  • If “findElement” will not find any element within the page then it will throw NoSuchElementException.
  • Syntax – WebElement findElement(By. “ANY LOCATOR”);

findElements() method –

  • This method is use to find multiple element on webpage.
  • It returns a list of WebElements.
  • If no element is not found, it will return empty List of WebElement and no exception will be thrown.
  • Syntax – java.util.List<WebElement> findElements(By. “ANY LOCATOR”);

Now we have to install 2 plugins[Firebug, Firepath] so as to integrate it with Firefox as it will help us to inspect the element and to generate Xpath.

  • First Add Firebug to the Firefox. Click this link to Add.
  • After integrating the FireBug with Firefox add FirePath from this Link.

Now Open the Firebug by Pressing F12 on your keyboard. Then click on the inspect Element icon in the Firebug. It will show the html view of that particular element. See below for the image.

1

Now Lets discuss the Syntax for each Locator.

  • Locating Element By.Id()-

By.Id  – This helps to find the element using the Id.

Syntax-

driver.findElement(By.id(“id of the element”));

  • Locating Element By.Name() –

By.Name – This helps to locate the element by the value of the “name” attribute.

Syntax-

driver.findElement(By.name(“name of the element”));

  • Locating Element By.tagName() –

By.tagName – This helps to locate the web element based on the HTML tag.

Syntax-

driver.findElement(By.tagName(“tagname”));

  • Locating Element By.linkText() –

By.linkText – This helps to locate the element by text displayed on link.

Syntax-

 driver.findElement(By.linkText(“link text”));

  • Locating ElementBy.partiallinkText: It is same as Link text. Only difference is here it helps to locate the  link element which is having partial matching text.

Syntax-

driver.findElement(By.partiallinkText(“partial text”));

  • By.ClassName :- This helps to locate the web element based on the“class” attribute.

Syntax-

driver.findElement(By.className(“Name of the class”));

  • By.xpath:- This helps to locate elements via xpath.

Syntax-

driver.findElement(By.xpath(“xpath of the element”));

  • By.cssSelector:- This helps to locate element using the CSSSelector.

Syntax-

driver.findElement(By.cssSelector(“selector”));

So, in this post we discussed about the different ways of Locating Elements on a web page. Hope this article will help you 🙂

A small Tip for – How to take Screen Shot using Selenium WebDriver?

In the previous blog we had discussed in short , ” How to automate a browser !! “. As a beginner like me,  you will be very keen to take screen shot of a particular page.

Here is a simple script for taking screen shot of any URL. I have taken “http://www.flipkart.com/&#8221; as mu URL.

package login;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Screen_shot {

public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub

// Initialize WebDriver
WebDriver driver = new FirefoxDriver();
// Wait For Page To Load

driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
// Go to URL
driver.get("http://www.flipkart.com/");
// Maximize Window
driver.manage().window().maximize();
// Take ScreenShot
File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("D:\\selenium\\screenshot1.png"), true);
// Close Driver
driver.quit();
}
}

Below are few points with images.

  • 13456
  • For the above image you have to Import this package only [  ‘FileUtlis’ (org.apache.commons.io) ]
  • After completion of the script, the PNG file will be stored in your given location. I have given [ “D:\\selenium\\screenshot1.png” ]. Here is the screen shot below.

File has been saved in the format i.e png as mentioned in the script.

  • File has been saved in the format i.e “png” as mentioned in the script. We can save the image in different file type like PNG, JPEG, GIF, BMP.

Hope this helps Beginners like me… 🙂

 

 

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… 🙂