15K Views

page refr

As we know that in creating of selenium test cases can result in an un-maintainable project. one of reason is that too many duplicated codes used. Duplicate code could be caused by duplicated functionality and this will result in duplicate use of locators. The disadvantage of duplicated code is that the project is less maintainable. If some locator will get change we have to walk through whole code to adjust locator where necessary. By using page object pattern using pagefactory we can reduce duplicate test code, and also it improve readability of test code. Implementaion of page object model created by writing seperate  test object and test script.

Advantage of  using Page Object Pattern
  • Easy to maintain Test scripts
  • It improve readability of scripts
  • Re-usability of code
  • Reduce or Eliminate duplicacy
How to implement Page Object Model

There are two different ways of implementing pom:

1) Regular java classes: Please visit Page Object Model

2) Page Factory class:  Please follow  the article

Selenium PageFactory

In PageFactory we use Annotations and it is like

@FindBy(id = “Username”)

public WebElement EnterUsername;

As i told above that PageFactory contains all the elements of webpage at start when we initalize any page class objects. In PageFactory we call a method on the webelement, the driver will go and find it on the current once again.

Divide the single PageFactory Objects in to different Page Classes

1. Create a ‘New Package‘ file and name it as ‘pageObjects’, by right click on the Project and select New > Package.

2. Create  ‘New class‘ file for Home Page & LogIn Page and  select New > Class. In our case it is Home Page and LogIn Page.

Home Page Class

As in below code i explain  in Shopify Store how customer will create ticket regarding his query related to any products.

package page_objects;
 
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
 
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.FindBy;
 
import org.openqa.selenium.support.How;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
 
public class LogIn_PG_POF {
   
 static WebDriver driver;
	WebDriverWait wait =  new WebDriverWait(driver, 80);
	
	//Click on Tickets
    @FindBy(xpath = "//a[@href='/pages/tickets']")
    public WebElement btntickets ;
	
	//Click On create-Ticket in customer panel
    @FindBy(xpath = "//a[@href='/pages/tickets/create-ticket']")
    public WebElement create_ticket ;
	
	//Select Type of Tickets
    @FindBy(name = "type")
    public WebElement select_type ;
	
	//Click on Type
    @FindBy(css = ".drop-down-wrapper.create-type-place ul li:nth-child(1)")
    public WebElement click_type ;
	
	//Enter Subject
    @FindBy(css = "input[type='text']")
    public WebElement subject ;
	
	//Enter Message
    @FindBy(className = "block")
    public WebElement message ;
	
	//Add Attachment 
    @FindBy(css = "#myDiv #fileSelector")
    public WebElement Attachment_Add ;
	
	//Click on Create Ticket
    @FindBy(className = "create-ticket-form-submit")
    public WebElement Create_ticket ;
	 	 
		 
// This is a constructor, as every page need a base driver to find web elements
 
public static WebDriver LogIn_PG_POF(){ 
 
	//System.setProperty("webdriver.gecko.driver", "/home/users/shivani.kukreti/Downloads/Documents/geckodriver");
	System.setProperty("webdriver.chrome.driver", "/home/users/shivani.kukreti/Downloads/chromedriver");
	driver = new ChromeDriver();
	return driver;
    } 
 

LogIn Page Class

package page_objects;
 
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
 
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.FindBy;
 
import org.openqa.selenium.support.How;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
 
public class LogIn_PG_POF {
   
 static WebDriver driver;
	WebDriverWait wait =  new WebDriverWait(driver, 80);
	   
   ///Click on Customer login 
	@FindBy(xpath = "//a[@href='/account/login']")
   public WebElement clickcustomerlogo;
   
  //Enter Customer Email
	@FindBy(id = "CustomerEmail")
	public WebElement txtbx_UserName;
 
    //Enter Customer Email
    @FindBy( id = "CustomerPassword")  
    public WebElement txtbx_Password;
	
	//Click on Customer Login
    @FindBy(css = "input[type='submit']")
    public WebElement btn_Login ;
	 
		  
// This is a constructor, as every page need a base driver to find web elements
 
public static WebDriver LogIn_PG_POF(){ 
 
	//System.setProperty("webdriver.gecko.driver", "/home/users/shivani.kukreti/Downloads/Documents/geckodriver");
	System.setProperty("webdriver.chrome.driver", "/home/users/shivani.kukreti/Downloads/chromedriver");
	driver = new ChromeDriver();
	return driver;
    } 
 
}

Test Case

package automationFramework;
 
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
 
		import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import page_objects.*;
 
		
 
	public class Customer_create_ticket {

	
		//static WebDriver driver;
		
		public static void main(String[] args) throws InterruptedException{
        

			
			//System.setProperty("webdriver.chrome.driver", "/home/users/shivani.kukreti/Downloads/chromedriver");
			// System.setProperty("webdriver.gecko.driver", "/home/users/shivani.kukreti/Downloads/Documents/geckodriver");


			WebDriver driver =	  LogIn_PG_POF.LogIn_PG_POF();
			WebDriverWait wait =  new WebDriverWait(driver, 80);
             
			driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
 
			driver.get("https://qa-webkul.myshopify.com/account/login");
 
			// This is to Instantiate Home Page and LogIn Page class  
 
			
			LogIn_PG_POF LoginPage = PageFactory.initElements(driver, LogIn_PG_POF.class);
 
			// Once both classes Initialised, use their Web Element Objects
           
			LoginPage.ClickEnterpassword.click();
			
			LoginPage.Enterpassword.sendKeys("rtuyez");
			
			driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
			
			//WebElement Enter = wait.until(ExpectedConditions.visibilityOfElementLocated(By.name("commit")));
			
			Thread.sleep(4000);
			
			LoginPage.Enter.click();
			
			//Customer click on customer profile logo
                        LoginPage.clickcustomerlogo.click();
			//Enter Customer Email
                        LoginPage.txtbx_UserName.sendKeys("shivanikukreti.symfony129@webkul.com");
                        //Enter password
 			LoginPage.txtbx_Password.sendKeys("webkul123");
                        //Enter on login
			LoginPage.btn_Login.click();
			//Enter on Tickets
			LoginPage.btntickets.click();
			//click on Create-ticket
			LoginPage.create_ticket.click();
			//wait driver for 10 sec
			driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
			//click on Ticket type
			LoginPage.click_type.click();
			//Select Ticket type
			LoginPage.select_type.click();
			//Enter subject
			LoginPage.subject.sendKeys("Shopify");
			//Enter message
			 LoginPage.message.sendKeys("This is Message");
			//Add Attachment
			 LoginPage.Attachment_Add.sendKeys("/home/users/shivani.kukreti/Documents/blog-db.png");
			//click on create ticket
			  LoginPage.Create_ticket.click();
			
		 
			Thread.sleep(2000);
			
		   System.out.println(" Login Successfully, now it is the time to Log Off buddy.");
 
		
 
			driver.quit();
 
		}
 
	}
. . .

Comment

Add Your Comment

Be the first to comment.

css.php