Skip to main content

Protractor Page Object Model

Hi All, I would like to share my learning regarding Protractor Page Object Model (POM). Protractor is an end to end test framework used to automate Angular web applications.

In POM, for each web page there should be corresponding page class. Carrying out the task of "Finding the web elements and methods" will be done from the page class. This of course improves the maintainability and reusability of the code.

Here I will show a simple example without Page Object Model which I have written in Javascript using Protractor framework.

describe('Angular home page', function () {

    it('should add the String in todo list ', function () {
        browser.get('https://angularjs.org/');
        element.all(by.css("[placeholder='add new todo here']")).sendKeys("Add POM");
        element(by.css('input.btn-primary')).click();

        var getText = element.all(by.css('label.checkbox'));
        getText.get(2).getText().then(function (results) {
            expect(results).toEqual("Add POM");
        });
    });
});
To improve the code we will write the test script with Page Object Model, for that we should create a new page class to apart the web elements and the methods.

var HomePage = function () {

    var textField = element.all(by.css("[placeholder='add new todo here']"));
    var addButton = element(by.css('input.btn-primary'));
    var textResults= element.all(by.css('label.checkbox'));

    this.setText = function (text) {
        textField.sendKeys(text);
    };
    this.clickAddButton = function () {
        addButton.click();
    };
    this.getText=function () {
        return textResults;
    };
};
module.exports = new HomePage();

To run the test we will write a script to use the Page objects and the methods.
describe('Angular page', function () {
    it('should add the String in todo list ', function () {
        browser.get('https://angularjs.org/');

        var angularHomePage = require(protractor.basePath + '/e2e-tests/blog/angularPage.js');
        angularHomePage.setText("Add POM");
        angularHomePage.clickAddButton();

        var getText = angularHomePage.getText();
        getText.get(2).getText().then(function (results) {
            expect(results).toEqual("Add POM");
        });
    });
});

Now You are set to run the test script with Page Object Model, Which is useful when the test suite grows with addition of new test cases, Page Object classes can be used in any number of test scripts which uses the same set of elements defined in the respective page object class.


Comments

Popular posts from this blog

Katalon studio Execution Profiles

Hi Folks, I want to share my experience regarding the learning, My project manager asked me to find the best way to automate the web application for the regression/retest purpose. So I had a research on the tools and technologies regarding automation. So I found this :) In this post I would like to share my knowledge on Katalon Studio Profiles.  Let's have a quick idea regarding Katalon studio. Katalon studio is a free automation testing tool and supports the web and mobile environment, and built on top of the open-source automation frameworks Selenium, Appium with a specialized IDE. Pre requests - You can download Katalon studio from this link. I am using the latest version in order to avoid unnecessary errors. First, I will show how to record the Web application via Katalon studio. Find below mentioned test scenarios which are going to be recorded Launch the application (URL : http://demoaut.katalon.com/ ) Make an Appointment Login using provided credentials...

Data Driven Protractor Testing

In this article I would like to share knowledge regarding Data driven testing. When using a Data driven automation test framework, we do not need to hard code the test data. Instead it enables to access the test data from a separate data file and use them in the automated test cases. First, let's take a look at a simple Protractor script without Data Driven framework describe('Angular home page', function () {       it('should add the String in todo list ', function () {          browser.get('https://angularjs.org/');          element.all(by.css("[placeholder='add new todo here']")).sendKeys("Add POM");          element(by.css('input.btn-primary')).click();          var getText = element.all(by.css('label.checkbox'));          getText.get(2).getText().then(function (results) {          expect(results).toEqua...

Protractor with Gitlab and Jenkins

Hi everyone.. welcome to the first article in my new series of articles on Automation Testing. For a change, let's start from the End. Let's assume you have written your automation code in protractor and your project is ready to be pushed in to the repository. So let's start with a brief introduction on how to push our automation code to a Git repository. In this lesson I will be using GitLab repository. First things first.. You can't just push the code to a repository. Yeah.... you have to install Gitlab and make sure you have configured it properly. Try following command to make sure Git have been installed in your machine. git --version Hope Git have been installed in your machine, Let's start with GitLab configuration. Sign up with GitLab account Go to GitLab account and get register with your email account To identify you as the author configure your Git username and email address in your machine git config -- global user.name git config -- g...