In this post, we will see Selenium Page Object Model Framework Java | Selenium Page Object Model and Page Factory Part 1
Program Code (RediffHomePage.java) :
package PageObjectRepository;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
public class RediffHomePage {
WebDriver driver;
By Logo=By.xpath("//span[@class='hmsprite logo']");
By SignIn=By.xpath("//a[@title='Already a user? Sign in']");
public RediffHomePage(WebDriver driver)
{
this.driver=driver;
}
public WebElement logo()
{
return driver.findElement(Logo);
}
public WebElement signIn()
{
return driver.findElement(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=dBvxa03Dojc
No comments:
Post a Comment