Now a days, Page Object
Model become
very popular test automation framework in Selenium, where web application pages are
represented as classes, and the all the web elements on the page are defined
as variables on the class. All possible user interactions can be implemented as
methods on the class.
Advantages:
1) User-friendly interface
for creation and execution of test suites.
2) Reusability of
code.
3)Reduce duplicate code
4) Easy maintenance.
Page Object Model Structure:
Implementation of Page Object Model Without PageFactory :
The structure of
Page object model where all page Web Elements of the application and
the user actions on these Web Elements are maintained as methods inside a class
file.
Example:
Let us take HMS portal is
an example for implementing POM framework. It contains Login page and Home page
once after logged in successfully.
Please find the below link to Download the Sample Page Object Model framework without page factory.
Page objects in login page:
public class LoginPage {
//object repository
public By usernameobj=By.name("username");
public By passwordobj=By.name("password");
public By loginObj=By.name("submit");
public By resetObj=By.name("reset");
//variables
public String username;
public String password;
//Page object Methods
//set the user name and password
public void generateData(String username,String password){
this.username=username;
this.password=password;
}
//Filling user name and password
public void fillingDataToLogin(WebDriver driver){
driver.findElement(usernameobj).clear();
driver.findElement(usernameobj).sendKeys(username);
driver.findElement(passwordobj).clear();
driver.findElement(passwordobj).sendKeys(password);
}
//Click on login button
public void clickOnLoginButton(WebDriver driver){
driver.findElement(loginObj).click();
}
//click on reset button
public void clickOnResetButton(WebDriver driver){
driver.findElement(loginObj).click();
}
}
Page objects in Home page:
public class HomePage {
//object repository
public By homePageUserNameObj =By.xpath("//h3[contains(.,'Welcome, admin Logout')]");
public By logoutObj =By.xpath("//a[@href='logout.php']");
//Getting logged in user name from Home page
public String getHomePageDashboardUserName(WebDriver driver){
return driver.findElement(homePageUserNameObj).getText();
}
}
Test Scripts:
Here I have covered the valid and invalid test cases of HMS portal.
public class TestCases extends BaseClass {
//object creation for LoginPage
LoginPage loginpage = new LoginPage();
HomePage homepage = new HomePage();
/**
* HMS-001 -In Valid Login
* @throws IOException
*/
@Test(priority=1)
public void inValidLoginOfHMS() throws IOException{
try
{
loginpage.generateData("inavlidusername", "inavlidusername");
loginpage.fillingDataToLogin(driver);
//CommonMethod.takesScreenshot(driver, "BeforeLogin", "Success");
loginpage.clickOnLoginButton(driver);
//Verification
if(CommonMethod.isAlertPresent(driver)){
Alert alert = driver.switchTo().alert();
System.out.println(alert.getText());
alert.accept();
}
}
catch(Throwable t)
{
//CommonMethod.takesScreenshot(driver, "In valid login","Failed");
System.out.println(t.getLocalizedMessage());
Error e1 = new Error(t.getMessage());
e1.setStackTrace(t.getStackTrace());
throw e1;
}
}
/**
* HMS-002 -Valid Login
* @throws IOException
*/
@Test(priority=2)
public void ValidLoginOfHMS() throws IOException{
try
{
loginpage.generateData("admin", "admin");
loginpage.fillingDataToLogin(driver);
//CommonMethod.takesScreenshot(driver, "Before Login","Success");
loginpage.clickOnLoginButton(driver);
//Verification
Assert.assertTrue(homepage.getHomePageDashboardUserName(driver).contains("admin"));
Assert.assertTrue(driver.findElement(homepage.logoutObj).isDisplayed());
//CommonMethod.takesScreenshot(driver, "After login","Success");
}
catch(Throwable t)
{
CommonMethod.takesScreenshot(driver, "valid login","Failed");
System.out.println(t.getLocalizedMessage());
Error e1 = new Error(t.getMessage());
e1.setStackTrace(t.getStackTrace());
throw e1;
}
}
}
In order to execute this test cases we need a BaseClass which contains Browser and Environment details configuration.
BaseClass:
public class BaseClass {
public static WebDriver driver;
public Properties prop;
@BeforeClass
@Parameters({"browserName","environment"})
public void setup(String browserName,String environment ) throws IOException, InterruptedException{
//selecting browser based on parameter from TestNG.xml
if(browserName.equalsIgnoreCase("firefox")){
driver = new FirefoxDriver();
}
else if(browserName.equalsIgnoreCase("chrome")){
System.setProperty("webdriver.chrome.driver", "F:/Workspace/POMFramework/Driver Files/chromedriver.exe");
driver= new ChromeDriver();
}
else if(browserName.equalsIgnoreCase("ie")){
System.setProperty("webdriver.ie.driver", "F:/Workspace/POMFramework/Driver Files/IEDriverServer.exe");
driver= new InternetExplorerDriver();
}
else if(browserName.equalsIgnoreCase("opera")){
System.setProperty("webdriver.opera.driver", "F:/Workspace/POMFramework/Driver Files/operadriver.exe");
driver= new OperaDriver();
}
//maximizing window
driver.manage().window().maximize();
//creating object for properties class
prop = new Properties();
//creating object for file input stream
FileInputStream file1 = new FileInputStream("F:/Workspace/POMFramework/src/main/resources/Environment.properties");
//loading data from file
prop.load(file1);
//Navigating to URL based on environment parameter from TestNG.xml
//ENV_QA,ENV_SATGING,ENV_DEV,ENV_PRODUCTION Retrieving from Environment.properties
if(environment.equalsIgnoreCase("qa")){
driver.get(prop.getProperty("ENV_QA"));
}
else if(environment.equalsIgnoreCase("staging")){
driver.get(prop.getProperty("ENV_SATGING"));
}
else if(environment.equalsIgnoreCase("dev")){
driver.get(prop.getProperty("ENV_DEV"));
}
else if(environment.equalsIgnoreCase("prod")){
driver.get(prop.getProperty("ENV_PRODUCTION"));
}
}
@AfterClass
public void end() throws InterruptedException{
Thread.sleep(5000);
driver.close();
}
}
Here we are maintaining Driver related reusable methods in CommonMethod class.(Alerts and Take Screenshot).
CommonMethod Class:
public class CommonMethod {
public static boolean isAlertPresent(WebDriver driver){
try{
driver.switchTo().alert();
return true;
}
catch(Exception e){
return false;
}
}
public static void takesScreenshot(WebDriver driver , String ScreenshotName,String message) throws IOException{
Date date = new Date();
Timestamp timestamp= new Timestamp(date.getTime());
String time=timestamp.toString();
time = time.replace(' ', '-');
time = time.replace(':', '-');
File screen=((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(screen, new File(System.getProperty("user.dir")+"\\Screenshots\\"+"_"+ScreenshotName+"_"+time+"_"+message+"_"+".jpg"));
}
}
Environment properties file
Here we are maintaining all the URL's of an application in Environment.properties file.
Environment.properties:
##Environment Details
ENV_DEV=http://http://www.seleniumfortesing.com/hms/
ENV_QA=http://http://www.seleniumfortesing.com/hms/
ENV_SATGING=http://www.seleniumfortesing.com/hms/
ENV_PRODUCTION=http://www.seleniumfortesing.com/hms/
And finally for executing test scripts we have use TestNG.xml file. Please find the below xml structure for the same
.
TestNG.xml:
<suite name="SmokeTestCasesSuite" verbose="1" preserve-order="true" >
<test name="Smoketest_Chrome1" preserve-order="true">
<!-- pass data on below parameters before executing test suite -->
<parameter name="browserName" value="chrome"></parameter>
<parameter name="environment" value="prod"></parameter>
<classes>
<class name="com.way2selenium.testcases.TestCases"></class>
</classes>
</test>
</suite>
Please find the below link to download the sample Page Object Model framework without page factory.
Find the below link to download the sample Page Object Model framework without page factory.
ReplyDeletehttps://drive.google.com/file/d/0B0pJK3xnwFskb29ZNUhnekpHMEU/view