Encountering a problem while creating a Page Object in webdriver.io - getting the error "setValue is not a function" or "cannot read property 'setValue' of undefined"

While working on a webdriver.io automation project, I encountered an issue with recognizing objects in my page object file (login.po.js) when calling them in the test spec file (test.spec.js). The error message displayed is

LoginPage.username.setValue is not a function
.

Here is the code snippet from my files:

login.po.js

var LoginPage =  {
    username: { get: function () { return $('#email'); } },
    password: { get: function () { return $('#password'); } },
    form:     { get: function () { return $('#login'); } },
    flash:    { get: function () { return $('#flash'); } },

    submit: { value: function() {
        this.form.click();
    } }
};
module.exports = LoginPage;

test.spec.js

var LoginPage = require('../page/login.po');

const userObj = {
    user: '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="463335233428272b2306232b272f2a6825292b">[email protected]</a>',
    password: 'password',
}
var assert = require('assert');

describe('login form', () => {

    it('should deny access with wrong creds', function () {
        LoginPage.username.setValue('username');
        LoginPage.password.setValue('password');
        LoginPage.submit();
        browser.pause(5000);
        expect(LoginPage.flash.getText()).to.contain('Your username is invalid!');
    });

    it('should allow access with correct creds', function () {
        LoginPage.username.setValue(userObj.user);
        LoginPage.password.setValue(userObj.password);
        LoginPage.submit();
        browser.pause(5000);
        expect(LoginPage.flash.getText()).to.contain('You logged into a secure area!');
    });
});

The errors that occurred during execution are shown below:

1) login form should deny access with wrong creds
LoginPage.username.setValue is not a function
TypeError: LoginPage.username.setValue is not a function
at Context.<anonymous> (D:\MyTest00\specs\test.spec.js:31:28)
at Context.executeSync (D:\MyTest00\node_modules\@wdio\sync\build\index.js:56:18)
at D:\MyTest00\node_modules\@wdio\sync\build\index.js:82:70

2) login form should allow access with correct creds
LoginPage.username.setValue is not a function
TypeError: LoginPage.username.setValue is not a function
at Context.<anonymous> (D:\MyTest00\specs\test.spec.js:45:28)
at Context.executeSync (D:\MyTest00\node_modules\@wdio\sync\build\index.js:56:18)
at D:\MyTest00\node_modules\@wdio\sync\build\index.js:82:70

I would appreciate any assistance in resolving this issue. Additionally, if you notice any other problems in my code, please feel free to suggest corrections. Thank you!


When I changed the last line of code in login.po.js to:

exports.LoginPage = LoginPage;

An error was displayed:

1) login form should deny access with wrong creds
Cannot read property 'setValue' of undefined
TypeError: Cannot read property 'setValue' of undefined
at Context.<anonymous> (D:\MyTest00\specs\test.spec.js:20:28)
at Context.executeSync (D:\MyTest00\node_modules\@wdio\sync\build\index.js:56:18)
at D:\MyTest00\node_modules\@wdio\sync\build\index.js:82:70

2) login form should allow access with correct creds
Cannot read property 'setValue' of undefined
TypeError: Cannot read property 'setValue' of undefined
at Context.<anonymous> (D:\MyTest00\specs\test.spec.js:28:28)
at Context.executeSync (D:\MyTest00\node_modules\@wdio\sync\build\index.js:56:18)
at D:\MyTest00\node_modules\@wdio\sync\build\index.js:82:70

Even after editing the first line of code in test.spec.js to:

var LoginPage = require('../page/login.po').LoginPage

The same error persisted:

1) login form should deny access with wrong creds
LoginPage.username.setValue is not a function
TypeError: LoginPage.username.setValue is not a function
at Context.<anonymous> (D:\MyTest00\specs\test.spec.js:31:28)
at Context.executeSync (D:\MyTest00\node_modules\@wdio\sync\build\index.js:56:18)
at D:\MyTest00\node_modules\@wdio\sync\build\index.js:82:70

2) login form should allow access with correct creds
LoginPage.username.setValue is not a function
TypeError: LoginPage.username.setValue is not a function
at Context.<anonymous> (D:\MyTest00\specs\test.spec.js:45:28)
at Context.executeSync (D:\MyTest00\node_modules\@wdio\sync\build\index.js:56:18)
at D:\MyTest00\node_modules\@wdio\sync\build\index.js:82:70

You can find the full code for this project on GitHub at:

github.com/seanray7/pageobject-webdriverio

Answer №1

specification file

const LoginPage = require('../page/login.po').LoginPage;
const userObj = {
    user: '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="05707660776b646860456068646c692b666a28">[email protected]</a>',
    password: 'password',
}
const assert = require('assert');
const expect = require('chai').use(require('chai-as-promised')).expect;


describe('login form', () => {

    it('should display the correct title ', async ()=> {
        await browser.url('http://the-internet.herokuapp.com/login');
        let title = await browser.getTitle();
        console.log(title);
       return assert.equal(title, 'The Internet');
    });
    it('should not grant access with provided credentials', ()=> {
       return LoginPage.LoginPageLibs.Login(userObj.user,userObj.password);   
    });
});

page file

const LoginPageLocator = {

    username: '#username',
    password: '#password',
    form: '//i[contains(text(),"Login")]',
    flash: '#flash'

};

const LoginPageLibs = {
    Login : async (username, password) => {
        let name_ele = await browser.$(LoginPageLocator.username);
       await name_ele.setValue(username);
        let password_ele = await browser.$(LoginPageLocator.password);
       await password_ele.setValue(password);
        let submit = await browser.$(LoginPageLocator.form)
       return submit.click();
    },
    getText:async(elem)=>{
        let element = await browser.$(elem);
        return element.getText();
    }
}

exports.LoginPage = {LoginPageLocator,LoginPageLibs};

Please see attached screenshot for successful run.

https://i.stack.imgur.com/L2710.png

Github - https://github.com/Bharath-Kumar-S/Wdio_sample.git

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

Guide for incorporating Google maps into a Vue.js application

Can anyone help me with displaying a Google map using Vue.js? I've tried but it's not showing up, and there are no errors in the console. Below is the code I'm using, and I need to display the map within that specific div tag. <template& ...

Enhance your text in TextInput by incorporating newline characters with advanced editing features

I'm encountering an issue with my Textarea component that handles Markdown headers: type TextareaProps = { initValue: string; style?: StyleProp<TextStyle>; onChange?: (value: string) => void; }; type OnChangeFun = NativeSynthetic ...

Transforming the current date() into a distinctive format, encompassing the utilization of T

Background In my Angular UI, I have a datepicker that is defined as: <date-picker name="contractEndDate" date="employee.contractEndDate"></date-picker> When the button is clicked, the contractEndDate value changes from null to the current da ...

Sloped Divider on the Upper Edge of the Page

I'm currently in the process of developing a new website, and I'm looking to create a unique design for the main navigation bar on the homepage. Here is the ideal layout that I have in mind: https://i.stack.imgur.com/pc8z4.png While I understan ...

Having trouble choosing an option from the drop-down menu despite trying various methods and still unable to make a selection

My coding journey is reflected in the following trials: package com.qa.pages; import java.util.List; import org.openqa.selenium.By; import ... miscellaneous code ... //After numerous attempts and trials, unfortunately no success. public static void te ...

Issue encountered when making API requests in JavaScript that is not present when using Postman

Currently, I am developing a web application using express and one of the functionalities is exposed through an API with an endpoint on 'api/tone'. This API acts as a wrapper for one of Watson's services but I choose not to call them directl ...

"Utilizing AngularJS to asynchronously send an HTTP POST request and dynamically update

I've been working on an angularjs chat module and have come across a challenge. I developed an algorithm that handles creating new chats, with the following steps: Click on the 'New Chat' button A list of available people to chat with wil ...

When sending an AJAX request to a URL, can I verify in the PHP page whether it is a request or if the page has been accessed?

In order to enhance security measures, I need to prevent users from accessing or interacting with the php pages that will be utilized for ajax functionality. Is there a method available to determine if a page has been accessed through an ajax request or b ...

Modify the starting URL in a Node.js application to /foo instead of just /

I am looking to visit a website with a default index URL like localhost:XXXX/foo, instead of the usual localhost:XXXX/. How can I achieve this specific setup? Any suggestions on how to make this happen? ...

Unable to invoke the jQuery function within CakePHP3

I am having trouble calling the jQuery function mentioned below in my CakePHP3 MVC project. There are no error messages, but nothing seems to be happening. The code is set up so that jQuery gets included automatically. The function I am trying to call sh ...

Can an HTML DIV be resized along with its contents?

Is it possible to scale a container with animated elements inside to fit the browser window, or do you have to adjust each child element individually? ...

"Enhancement in Chrome: Inclusion of Origin header in same-origin requests

When we POST an AJAX request to a server running locally, the code looks like this: xhr.open("POST", "http://localhost:9000/context/request"); xhr.addHeader(someCustomHeaders); xhr.send(someData); The webpage where this javascript is executed is also on ...

Fill a form with jQuery and ajax data after it has been submitted

I'm working on a simple HTML form and I want to use Ajax to perform a lookup using a PHP file after entering data into the first field. The goal is to fetch information from an external source for the two remaining fields. <form method="post" acti ...

Navigating Redirect Uri in Ionic for third-party API integration

After creating a webapp, I decided to venture into developing a mobile app using Ionic. The idea of making a progressive web app crossed my mind, but unfortunately Apple doesn't support it yet. (By the way, I'm still quite new to all of this) F ...

What could be causing one of my images to not appear on my Gatsby page?

Hey there, I could use some help with this issue: Recently, I created a website using Gatsby.js and deployed it to my web server (which is running NGINX on Ubuntu 20.04). The site uses Gatsby Image for querying and displaying images, and everything seems t ...

Why is the current Menu Item highlight feature not functioning properly?

Why isn't the highlight current Menu Item feature working? I've checked my code, but it doesn't seem to be functioning as expected. Could you lend me a hand? Html: <section id="menu-container"> <div id="bar"><img src="b ...

Exploring the world of color in Google visualization charts

I am currently working on a project that requires me to add different colors to specific zones on a graph. I am looking to use colors such as blue, red, yellow, and green. Here is the outcome I have achieved: I am aiming for something similar to this des ...

Resource loading unsuccessful: server encountered a status of 500 (Internal Server Error)

I'm struggling to figure out why I keep getting an Internal Server Error when trying to call a web service in my HTML page using JavaScript and Ajax. Here is the error message: Failed to load resource: the server responded with a status of 500 (Int ...

Using NextJs to track the position of a scrollbar

Incorporating NextJs for Server Side Rendering has been a game-changer for me. I've also implemented a navbar in my application that needs to adjust styles based on the scroll position. Is there a way to determine if the window has scrolled beyond 100 ...

Looking for a way to scroll images without relying on the marquee tag? Whether you prefer using JavaScript, jQuery,

<marquee> <div class="client"> <img src="images/c1.png"/> </div> <div class="client"> <img src="images/c2.png"/> </div> <div class="client"> ...