Skip to main content

POM in Selenium using Java and testNG

=================================================================== 

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

Popular posts from this blog

Add, remove, search an item in listview in C#

Below is the C# code which will help you to add, remove and search operations on listview control in C#. Below is the design view of the project: Below is the source code of the project: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace Treeview_control_demo {     public partial class Form2 : Form     {         public Form2()         {             InitializeComponent();             listView1.View = View.Details;                   }         private void button1_Click(object sender, EventArgs e)         {             if (textBox1.Text.Trim().Length == 0)...

display files and directories in Listview

Below is the C# code which displays all files and directories in listview control with their file size and creation date. If it is file then it also displays the extension of the file e.g. .txt, .jpg etc Below is the design view of the project: Listview to display files and directories with size and date created Below is the source code of the project: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.IO; namespace search_in_listview {     public partial class Form1 : Form     {         public Form1()         {             InitializeComponent();                   }         private void button1_Click(object sender, EventArgs ...

Add worklog in Jira using Python

 Below is the Python code to add the worklog in Jira. You need to install a request library for this. Here is the code: import requests from requests.auth import HTTPBasicAuth import json url = "https://your jira address here/rest/api/2/issue/ticket_number/worklog" auth = HTTPBasicAuth("username", "jira access token") headers = {     "Accept": "application/json",     "Content-Type": "application/json" } payload = json.dumps({     "comment": {         "content": [             {                 "content": [                     {                         "text": "This is for QA Testing",                         "type": "text"                     } ...