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)...

MySQL practical Tutorials part 9- SQL not operator, SQL Not Like, SQL greater than, SQL less than greater than operator

 ========================================================================= Not Equal SELECT title FROM books WHERE released_year = 2017;   SELECT title FROM books WHERE released_year != 2017;   SELECT title, author_lname FROM books;   SELECT title, author_lname FROM books WHERE author_lname = 'Harris';   SELECT title, author_lname FROM books WHERE author_lname != 'Harris'; ========================================================================= Not Like SELECT title FROM books WHERE title LIKE 'W';   SELECT title FROM books WHERE title LIKE 'W%';   SELECT title FROM books WHERE title LIKE '%W%';   SELECT title FROM books WHERE title LIKE 'W%';   SELECT title FROM books WHERE title NOT LIKE 'W%'; ========================================================================= Greater Than SELECT title, released_year FROM books ORDER BY released_year;   SELECT title, released_year FROM books  WHERE released_year > 2000 ORDER BY release...

MULTIPLEXER , Design & Implement the given 4 variable function using IC74LS153. Verify its Truth-Table

TITLE: MULTIPLEXER   AIM: Design & Implement the given 4 variable function using IC74LS153. Verify its Truth-Table.   LEARNING OBJECTIVE: ·        To learn about IC 74153 and its internal structure. ·        To realize 8:1 MUX and 16:1 MUX using IC 74153.   COMPONENTS REQUIRED: IC 74153, IC 7404, IC 7432, CDS, wires, Power supply. IC PINOUT:            1)     IC 74153 2)      IC 7404:                                              3) IC 7432 THEORY:   ·        Multiplexer is a combinational circuit that is one of the most widely used in digital design. ·        The multiplexer is a data selector which gates one out of several inputs to a sin...