Guide: Incorporating the Open Page Model Using JavaScript

I am currently working on setting up a page object model for the login page in my automation project.

conf.js

exports.config={
    seleniumAddress:'http://localhost:4444/wd/hub/',
    specs: ['spec.js']
}

spec.js

    describe('Test suite to verify the login functionality', function(){
    it('Verify browser settings', function(){
        browser.waitForAngularEnabled(false)
        browser.get('http://some-login-page.com');
        element(by.id('ctrlLogin_UserName')).sendKeys("valid-login");
        element(by.id('ctrlLogin_Password')).sendKeys("valid-password");
        element(by.id('ctrlLogin_LoginButton')).click();
    }); 
})

package.json

  "name": "automation",
  "version": "1.0.0",
  "description": "My first automation project",
  "main": "conf.js",
  "dependencies": {
    "jasmine": "^3.5.0",
    "protractor": "^5.4.3"
  },

If anyone has suggestions on how I can effectively implement the page object model on this login page, I would greatly appreciate it. Thank you!

Answer №1

To begin, you should establish an object that contains a method for each action you wish to perform/test on the page. It's best to start with something basic and build upon it as your code progresses. For example, you could create a Page Object like this:

var LoginPage = function() {
    var nameInput = element(by.id('ctrlLogin_UserName'));
    var PasswordInput = element(by.id('ctrlLogin_Password'));
    var LoginButton = element(by.id('ctrlLogin_LoginButton'));

    this.get = function() {
        browser.get('http://some-login-page.com');
    };

    this.setName = function(name) {
        nameInput.sendKeys(name);
    };

    this.setPassword = function(password) {
        PasswordInput.sendKeys(password);
    };

    this.submitLogin = function() {
        LoginButton.click();
    };
};
module.exports = new LoginPage();

For instance, you can include an additional method called login in your Page Object that takes both the username and password, and internally calls setName, setPassword, and submitLogin.

Once you have created the Page Object, you can easily utilize it in your test cases:

var LoginPage = require('./LoginPage');
describe('Test suite for validating login functionality', function(){
    it('Verify the browser', function(){
        browser.waitForAngularEnabled(false)
        LoginPage.get();
        LoginPage.setName('valid-login');
        LoginPage.setPassword('valid-password');
        LoginPage.submitLogin();
    });
})

If you have implemented the extra login() method in your Page Object, you can simply use it in your tests to reduce the amount of code written.

Now, whenever you need to perform a login operation in your tests, you can rely on your convenient abstraction layer of Page Objects. If the element IDs change, you only need to update them in one place. Life just became simpler ;-)

P.S. For more information, refer to the protractor docs at: https://github.com/angular/protractor/blob/master/docs/page-objects.md (I shamelessly borrowed my examples from there)

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Choose all options within the optgroup group

I am facing an issue with a select element that contains grouped options. My requirement is to be able to select or deselect all options within an optgroup when a specific option is clicked. Furthermore, I need the functionality to allow multiple optgroups ...

Button react-native press following textInput within scroll view aware of keyboard movements

I'm currently facing an issue where I have a TextInput and a button nested inside a KeyboardAwareScrollView. The goal is for the user to input some text and then tap the button created using TouchableOpacity, which should send the inputted text forwar ...

Modify the parent div's background color on mouseover of the child li

I want to update the background image of a parent div that contains a series of ul li elements. Here is the HTML code I am working with: <section class="list" id="services"> <div class="row"> <ul> <li>& ...

Retrieve data from a JSON file

I have a JSON file containing various player data, and I need to extract the "Name" field from it. { "player": [ { "Position": "TEST", "Name": "TEST", "Squad_No": "TEST", "Club": "TEST", "Age": "TEST" }, ...

In Firefox, the parent div's width is greater than the combined width of its children

In Firefox, I am encountering a peculiar problem. The issue arises when I have a div with a height defined in a fixed px value, and an img element nested within it. While this configuration works fine in Chrome, in Firefox, the parent div's width end ...

How to identify an element based on the text content of the element that follows it using XPath

I am working with this specific piece of HTML code <div class="a-expander-content a-spacing-base a-expander-inline-content a-expander-inner a-expander-content-expanded" style="" aria-expanded="true"> <form id="pp-yT-23" class="pmts-add-cr ...

Nodejs: A function is expected in the instanceof check, but an undefined value was provided

Encountering a peculiar error while using node-webkit, here's a detailed example to replicate it: index.js: var jQuery = require('jquery'); var Backbone = require('backbone'); (function($){ var ListView = Backbone.View.extend( ...

What is the method, by using JavaScript or CSS, to include extra space at the end of text block without starting a new line?

Imagine having some text, and at the conclusion of each paragraph there is a (more)... link. The layout ends up looking like this: This is just an example paragraph. Click on the more link for additional information. (more...) Now comes the desire to i ...

The bootstrap function for showing the modal with the ID "myModal" is malfunctioning

As someone who is just getting started with PHP and Bootstrap, I am running into an issue where the modal doesn't seem to work when triggered using the .modal method. Here is the PHP code I am using: if ($show_login_modal) { echo " <scr ...

Do you know if there is a setting in prettier that allows line breaks to be preserved?

Encountering an issue with the prettier extension in VS Code, Whenever I enter the following code: const result = await pool .request() .query('select NumberPlate, ID, TimeStamp from RESULTS order by ID'); and save the file, it con ...

python using selenium to locate the second element with xpath

In search of the solution to finding the path to the second element. How can I create this path? My attempt at locating the second element using xPath: getOpis = driver.find_element_by_xpath("(//article[2]/div/div/descendant-or-self::*)").text Unfortuna ...

Utilizing .show() and .hide() in conjunction with the opener

I have a website with a few photos, and when a user clicks on a photo, a div opens up to display an album, similar to Facebook's photo viewer. I want to be able to press the ESC key to go back to the original page. Inside the showAlbumDiv div, an ifr ...

The error message keeps popping up in my Eclipse IDE whenever I try to utilize the Selenium JAR files

Here is a basic Java code snippet for selenium: package myPackage; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class MyClass { public static void main(String[] args) { System.out.println("My ...

Tips on disregarding events within an html table

I have a see-through table with a width set to 100% that houses various HTML content. I am utilizing the table to properly center a particular element on the screen. However, the table is intercepting mouse events and preventing users from clicking on lin ...

Maintaining original coordinates while merging meshes in three.js

I'm working with threejs and my goal is straightforward. I aim to achieve the following: Create two cubes positioned with a 1 cube-wide gap in between Combine them into a single geometry object using geometry.merge Unfortunately, despite hours of e ...

Using webdriver selenium to extract information from various websites

I am attempting to extract data from multiple URLs and save them in a CSV file. However, with the current code that I have, although it successfully opens all three sites, it only saves the data from the final link, including all of its pages. data = {} ...

Unable to start the project - the selected option is not able to be launched and there have been no recent launches

I encountered the following issue: Yesterday, I was working on my Selenium and Maven project in Java using Eclipse. After finishing my work, I exported the project as an archive. Today, when I tried to import the entire project and run it on a different c ...

Expo is having trouble locating the module "color-convert" - such a frustrating problem

Having an issue! I ran the command npm start in my terminal and received the following error: Starting project at /home/pc/Documents/Projects/Mobile/weather_app Developer tools running on http://localhost:19002 Cannot find module 'color-convert' ...

Retrieving the value of a JSON object within a nested response

I need to extract specific values from a JSON response related to cars. Specifically, I want to retrieve the values for car, car.Make, and car.Price. However, my current method does not seem to be working. Here's what I've tried: $.each(jqXHR. ...

Having trouble setting a value as a variable? It seems like the selection process is not functioning properly

My Hangman game has different topics such as cities and animals. When a user selects a topic, the outcome should be a random item from that specific topic. For example: London for cities or Zebra for animals. Currently, I am only generating a random lett ...