===================================================================
Below is the sample code for Page object model with page factory pattren.
===================================================================
There are 3 classes as mentioned below:
1) Base Class: which contains all constants like page url, username, passord, filename etc.
2) Page class: which acts like object/webelement repositories like xpath/id/name or webelemnt objects instantiations and functions which have logic to do the operations on webelemnts.
3) test case class: this will actually perform the operations on webelements. Like enter data to textboxes, cliscking on a button, select dropdown values.
===================================================================
Base class: below base class have a url, username and password are assigned.
===================================================================
package POM;
public class Constant {
//URL that need to be test
final static String url ="url of a web page";
// login credentials
final static String username="test username";
final static String password="test password";
}
===================================================================
Page Class: below class will store webelement have a login function for those webelemnts.
===================================================================
package POM_Page;
import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
public class POM_Page_Class {
private WebDriver driver;
private WebDriverWait wait = null;
String dateon = getcurrentdate();
@FindBy(id = "uname")
WebElement uname;
@FindBy(id = "upwd")
WebElement paswd;
@FindBy(id = "log")
WebElement login;
public POSmileSource(WebDriver driver) {
this.driver = driver;
PageFactory.initElements(driver, this);
}
public void login() {
uname.sendKeys(Constant.username);
paswd.sendKeys(Constant.password);
login.click();
}
}
===================================================================
Test case class: this will open a browser session. Navigate to url mentioned in a base class. Set browser properties and perform operations on webelements defined in page class.
It has testNG test annotations. before class will run at first and afterclass runs at the last. Inbetween the test annotations are executed.
===================================================================
package POM_testCase_package;
import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.testng.Assert;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class POM_Testcase {
private WebDriver driver;
@BeforeTest(alwaysRun = true)
public void before_test() throws IOException {
System.setProperty("webdriver.chrome.driver", "path to chromedriver");
HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
chromePrefs.put("profile.default_content_settings.popups", 0);
chromePrefs.put("download.default_directory", directorypath);
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", chromePrefs);
options.addArguments("disable-popup-blocking");
// DesiredCapabilities cap = DesiredCapabilities.chrome();
// cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
// cap.setCapability(ChromeOptions.CAPABILITY, options);
// driver = new ChromeDriver(cap);
driver = new ChromeDriver(options);
driver.get(Constant.url);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}
@BeforeTest(alwaysRun = true)
public void before_test() throws IOException {
File dir = new File(directorypath);
if (dir.exists()) {
System.out.println(dir + " : is already present");
POSmileSource.deleletAllfiles(directorypath);
}
System.setProperty("webdriver.chrome.driver",
"D:/ChrisadReportAutomation/chromedriver.exe");
HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
chromePrefs.put("profile.default_content_settings.popups", 0);
chromePrefs.put("download.default_directory", directorypath);
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", chromePrefs);
options.addArguments("disable-popup-blocking");
// DesiredCapabilities cap = DesiredCapabilities.chrome();
// cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
// cap.setCapability(ChromeOptions.CAPABILITY, options);
// driver = new ChromeDriver(cap);
driver = new ChromeDriver(options);
driver.get(Constant.url);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}
@Test(priority = 1)
private void test_Authentication() {
driver.manage().window().maximize();
loginDemo = new POM_Page_Class(driver);
loginDemo.login();
}
@AfterTest(alwaysRun = true)
public void afterTest() {
driver.close();
}
}
===================================================================
At last the testNg file structure is as follows:
Which will run 3 classes. Ful path is defined for all the classes in testNg file.
===================================================================
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Default suite">
<test verbose="2" name="Default test">
<classes>
<class name="POM.Constant"/>
<class name="POM_Page.POM_Page_Class"/>
<class name="POM_testCase_package.POM_testCase"/>
</classes>
</test> <!-- Default test -->
</suite> <!-- Default suite -->
===================================================================
Comments
Post a Comment