Wednesday 27 July 2022

Selenium Page Factory Model in Java with Example | Selenium Page Object Model & Page Factory Part 2

                     In this post, we will see Selenium Page Factory Model in Java with Example | Selenium Page Object Model & Page Factory Part 2 


Program Code (RediffHomePage.java) :

package PageObjectRepository;


import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.support.FindBy;

import org.openqa.selenium.support.PageFactory;


public class RediffHomePage {

WebDriver driver;

//By Logo=By.xpath("//span[@class='hmsprite logo']");

@FindBy(xpath="//span[@class='hmsprite logo']")

WebElement Logo;

//By SignIn=By.xpath("//a[@title='Already a user? Sign in']");

@FindBy(xpath="//a[@title='Already a user? Sign in']")

WebElement SignIn;

public RediffHomePage(WebDriver driver)

{

this.driver=driver;

PageFactory.initElements(driver, this);

}

public WebElement logo()

{

//return driver.findElement(Logo);

return Logo;

}

public WebElement signIn()

{

//return driver.findElement(SignIn);

return SignIn;

}


}


Program Code (RediffSignInPage.java) :

package PageObjectRepository;


import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;


public class RediffSignInPage {

WebDriver driver;

By UserName=By.xpath("//input[@id='login1']");

By Password=By.xpath("//input[@id='password']");

By SignInButton=By.xpath("//input[@title='Sign in']");

public RediffSignInPage(WebDriver driver)

{

this.driver=driver;

}

public WebElement userName()

{

return driver.findElement(UserName);

}

public WebElement password()

{

return driver.findElement(Password);

}

public WebElement signInButton()

{

return driver.findElement(SignInButton);

}


}


Program Code (Program1.java) :

package Testcases;


import java.time.Duration;


import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.chrome.ChromeDriver;

import org.testng.Assert;

import org.testng.annotations.AfterMethod;

import org.testng.annotations.BeforeMethod;

import org.testng.annotations.Test;


import PageObjectRepository.RediffHomePage;

import PageObjectRepository.RediffSignInPage;


public class Program1 {


WebDriver driver;

@BeforeMethod

public void setUp()

{

System.setProperty("webdriver.chrome.driver", "C:\\selenium\\chromedriver_win32\\chromedriver.exe");

driver=new ChromeDriver();

driver.get("https://www.rediff.com/");

driver.manage().window().maximize();

driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));

}

@Test

public void verifyLogo()

{

RediffHomePage rhp=new RediffHomePage(driver);

WebElement logo=rhp.logo();

Boolean flag=logo.isDisplayed();

Assert.assertTrue(flag);

}

@Test

public void signInTestcase()

{

RediffHomePage rhp=new RediffHomePage(driver);

rhp.signIn().click();

RediffSignInPage rsp=new RediffSignInPage(driver);

rsp.userName().sendKeys("abc");

rsp.password().sendKeys("123");

rsp.signInButton().click();

Assert.assertTrue(driver.getCurrentUrl().contains("f4mail.rediff.com"));

}

@AfterMethod

public void tearDown()

{

driver.quit();

}

}


Watch following video:


Watch on YouTube: https://www.youtube.com/watch?v=PE74GGdGbHY

No comments:

Post a Comment