Page Generator Class
package com.qa.amazon.pages;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.PageFactory;
import com.qa.amazon.base.BasePage;
public class PageGenerator {
public WebDriver driver;
//Constructor
public PageGenerator(WebDriver driver){
this.driver = driver;
}
//JAVA Generics to Create and return a New Page
public <TPage extends BasePage> TPage GetInstance (Class<TPage> pageClass) throws Exception {
try {
//Initialize the Page with its elements and return it.
return PageFactory.initElements(driver, pageClass);
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
}
Sample Test
package com.qa.amazon.pages;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.PageFactory;
import com.qa.amazon.base.BasePage;
public class PageGenerator {
public WebDriver driver;
//Constructor
public PageGenerator(WebDriver driver){
this.driver = driver;
}
//JAVA Generics to Create and return a New Page
public <TPage extends BasePage> TPage GetInstance (Class<TPage> pageClass) throws Exception {
try {
//Initialize the Page with its elements and return it.
return PageFactory.initElements(driver, pageClass);
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
}
Page Factory is an inbuilt Page Object Model concept for Selenium WebDriver but it is very optimized.
Here as well, we follow the concept of separation of Page Object Repository and Test Methods. Additionally, with the help of PageFactory class, we use annotations @FindBy to find WebElement. We use initElements method to initialize web elements
Here as well, we follow the concept of separation of Page Object Repository and Test Methods. Additionally, with the help of PageFactory class, we use annotations @FindBy to find WebElement. We use initElements method to initialize web elements
Base Page Class Will Extend PageGenerator Class
package com.qa.amazon.base;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import com.qa.amazon.pages.PageGenerator;
public class BasePage extends PageGenerator{
public BasePage(WebDriver driver) {
super(driver);
}
/**
* Utility function to switch to recent Open Window
*/
public void switchToWindow(){
for(String winHandle : driver.getWindowHandles()){
driver.switchTo().window(winHandle);
}
}
/**
* Utility Function to switch between Windows
* @param windowName
*/
public void switchToWindow(String windowName){
driver.switchTo().window(windowName);
}
/**
* Explicit Wait Utility function to wait for particular element
* @param element
*/
public void wait(WebElement element){
WebDriverWait wait=new WebDriverWait(driver, 45);
wait.until(ExpectedConditions.elementToBeClickable(element));
}
}
Test Base Class (See Highlighted Parts)
package com.qa.amazon.base;
import io.qameta.allure.Attachment;
import io.qameta.allure.Step;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Properties;
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.chrome.ChromeDriver;
import org.testng.ITestResult;
import org.testng.Reporter;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import com.qa.amazon.pages.PageGenerator;
import com.qa.amazon.util.TestUtil;
public class TestBase {
public WebDriver driver;
public Properties prop;
public PageGenerator page; // creating object in BaseTest , so that in test cases we can directly call this object and by which we can directly call GetInstance function
public TestBase(){
try {
prop = new Properties();
FileInputStream fis = new FileInputStream(".//src//main//java//com//qa//amazon//config//config.properties");
prop.load(fis);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
catch(IOException io){
io.printStackTrace();
}
}
@BeforeMethod
public void setup(Method result) {
initial();
}
@Step("Opening Browser")
public void initial(){
String browserName = prop.getProperty("browser");
if(browserName.equals("chrome")){
System.setProperty("webdriver.chrome.driver", ".//src//test//java//resources//chromedriver.exe");
driver = new ChromeDriver();
}
driver.manage().window().maximize();
driver.manage().deleteAllCookies();
driver.manage().timeouts().pageLoadTimeout(TestUtil.PAGE_LOAD_TIMEOUT,TimeUnit.SECONDS);
driver.manage().timeouts().implicitlyWait(TestUtil.IMPLICIT_WAIT,TimeUnit.SECONDS);
driver.get(prop.getProperty("url"));
page=new PageGenerator(driver); // create new Object while initializing the New Object
}
@Step("Taking Screenshot for Failure case")
@Attachment
public byte[]screenShot()
{
return ((TakesScreenshot)driver).getScreenshotAs(OutputType.BYTES);
}
@Step("Closing Browser")
@AfterMethod
public void teardown(ITestResult result) {
{
if(result.getStatus()==ITestResult.FAILURE)
{
System.out.println("Name-->"+result.getName());
screenShot();
}
driver.quit();
}
}
/**
* Getting Title of the page
* @return title of the page as String
*/
public String getTitle(){
return driver.getTitle();
}
}
Sample Test
@Test
@Description("Select Departments section > Electronics > Headphones > Select first available headphone and Add to cart")
public void addProductInCart(Method method) throws Exception{
page.GetInstance(LoginPage.class).login();
Assert.assertEquals(getTitle().trim(), "Amazon Sign-In","Looks Like Sign in Page is not loaded");
page.GetInstance(SignInPage.class).signInToAmazon(prop.getProperty("username"), prop.getProperty("password"));
No comments:
Post a Comment