Protractor is unable to utilize local properties defined within the .then statement

Lately, I've been working on streamlining my Protractor e2e tests using the Page Object model for better reusability. Each Page Object typically starts with defining elements on that page followed by local functions. Here's an example:

'use strict';

var examplePage = function() {
    // Locators
    this.button1   = $('#button1')
    this.element1  = $('#element1');
    this.element2  = $('.element2');

    // Error Messages
    this.ERROR_ONE = 'There was an error!';

    // Protractor ExpectedConditions instance
    var until = protractor.ExpectedConditions;

    this.doSomeStuff = function() {
        this.button1.click().then(function() {
            browser.wait(until.visibilityOf(this.element1), global.TIMEOUT_INTERVAL_SHORT, this.ERROR_ONE);
            return this.element1;
        });
    };
};
module.exports = examplePage;

The code above serves as a simple illustration, but my actual code closely resembles it.

Sometimes, I face challenges in controlling the order of function execution to prevent premature execution. However, this creates a restricted scope inside the .then statement. For instance, 'this.ERROR_ONE' property becomes inaccessible, making it hard to maintain consistency across my Page Objects.

Has anyone else encountered this issue and found a solution? It can get cumbersome keeping track of the same values across multiple Page Objects.

Answer №1

The Protractor team has provided a helpful tutorial on implementing the PageObject pattern for Protractor frameworks. One key point is that you should not use the keyword this to declare variables.

In the tutorial, it demonstrates direct access to objects as shown below:

var examplePage = function() {
    // Locators
    var button1   = $('#button1');
    var element1  = $('#element1');
    var element2  = $('.element2');

    // Error Messages
    var ERROR_ONE = 'There was an error!';

    // Protractor ExpectedConditions instance
    var until = protractor.ExpectedConditions;

    this.doSomeStuff = function() {
        this.button1.click().then(function() {
            browser.wait(until.visibilityOf(element1), global.TIMEOUT_INTERVAL_SHORT, ERROR_ONE);
            return element1;
        });
    };
};
module.exports = examplePage;

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

"Enhancing security measures with multiple nonce for Content Security Policy

I am encountering an issue with my single page application, which is developed in .net core MVC 2.2. The application loads html sections on the fly. In the main document, I have added a Content Security Policy (CSP) policy with a dynamically generated hea ...

How to store data retrieved with $http.get in AngularJS into a variable

I am attempting to assign data retrieved from $http.get to a variable in my controller. $http.get(URL).success(function (data) { $scope.results = data; console.log('results within $http.get :'+ $scope.results); }); console.lo ...

NodeJS: Implementing external URL redirection functionality

I've set up a GET /auth route that is supposed to redirect to an external resource, like https://google.com. However, instead of redirecting me to the correct URL, it redirects me to http:localhost:3000/api/auth/https://google.com. Is there a way to ...

Efficiently convert a JSON array into an HTML table using the Jquery

Currently, I'm facing a challenge in my project. The issue arises when a query is sent to the server using jQuery, and an array is returned as a response. My struggle lies in transferring this data to a dynamic table since the column count varies with ...

Conceal the <p> tag if the content inside is either "0" or blank

I'm currently working on a basic calculator project and I've hit a roadblock. I need to hide certain elements based on conditions. The code snippet with explanations is provided below. function calculateArea() { var length = document.getElem ...

collaborate and coordinate a territory among various components on a map

I'm currently working with an array of elements that are being drawn on a canvas. export function useCanvas(){ const canvasRef = useRef(null); const [ elements, setElements] = useState([]); const [ isHover, setIsHover] = useState(false); ...

determining the dimensions of an SVG element following skew transformation using JavaScript

I have an SVG element called rect that has the following attributes: <rect x="50" y="10" width="20" height="30" style="stroke: #000000; fill:none;" /> After applying a transformation of skewX(10), the dimensions of the rectangle are altered. How c ...

Main.js in Electron cannot find the NODE_ENV environment variable

Currently, I am in the process of developing an application which involves: React 16.4.0 Electron 2.0.2 Webpack 4.11.0 After successfully compiling and running the app using webpack (webpack dev server), my focus now shifts to showing only Chr ...

What is the procedure for updating a state property's value?

Currently, I am in the process of developing a small application that has the ability to fetch data, display it in the DOM, and allow users to select an item to view detailed information about the chosen user. To manage all these functionalities, I am util ...

How can I input text into separate tabs using a specific method?

I've been struggling with this issue for a while now and here's the code I have so far: <html> <head> <script src="http://mihaifrentiu.com/wp-content/themes/mf/js/jquery_1.7.1.min.js" type="text/javascript"></scr ...

ng-include does not incorporate a partial view

I am facing an issue with ng-include as this code is not functioning properly and I'm unable to identify the error. <select name="select" id="select" class='input-large' ng-model="selectedbien"> ...

Creating a cascading layout with React Material-UI GridList

Can the React Material-UI library's GridList component be used in a layout similar to Pinterest's cascading style? I have utilized Material-UI Card components as children for the GridList: const cards = props.items.map((item) => <Card ...

Certain characteristics remain constant while others experience variations

I have a code that successfully changes the header opacity when scrolling down, but now I want it to change the background color instead. Strangely, the code does not seem to affect this attribute, although it works fine for other attributes like opacity a ...

Troubleshooting problem with uploading files using ChromeDriver in Selenium WebDriver

When submitting a merge request on GitLab, I am facing an issue with attaching a file from my local drive. After clicking the "Attach a file" button and selecting the file locally, it appears in the text area input. https://i.stack.imgur.com/gXUMq.jpg Ho ...

`the issue of $scope object not being passed correctly to ng-if and ng-class ternary conditions

**app.js:** $scope.servers = [ {name:'SQL Server', status:"up"}, {name:'Web Server', status:"down"}, {name:'Index Server', status:"down"} ]; **index.html:** <table> ...

Retrieving the Short Date Format from the user's device or browser within a React application

Currently, I am in the process of utilizing reactjs along with material UI datepicker. My objective is to transmit the short date format to the datepicker component, such as format="MM/dd/yyyy". In addition, I wish to employ the pre-existing date ...

When I click on the md-select element, a superfluous "overflow-y: scroll;" gets added to the <body> of my webpage

Typically, I have the following styles on my body: element.style { -webkit-app-region: drag; } However, when I interact with a md-select element (you can observe this behavior on the provided link), additional styles are applied. element.style { -w ...

Webpack is unable to locate the necessary files that are not present

I am currently attempting server-side rendering and have copied my course teacher's code. The structure of my src folder is as follows: /src -> [/client -> index.jsx, /server -> server.js, /shared -> Header.jsx]. Unfortunately, I encounter ...

Effortlessly creating a sine wave effect for a link underline on hover using CSS and jQuery

Here is an example: $(function(){ var x = 0; setInterval(function(){ x-=1; $(".link-wavy:hover").css('background-position', x + 'px 100%'); }, 20); }) body { font-family: 'So ...

Enhance the functionality of jqGrid by customizing key bindings for arrow and tab keys

In order to enhance my jqGrid functionality, I am seeking to implement the ability for users to press different keys for specific actions. For example, pressing Enter should save data (which is currently working fine), while pressing the left arrow key sho ...