POM:
SlideShareLoginPage.java
SlideShareLogoutPage.java
SlideShareUploadPage1.java
SlideShareUploadPage2.java
SlideShareTestCase.java
CustomListener.java
testng.xml
- Page Object Model is a design pattern to create Object Repository for WebElements.
- Every web page in the web application there should be corresponding page class.
- Page class contains web page elements and reusable methods
Advantage of POM:
- Verification and operations on the pages are separated
- Page Objects can be used for performance, functional and User Interface Testing
- Easy to maintain, debug and optimized
Hierarchy of Page Object Model:
SlideShareLoginPage.java
package net.slideshare.pages;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import static utility.Helper.*;
public class SlideShareLoginPage {
@FindBy(id = "user_login")
public WebElement txtUsername;
@FindBy(name = "user_password")
public WebElement txtPassword;
@FindBy(id = "login_from_loginpage")
public WebElement btnLogin;
public SlideShareLoginPage(WebDriver driver) {
PageFactory.initElements(driver, this);
}
public void slideShareLogin(String user, String pass) {
synchronization(txtUsername);
txtUsername.clear();
txtUsername.sendKeys(user);
synchronization(txtPassword);
txtPassword.clear();
txtPassword.sendKeys(pass);
synchronization(btnLogin);
btnLogin.click();
}
}
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import static utility.Helper.*;
public class SlideShareLoginPage {
@FindBy(id = "user_login")
public WebElement txtUsername;
@FindBy(name = "user_password")
public WebElement txtPassword;
@FindBy(id = "login_from_loginpage")
public WebElement btnLogin;
public SlideShareLoginPage(WebDriver driver) {
PageFactory.initElements(driver, this);
}
public void slideShareLogin(String user, String pass) {
synchronization(txtUsername);
txtUsername.clear();
txtUsername.sendKeys(user);
synchronization(txtPassword);
txtPassword.clear();
txtPassword.sendKeys(pass);
synchronization(btnLogin);
btnLogin.click();
}
}
SlideShareLogoutPage.java
package net.slideshare.pages;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
public class SlideShareLogoutPage {
@FindBy(linkText="Analytics")
public WebElement linkAnalytics;
@FindBy(linkText="My Clipboards")
public WebElement linkMyClipboards;
@FindBy(linkText="Support")
public WebElement linkSupport;
@FindBy(linkText="Account Settings")
public WebElement linkAccountSettings;
@FindBy(linkText = "Logout")
public WebElement linkLogout;
@FindBy(linkText="My Uploads")
public WebElement linkMyUploads;
@FindBy(linkText = "Home")
public WebElement linkHome;
@FindBy(xpath="//a[@class='user-image']")
public WebElement imgUser;
@FindBy(linkText = "Upload")
public WebElement linkUpload;
public SlideShareLogoutPage(WebDriver driver){
PageFactory.initElements(driver, this);
}
}
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
public class SlideShareLogoutPage {
@FindBy(linkText="Analytics")
public WebElement linkAnalytics;
@FindBy(linkText="My Clipboards")
public WebElement linkMyClipboards;
@FindBy(linkText="Support")
public WebElement linkSupport;
@FindBy(linkText="Account Settings")
public WebElement linkAccountSettings;
@FindBy(linkText = "Logout")
public WebElement linkLogout;
@FindBy(linkText="My Uploads")
public WebElement linkMyUploads;
@FindBy(linkText = "Home")
public WebElement linkHome;
@FindBy(xpath="//a[@class='user-image']")
public WebElement imgUser;
@FindBy(linkText = "Upload")
public WebElement linkUpload;
public SlideShareLogoutPage(WebDriver driver){
PageFactory.initElements(driver, this);
}
}
SlideShareMyUploadPage.java
package net.slideshare.pages;
import java.util.List;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
public class SlideShareMyUploadPage {
@FindBy(linkText = "Analytics")
public WebElement linkAnalytics;
@FindBy(linkText = "My Clipboards")
public WebElement linkMyClipboards;
@FindBy(linkText = "Support")
public WebElement linkSupport;
@FindBy(linkText = "Account Settings")
public WebElement linkAccountSettings;
@FindBy(linkText = "Logout")
public WebElement linkLogout;
@FindBy(linkText = "My Uploads")
public WebElement linkMyUploads;
@FindBy(xpath = "//a[@class='user-image']")
public WebElement imgUser;
@FindBy(xpath = "//input[@id='check-']")
List chkUploads;
@FindBy(xpath = "//*[text()='Select all']")
public WebElement linkSelectAll;
@FindBy(id = "private")
public WebElement makePrivate;
@FindBy(id = "public")
public WebElement makePublic;
@FindBy(xpath = "//*[contains(text(),'Delete')]")
public WebElement delete;
public SlideShareMyUploadPage(WebDriver driver) {
PageFactory.initElements(driver, this);
}
}
import java.util.List;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
public class SlideShareMyUploadPage {
@FindBy(linkText = "Analytics")
public WebElement linkAnalytics;
@FindBy(linkText = "My Clipboards")
public WebElement linkMyClipboards;
@FindBy(linkText = "Support")
public WebElement linkSupport;
@FindBy(linkText = "Account Settings")
public WebElement linkAccountSettings;
@FindBy(linkText = "Logout")
public WebElement linkLogout;
@FindBy(linkText = "My Uploads")
public WebElement linkMyUploads;
@FindBy(xpath = "//a[@class='user-image']")
public WebElement imgUser;
@FindBy(xpath = "//input[@id='check-']")
List
@FindBy(xpath = "//*[text()='Select all']")
public WebElement linkSelectAll;
@FindBy(id = "private")
public WebElement makePrivate;
@FindBy(id = "public")
public WebElement makePublic;
@FindBy(xpath = "//*[contains(text(),'Delete')]")
public WebElement delete;
public SlideShareMyUploadPage(WebDriver driver) {
PageFactory.initElements(driver, this);
}
}
SlideShareUploadPage1.java
package net.slideshare.pages;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
public class SlideShareUploadPage1 {
@FindBy(linkText = "Analytics")
public WebElement linkAnalytics;
@FindBy(linkText = "My Clipboards")
public WebElement linkMyClipboards;
@FindBy(linkText = "Support")
public WebElement linkSupport;
@FindBy(linkText = "Account Settings")
public WebElement linkAccountSettings;
@FindBy(linkText = "Logout")
public WebElement linkLogout;
@FindBy(linkText = "My Uploads")
public WebElement linkMyUploads;
@FindBy(xpath = "//a[@class='user-image']")
public WebElement imgUser;
@FindBy(linkText = "Upload")
public WebElement linkUplaod;
@FindBy(tagName = "title")
public WebElement titleUploadPage1;
@FindBy(xpath = "//form[@id='js_upload']//input[@name='file']")
public WebElement fileUpload;
public SlideShareUploadPage1(WebDriver driver) {
PageFactory.initElements(driver, this);
}
}
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
public class SlideShareUploadPage1 {
@FindBy(linkText = "Analytics")
public WebElement linkAnalytics;
@FindBy(linkText = "My Clipboards")
public WebElement linkMyClipboards;
@FindBy(linkText = "Support")
public WebElement linkSupport;
@FindBy(linkText = "Account Settings")
public WebElement linkAccountSettings;
@FindBy(linkText = "Logout")
public WebElement linkLogout;
@FindBy(linkText = "My Uploads")
public WebElement linkMyUploads;
@FindBy(xpath = "//a[@class='user-image']")
public WebElement imgUser;
@FindBy(linkText = "Upload")
public WebElement linkUplaod;
@FindBy(tagName = "title")
public WebElement titleUploadPage1;
@FindBy(xpath = "//form[@id='js_upload']//input[@name='file']")
public WebElement fileUpload;
public SlideShareUploadPage1(WebDriver driver) {
PageFactory.initElements(driver, this);
}
}
SlideShareUploadPage2.java
package net.slideshare.pages;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
public class SlideShareUploadPage2 {
@FindBy(linkText = "Analytics")
public WebElement linkAnalytics;
@FindBy(linkText = "My Clipboards")
public WebElement linkMyClipboards;
@FindBy(linkText = "Support")
public WebElement linkSupport;
@FindBy(linkText = "Account Settings")
public WebElement linkAccountSettings;
@FindBy(linkText = "Logout")
public WebElement linkLogout;
@FindBy(linkText = "My Uploads")
public WebElement linkMyUploads;
@FindBy(xpath = "//a[@class='user-image']")
public WebElement imgUser;
@FindBy(css = "input[id^='slideshow_title']")
public WebElement slideshowTitle;
@FindBy(xpath = "//textarea[starts-with(@id, 'slideshow_description')]")
public WebElement slideshowDescription;
@FindBy(css = "select[id^='slideshow_category'] > option[value='6']")
public WebElement slideshowCategory;
@FindBy(xpath = "//button[contains(text(), 'Publish')][1]")
public WebElement publishButton;
@FindBy(xpath = "//iframe[@id='upload_preview']")
public WebElement imgUploadPreview;
public SlideShareUploadPage2(WebDriver driver) {
PageFactory.initElements(driver, this);
}
}
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
public class SlideShareUploadPage2 {
@FindBy(linkText = "Analytics")
public WebElement linkAnalytics;
@FindBy(linkText = "My Clipboards")
public WebElement linkMyClipboards;
@FindBy(linkText = "Support")
public WebElement linkSupport;
@FindBy(linkText = "Account Settings")
public WebElement linkAccountSettings;
@FindBy(linkText = "Logout")
public WebElement linkLogout;
@FindBy(linkText = "My Uploads")
public WebElement linkMyUploads;
@FindBy(xpath = "//a[@class='user-image']")
public WebElement imgUser;
@FindBy(css = "input[id^='slideshow_title']")
public WebElement slideshowTitle;
@FindBy(xpath = "//textarea[starts-with(@id, 'slideshow_description')]")
public WebElement slideshowDescription;
@FindBy(css = "select[id^='slideshow_category'] > option[value='6']")
public WebElement slideshowCategory;
@FindBy(xpath = "//button[contains(text(), 'Publish')][1]")
public WebElement publishButton;
@FindBy(xpath = "//iframe[@id='upload_preview']")
public WebElement imgUploadPreview;
public SlideShareUploadPage2(WebDriver driver) {
PageFactory.initElements(driver, this);
}
}
SlideShareTestCase.java
package net.slideshare.test;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.sikuli.script.FindFailed;
import org.sikuli.script.Screen;
import org.testng.Assert;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import net.slideshare.pages.SlideShareLoginPage;
import net.slideshare.pages.SlideShareLogoutPage;
import net.slideshare.pages.SlideShareMyUploadPage;
import net.slideshare.pages.SlideShareUploadPage1;
import net.slideshare.pages.SlideShareUploadPage2;
import utility.Helper;
public class SlideShareTestCase {
WebDriver driver;
SlideShareLoginPage login;
SlideShareLogoutPage logout;
SlideShareUploadPage1 upload1;
SlideShareUploadPage2 upload2;
SlideShareMyUploadPage myUploads;
public static WebDriverWait wait;
Properties prop;
String user, pass;
@BeforeTest
public void beforeTest() {
driver = Helper.launchBrowser("chrome");
wait = new WebDriverWait(driver, 10);
wait.pollingEvery(50, TimeUnit.MILLISECONDS);
wait.withTimeout(2, TimeUnit.SECONDS);
wait.ignoring(WebDriverException.class);
try {
prop = new Properties();
prop.load(new FileReader("credentials.properties"));
user = prop.getProperty("suser");
pass = prop.getProperty("spass");
} catch (IOException e) {
e.printStackTrace();
}
}
@Test(priority = 0, groups = { "regression", "smoke" })
public void slideShareLoginTest() {
driver.get("https://www.slideshare.net/login");
login = new SlideShareLoginPage(driver);
if (driver.getTitle().equals("Log in to SlideShare")) {
login.slideShareLogin(user, pass);
}
wait.until(ExpectedConditions.titleIs("Share and Discover Knowledge on LinkedIn SlideShare"));
Assert.assertTrue(driver.getTitle().equals("Share and Discover Knowledge on LinkedIn SlideShare"));
}
@Test(priority = 1, groups = "regression")
public void slideShareUploadTest() throws InterruptedException, FindFailed {
upload1 = new SlideShareUploadPage1(driver);
upload2 = new SlideShareUploadPage2(driver);
Helper.synchronization(upload1.linkUplaod);
upload1.linkUplaod.click();
wait.until(ExpectedConditions.titleContains("Upload"));
Screen sc = new Screen();
sc.click("./Image/Select File.png");
sc.click("./Image/Documents.png");
sc.click("./Image/File.png");
sc.click("./Image/Open.png");
Helper.synchronization(upload2.imgUploadPreview);
upload2.slideshowDescription.sendKeys("Testing File Uploading");
Helper.synchronization(upload2.slideshowDescription);
upload2.slideshowCategory.click();
upload2.publishButton.click();
Helper.synchronization(driver.findElement(By.cssSelector(".slide_image"));
Assert.assertTrue(driver.findElement(By.cssSelector(".slide_image")).isEnabled());
}
@Test(priority = 2, groups = "regression")
public void slideShareDeleteUploadTest() throws InterruptedException {
myUploads = new SlideShareMyUploadPage(driver);
Actions actions = new Actions(driver);
Helper.synchronization(myUploads.imgUser);
actions.moveToElement(myUploads.imgUser).perform();
Helper.synchronization(myUploads.linkMyUploads);
actions.moveToElement(myUploads.linkMyUploads).perform();
actions.click(myUploads.linkMyUploads).perform();
Helper.synchronization(myUploads.linkSelectAll);
actions.click(myUploads.linkSelectAll).perform();
actions.click(myUploads.delete).perform();
driver.switchTo().alert().accept();
Helper.synchronization(driver.findElement(By.xpath("//a[contains(text(),'Upload one now')]")));
Assert.assertTrue(driver.findElement(By.xpath("//a[contains(text(),'Upload one now')]")).isDisplayed());
}
@Test(priority = 3, groups = { "regression", "smoke" })
public void slideShareLogoutTest() throws InterruptedException {
driver.get("http://www.slideshare.net/");
logout = new SlideShareLogoutPage(driver);
Actions actions = new Actions(driver);
Helper.synchronization(logout.imgUser);
actions.moveToElement(logout.imgUser).perform();
Helper.synchronization(logout.linkLogout);
actions.moveToElement(logout.linkLogout).perform();
Helper.synchronization(logout.linkLogout);
logout.linkLogout.click();
Assert.assertTrue(driver.findElement(By.linkText("Login")).getText().equals("Login"));
}
@AfterTest
public void afterTest() {
driver.quit();
}
}
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.sikuli.script.FindFailed;
import org.sikuli.script.Screen;
import org.testng.Assert;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import net.slideshare.pages.SlideShareLoginPage;
import net.slideshare.pages.SlideShareLogoutPage;
import net.slideshare.pages.SlideShareMyUploadPage;
import net.slideshare.pages.SlideShareUploadPage1;
import net.slideshare.pages.SlideShareUploadPage2;
import utility.Helper;
public class SlideShareTestCase {
WebDriver driver;
SlideShareLoginPage login;
SlideShareLogoutPage logout;
SlideShareUploadPage1 upload1;
SlideShareUploadPage2 upload2;
SlideShareMyUploadPage myUploads;
public static WebDriverWait wait;
Properties prop;
String user, pass;
@BeforeTest
public void beforeTest() {
driver = Helper.launchBrowser("chrome");
wait = new WebDriverWait(driver, 10);
wait.pollingEvery(50, TimeUnit.MILLISECONDS);
wait.withTimeout(2, TimeUnit.SECONDS);
wait.ignoring(WebDriverException.class);
try {
prop = new Properties();
prop.load(new FileReader("credentials.properties"));
user = prop.getProperty("suser");
pass = prop.getProperty("spass");
} catch (IOException e) {
e.printStackTrace();
}
}
@Test(priority = 0, groups = { "regression", "smoke" })
public void slideShareLoginTest() {
driver.get("https://www.slideshare.net/login");
login = new SlideShareLoginPage(driver);
if (driver.getTitle().equals("Log in to SlideShare")) {
login.slideShareLogin(user, pass);
}
wait.until(ExpectedConditions.titleIs("Share and Discover Knowledge on LinkedIn SlideShare"));
Assert.assertTrue(driver.getTitle().equals("Share and Discover Knowledge on LinkedIn SlideShare"));
}
@Test(priority = 1, groups = "regression")
public void slideShareUploadTest() throws InterruptedException, FindFailed {
upload1 = new SlideShareUploadPage1(driver);
upload2 = new SlideShareUploadPage2(driver);
Helper.synchronization(upload1.linkUplaod);
upload1.linkUplaod.click();
wait.until(ExpectedConditions.titleContains("Upload"));
Screen sc = new Screen();
sc.click("./Image/Select File.png");
sc.click("./Image/Documents.png");
sc.click("./Image/File.png");
sc.click("./Image/Open.png");
Helper.synchronization(upload2.imgUploadPreview);
upload2.slideshowDescription.sendKeys("Testing File Uploading");
Helper.synchronization(upload2.slideshowDescription);
upload2.slideshowCategory.click();
upload2.publishButton.click();
Helper.synchronization(driver.findElement(By.cssSelector(".slide_image"));
Assert.assertTrue(driver.findElement(By.cssSelector(".slide_image")).isEnabled());
}
@Test(priority = 2, groups = "regression")
public void slideShareDeleteUploadTest() throws InterruptedException {
myUploads = new SlideShareMyUploadPage(driver);
Actions actions = new Actions(driver);
Helper.synchronization(myUploads.imgUser);
actions.moveToElement(myUploads.imgUser).perform();
Helper.synchronization(myUploads.linkMyUploads);
actions.moveToElement(myUploads.linkMyUploads).perform();
actions.click(myUploads.linkMyUploads).perform();
Helper.synchronization(myUploads.linkSelectAll);
actions.click(myUploads.linkSelectAll).perform();
actions.click(myUploads.delete).perform();
driver.switchTo().alert().accept();
Helper.synchronization(driver.findElement(By.xpath("//a[contains(text(),'Upload one now')]")));
Assert.assertTrue(driver.findElement(By.xpath("//a[contains(text(),'Upload one now')]")).isDisplayed());
}
@Test(priority = 3, groups = { "regression", "smoke" })
public void slideShareLogoutTest() throws InterruptedException {
driver.get("http://www.slideshare.net/");
logout = new SlideShareLogoutPage(driver);
Actions actions = new Actions(driver);
Helper.synchronization(logout.imgUser);
actions.moveToElement(logout.imgUser).perform();
Helper.synchronization(logout.linkLogout);
actions.moveToElement(logout.linkLogout).perform();
Helper.synchronization(logout.linkLogout);
logout.linkLogout.click();
Assert.assertTrue(driver.findElement(By.linkText("Login")).getText().equals("Login"));
}
@AfterTest
public void afterTest() {
driver.quit();
}
}
CustomListener.java
package com.test.listener;
import org.testng.ITestResult;
import org.testng.Reporter;
import org.testng.TestListenerAdapter;
public class CustomListener extends TestListenerAdapter {
@Override
public void onTestSuccess(ITestResult tr) {
Reporter.log("Test " + tr.getName() + " is Passed");
}
@Override
public void onTestFailure(ITestResult tr) {
Reporter.log("Test " + tr.getName() + " is Failed");
}
}
import org.testng.ITestResult;
import org.testng.Reporter;
import org.testng.TestListenerAdapter;
public class CustomListener extends TestListenerAdapter {
@Override
public void onTestSuccess(ITestResult tr) {
Reporter.log("Test " + tr.getName() + " is Passed");
}
@Override
public void onTestFailure(ITestResult tr) {
Reporter.log("Test " + tr.getName() + " is Failed");
}
}
testng.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Slide Share Suite">
<listeners>
<listener class-name="com.test.listener.CustomListener"></listener>
</listeners>
<test name="Slide Share Test">
<classes>
<class name="net.slideshare.test.SlideShareTestCase"/>
</classes>
</test>
</suite>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Slide Share Suite">
<listeners>
<listener class-name="com.test.listener.CustomListener"></listener>
</listeners>
<test name="Slide Share Test">
<classes>
<class name="net.slideshare.test.SlideShareTestCase"/>
</classes>
</test>
</suite>
I really liked your blog post.Much thanks again. Awesome.
ReplyDeleteselenium online trainings
selenium trainings