Error: Assertion method is not found in the page object - Unable to access property 'ok' as it is undefined

Recently, I encountered an issue with a small nightwatch test that checks for a positive number value in a price field. Everything runs smoothly when the code is directly within the test itself. However, when I tried moving it to my page object file as a function, I ran into errors.

checkPrice: function (){
    this.expect.element('@price').text.to.be.visible.before(1000);
    this.getText ('@price', function(result) {
       var myPrice = Number(result.value.replace('-.','').trim());
       this.assert.ok(isPositive(myPrice), 'price is a positive number');
    });
}, 

The error message reads: "Cannot read property 'ok' of undefined." Despite attempting to use assert.equal, I faced the same problem where the property was inaccessible. After some research, I found that the issue might be related to the scope of assert as a member of client.

Many discussions mention the tricky nature of using this, especially for someone like me with limited programming skills in JavaScript. Even switching from this to client resulted in "Cannot read property 'assert' of undefined".

Considering these challenges, I seek suggestions on why this setup isn't working and how I can resolve the issue. Any insights or recommendations would be greatly appreciated.

Answer №1

this undergoes a transformation once you step into the inner function's scope, so it's necessary to retain a reference to it.

checkPrice: function (){
    var _this = this;
    this.expect.element('@price').text.to.be.visible.before(1000);
    this.getText ('@price', function(result) {
       var myPrice = Number(result.value.replace('-.','').trim());
       _this.assert.ok(isPositive(myPrice), 'price is a positive number');
    });
}, 

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

Xpath: Choosing the parent node value in xpath when a child node is present

Trying to generate an HTML table using JavaScript based on XML data. When a child element like //host exists, I need to extract the value of a parent element called "name" from a different ancestor using XPath The required channel name can be found in th ...

Adjust the jQuery slider values and then transmit them using AJAX

I´m currently utilizing the jQuery slider plugin and have a specific goal in mind: 1. My aim is to establish different maximum values for the slider based on which li data-value element the user has selected. For instance, if the user clicks on the < ...

Storing a variable in session storage using Vue/Nuxt for persistent data

I am currently utilizing the Google Maps Javascript API to create a map object on my website. The challenge I am facing is that every time I navigate between pages and return to the map page, the map object gets reinstantiated unnecessarily, resulting in a ...

Adjusting the sizes of images with varying aspect ratios to perfectly fit within a div without distorting its shape

I'm currently in the process of developing my portfolio website and I am faced with creating a simplistic image browser feature. My goal is to have a container div that adjusts in size according to the browser window, allowing it to house images of va ...

Absorbing a mistake within my observable while also supplying a saved value - ensuring the subscription remains active

I'm really struggling with error handling in my observable and it's starting to drive me crazy. Despite reading countless articles, I still can't seem to get the hang of it. Ideally, I want to handle errors within the service itself and fall ...

What is the best way to compare keys and values in a JSON object in Lodash when one object contains additional keys?

Consider the following two JSON objects: var obj1 = {a: "apple", b: "banana", c: "carrot"} var obj2 = {a: "apple", e: “egg” b: "banana", c: "carrot", d: "dog"} I'm looking for ...

Steps to automatically redirect to the login page when a user is not authenticated in a Vue 3 frontend with Laravel as the backend

RoxanneSkywalker OP Posted 32 minutes ago Achievement Unlocked: Ten Thousand Strong Badge How can I implement a redirect to the login page when a user is not authenticated in Vue 3 with Laravel as the backend? Hey there, I am seeking guidance on how to re ...

Notification box appears only when closing the window, not when reloading the page

I've been trying to find a way to remind my users to log out before closing the window. I found this code that seems to work: window.onbeforeunload = confirmExit; function confirmExit() { return "It's best to log out before you close this pa ...

Searching to loop through JSON within a Sequelize / Postgres database query in order to identify a corresponding value

Hello everyone, I'm new here so please bear with me. I'm currently working on a project using Sequelize with PostgresDB in Node and I need to search for a contact by email using findOrCreate function. The email entry is stored in JSON format (se ...

Getting JSON data from PHP in AngularJS involves making an HTTP request to the PHP file

I've been searching extensively, but haven't found a solution. I need the data from my PHP script to be formatted like this: $scope.places = [{name: 'John'},{name: 'Jane'}]; My issue is figuring out how to make this happen. ...

Exploring locations using the Google Geolocation API

I have a website and am trying to determine the location of my visitors. I came across this code on Google Code: <script type="text/javascript" src="gears_init.js"></script> <script type="text/javascript"> var geo = google.gears.factory ...

Avoiding HTML (JSX) tag duplication in React Component

Having the same code in my React component multiple times is becoming repetitive and inefficient. Is there a way to follow the DRY principle and avoid repeating this code? While creating a landing page with Sass styling, I noticed that I am duplicating t ...

Testing the capabilities of AngularJS with e2e testing using the angular-recaptcha library

I have been attempting to e2e test my basic application, but I am facing challenges with the angular-recaptcha plugin from VividCortex (https://github.com/VividCortex/angular-recaptcha). Here is the test case I am working on: it('should redirect t ...

React-Redux has encountered an unforeseen token

I'm completely stumped as to why I keep receiving such a pesky syntax error: 30 | }) 31 | */ 32 | function mapStateToProps(state) { | ////////////////// ^ 33 | return { 34 | count: state.count ...

"Error: The term 'Readable' is not

When I input this code: const { Readable } = require('stream'); Readable ends up being undefined. However, when I try this code instead: import { Readable } from 'stream'; Readable becomes an empty object with no properties. It&apos ...

Move the menu button to the right of the title slide, beyond the edge of the card

Having an issue with the MuiCardHeader component. <CardHeader disableTypography avatar={renderAvatar()} action={ <IconButton onClick={toggleMenu}> <img src={MoreIcon} alt=""/> </IconButton ...

The update feature seems to be malfunctioning within the MEAN Stack environment, specifically with node.js and angular js

Looking for some assistance as a beginner in the mean stack. I'm trying to update a record using the update function, but it's not working as expected. I need to update a specific object based on its ID, however, I'm encountering issues wit ...

The issue arises with getInitialProps as it fails to pass data to the page component while attempting to retrieve initial information and subsequently modify it using a button

I am currently working on a component located at app\page.tsx in Next.js v13.4.12, and it includes a button. My goal is to have the button trigger the handleClick function when clicked. The issue I'm facing is that the getInitialProps function ...

Encountering an issue where utilizing a dynamic string as a function name results in the error message "Function not found."

Currently, I am facing an issue while trying to utilize a string as the function name in Jquery 2.2. Let me provide you with an example of what I have: var barBaz = 'myFunctionName'; Next, there is a specific function that I wish to invoke, and ...

Retrieving information from a JSON Object based on a specific key

There is a JSON Object available with the following data. { "headerContent": [ { "name": "User Id", "key": "userId", "type": "text", "default": & ...