Automation Using Selenium Webdriver

Tuesday, 4 October 2016

Different ways to get all "404 page not found" links using Selenium WebDriver

In this post, I will explain the most convenient way using Selenium WebDriver to get all the links from web page and verifying each page contains specific like 404 or Page not found in different ways.

//Following ways to identify 404 links
By using page title
By using page source
By using response code of page URL     
  
Please find the below code for the same.
Sample Code:
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class GetAllLinks {
 public WebDriver driver;
 ArrayList<String> al ;
 @BeforeClass
 public void setup(){
  driver = new FirefoxDriver();
  driver.get("http://www.facebook.com/");
  driver.manage().window().maximize();
  
 }
 @Test
 public void identifyBrokenAnd404Links() throws MalformedURLException, IOException{
  
      al = new ArrayList<String>();
  
    //identifying total number of URls in a page
    List<WebElement> links = driver.findElements(By.tagName("a")); 
    System.out.println(links.size());
   
           //for Getting all links from page
          for (int i = 0;i<links.size(); i++) {
      
        //get one by one URL href value
        String URL=links.get(i).getAttribute("href"); 
       
        //Removing unwanted URLS based on http or https
        if(links.get(i).getAttribute("href").contains("https")||links.get(i).getAttribute("href").contains("http"))
        {
        System.out.println(URL);
       
         //storing all in URL's in array list
         al.add(URL);
       
        }
          }
     
          //Identifying  broken and 404 links
         
          for(int i=0;i<al.size();i++){
           
           //Navigating each URL
           driver.get(al.get(i));
           
           //getting response Code for the link
           int statusCode= ResponseCode(al.get(i));

           //verifying 404 links using page title
           
           if(driver.getTitle().contains("404")){
            
            System.out.println("404 link is  "+al.get(i));
            
           }
           
           //verifying 404 links using page source
           else if(driver.getPageSource().contains("404 page not found")){
            
            System.out.println("404 link is  "+al.get(i));
            
           }
           //verifying 404 links using status code
           else if(statusCode==404){
            
            System.out.println("404 link is  "+al.get(i));
           } 
           
           
          } 
  }
    //method for generating response code for URL
     public static int ResponseCode(String URL) throws MalformedURLException, IOException {     
        URL url = new URL(URL); 
        HttpURLConnection huc = (HttpURLConnection) url.openConnection(); 
        huc.setRequestMethod("GET"); 
        huc.connect(); 
        return huc.getResponseCode(); 
     } 

}
              

Monday, 3 October 2016

Data Driven Framework in Selenium Webdriver


             Data driven framework in selenium web driver 

this framework purely depends on data and data source can be anything like Excel file, CSV File, database.

In data driven framework script will be separated from Data part, it means so if any changes happen we do not have to modify all the test cases.

Example-
I have to create 50 Gmail accounts so I have two approaches to do this

First- I can create 50 scripts and run them.

Second- I can keep all data separate in the file and changes the data only that is required for script and script will be only one. In future, any changes in my application then I have to modify my one script only not fifty.

In simple words when we have to execute same script, multiple sets of data we will adopt data driven framework

In this post, we are taking data from 2D Array and feeding data into script

Scenario 1- Open Facebook and type username and password and login
this test case should run 2 times with different set of data(data we have provided in the 2D array)

Lest Implement the same

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class TestDDT {

// this will take data from dataprovider which we created
@Test(dataProvider="testdata")
public void TestFireFox(String uname,String password){

// Open browsre
WebDriver driver=new FirefoxDriver();


// Maximize browser
driver.manage().window().maximize();


// Load application
driver.get("http://www.facebook.com");


// clear email field

driver.findElement(By.id("email")).clear();


// Enter usename
driver.findElement(By.id("email")).sendKeys(uname);

// Clear password field
driver.findElement(By.id("pass")).clear();


// Enter password
driver.findElement(By.id("pass")).sendKeys(password);
}
// this is DataProvider which actually feed data to our test cases here I have taken 2 D //array with 2 rows and 2 column it means. It will run our test case two times because we //have taken 2 rows. While first iteration this will pass username and password to test //case and in second iteration perform the same for second rows

@DataProvider(name="testdata")
public Object[][] TestDataFeed(){

// Create object array with 2 rows and 2 column- first parameter is row and second is //column
Object [][] facebookdata=new Object[2][2];

// Enter data to row 0 column 0
facebookdata[0][0]="Selenium1@gmail.com";


// Enter data to row 0 column 1
facebookdata[0][1]="Password1";


// Enter data to row 1 column 0
facebookdata[1][0]="Selenium2@gmail.com";

// Enter data to row 1 column 0
facebookdata[1][1]="Password2";

// return arrayobject to testscript
return facebookdata;
}

}</span>

Data driven framework in selenium webdriver using Excel files
In above post, we have seen data driven using 2D array but once your test data will increase then you have to switch to Excel or CSV or Database.

Create some test data in Excel that we will pass to the script. For demo purpose, I have to take username and password in Excel.
Before implement the program please make sure you are familiar with Reading Data from Excel.
Copy below program and before executing make sure following Thing configured correctly.

Eclipse, TestNG, JExcel jar
Scenario -Open Facebook and login with different username and password
username and password will be coming from excel sheet

Note- in below program we have 2 rows only so test case will execute 2 times with different data
package DataDrivenTesting;

import java.io.IOException;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class TestDDT1 {

WebDriver driver;
Workbook wb;
Sheet sh1;
int numrow;
String username;
String password;

@BeforeTest
public void Setup()

{
driver=new FirefoxDriver();
driver.manage().window().maximize();
driver.get("http://www.facebook.com");
}

@Test(dataProvider="testdata")
public void TestFireFox(String uname,String password1)

{

driver.findElement(By.id("email")).clear();
driver.findElement(By.id("email")).sendKeys(uname);
driver.findElement(By.id("pass")).clear();
driver.findElement(By.id("pass")).sendKeys(password1);
}

@DataProvider(name="testdata")
public Object[][] TestDataFeed(){

try {

// load workbook
wb=Workbook.getWorkbook(new File("location of excel sheet/fbdata.xls"));

// load sheet in my case I am referring to first sheet only
sh1= wb.getSheet(0);

// get number of rows so that we can run loop based on this
numrow=  sh1.getRows();
}
catch (Exception e)

{
e.printStackTrace();
}

// Create 2 D array and pass row and columns
Object [][] facebookdata=new Object[numrow][sh1.getColumns()];

// This will run a loop and each iteration  it will fetch new row
for(int i=0;i<numrow;i++){

// Fetch first row username
facebookdata[i][0]=sh1.getCell(0,i).getContents();
// Fetch first row password
facebookdata[i][1]=sh1.getCell(1,i).getContents();

}

// Return 2d array object so that test script can use the same
return facebookdata;
}

@AfterTest
public void QuitTC(){

// close browser after execution
driver.quit();
}

}</span>

I hope you have enjoyed the data driven framework in selenium webdriver and Hope you will implement the same. If you still have any doubt then let me know in the comment section. You can implement the same data driven framework using POM (Page Object model ) which will be the advanced version of data driven framework in selenium webdriver.



Kindly share this with your friends as well



Sunday, 2 October 2016

POM Framework Using With Page Factory and With out Page Factory


Page Object Model using Selenium Webdriver and Implementation


1- It is design pattern in which will help you to maintain the codeand code duplication,
  which is a crucial thing in Test automation.

2- You can store all locators and respective methods inthe separateclass and Call them from the test in which you have to use.So the benefit from this will be if any changes in Page the you do not have to modify the test simply modify the respective page and that all.

3- You can create a layer between your test script and application page, which you have to automate.

4- In other words, it will behave as Object repository where all locators are saved.

“Please don’t get confused if you are using Object repository concept then do not use Page Object model because both will serve the same purpose.
Implementation of Page Object model using Selenium WebdriverIf you want to implement
 Page Object model then you have two choices and you can use any of it.

1 – Page Object model without PageFactory

2- Page Object Model with Pagefactory.

Personally, I am a big fan of Page Factory, which comes with Cache Lookup feature, which allows me to store frequently used locators in catch so that performance will be faster. We will discuss both you can implement based on your preferences.

Page Object Model With out page Factory


  Let’s take very basic scenario which you can relate to any application. Consider you have login page where username, password, and login button is present.

I will create a separate Login Page, will store three locators, and will create methods to access them. Kindly refer below screenshot for reference.
Now I want to design test case so I can use the Login class, which I created and can call the methods accordingly.
This Login class will be used by all the scripts, which you will create in future so if any changes happened then you have to update only Login Class not all the test cases. This sounds good right.
Program for Page Object Model using Selenium Webdriver without Page factory
package com.Facebook.Pages;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.CacheLookup;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;

/**
* @author jagan


* This class will store all the locator and methods of login page
*
*/
public class LoginPage 
{
WebDriver driver;
By username=By.id("user_login");
By password=By.xpath(".//*[@id='user_pass']");
By loginButton=By.name("wp-submit");
public LoginPage(WebDriver driver)
{
this.driver=driver;
}
public void loginToFacebook(String userid,String pass)
{

driver.findElement(username).sendKeys(userid);
driver.findElement(password).sendKeys(pass);
driver.findElement(loginButton).click();
}
public void typeUserName(String uid)
{
driver.findElement(username).sendKeys(uid);
}
public void typePassword(String pass)
{
driver.findElement(password).sendKeys(pass);
}
 public void clickOnLoginButton()
{
driver.findElement(loginButton).click();
}
}
TestCase using Page Object Model using Selenium Webdriver
package com.Facebook.Testcases;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;

import com.Facebook.Pages.LoginPage;

/**
* @author Jagan
*
*/
public class VerifyFacebookLogin 
{
@Test
public void verifyFacebookLogin()
{
WebDriver driver=new FirefoxDriver();
driver.manage().window().maximize();
driver.get("http://www.facebook.com");
LoginPage login=new LoginPage(driver):
login.clickOnLoginButton();
driver.quit();
}
}
Page Object Model using Selenium Webdriver with Page Factory
Selenium has built in class called PageFactory, which they mainly created for Page Object purpose, which allows you to store elements in cache lookup.

The only difference, which you will get without PageFactory and with PageFactory, is just initElement statement.Let us check the code and will see what changes required for with PageFactory Approach


Code for Page Object Model Using Selenium Webdriver using Page Factory

package com.Facebook.Pages;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.CacheLookup;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;
public class LoginPageNew 
{
WebDriver driver;
public LoginPageNew(WebDriver ldriver)
{
this.driver=ldriver;
}
@FindBy(id="user_login") 
@CacheLookup
WebElement username; 
@FindBy(how=How.ID,using="user_pass")
@CacheLookup
WebElement password;
@FindBy(how=How.XPATH,using=".//*[@id='wp-submit']")
@CacheLookup
WebElement submit_button;
@FindBy(how=How.LINK_TEXT,using="Lost your password?")
@CacheLookup
WebElement forget_password_link;
public void login_Facebook(String uid,String pass)
{
username.sendKeys(uid);
password.sendKeys(pass);
submit_button.click();
} 
}
                Test Case using Page Factory

package com.facebook.Testcases;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.PageFactory;
import org.testng.annotations.Test;
import com.wordpress.Pages.LoginPage;
import com.wordpress.Pages.LoginPageNew;
import Helper.BrowserFactory;
public class VerifyValidLogin 
{
 @Test
public void checkValidUser()
{
// This will launch browser and specific url 
WebDriver driver=BrowserFactory.startBrowser("firefox", "http://www.facebook.com");
// Created Page Object using Page Factory
LoginPageNew login_page=PageFactory.initElements(driver, LoginPageNew.class);
// Call the method
login_page.login_wordpress("jagan", "jagan768");
}
}