При создании объекта страницы в webdriver.io произошла ошибка .setValue не является функцией / не может прочитать свойство 'setValue' из неопределенного - PullRequest
2 голосов
/ 06 марта 2020

Я создаю автоматизацию webdriver.io с файлом объекта страницы (login.po. js) и файлом test spe c (test.spe c. js), но, похоже, чтобы не быть в состоянии распознать объект, когда я вызываю его в тестовом файле spe c (test.spe c. js), он показывает ошибку LoginPage.username.setValue is not a function.

Вот мой код ниже:

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.spe c. js

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

const userObj = {
    user: 'username@email.com',
    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!');
    });
});

ошибка, которая может возникнуть при его запуске:

 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

помощь по этому вопросу будет значительно оценена, и если вы также обнаружили другую проблему в моем коде, пожалуйста, помогите мне исправить ее, большое спасибо


изменение последней строки кода в login.po. js

exports.LoginPage = LoginPage;

показывает ошибку:

  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

Редактирование первой строки кода в test.spe c. js:

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

по-прежнему показывает ошибку:

  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

GitHub репозиторий для полного кода на это: github.com/seanray7/pageobject-webdriverio

1 Ответ

2 голосов
/ 07 марта 2020

spe c file

const LoginPage = require('../page/login.po').LoginPage;
const userObj = {
    user: 'username@email.com',
    password: 'password',
}
const assert = require('assert');
const expect = require('chai').use(require('chai-as-promised')).expect;


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

    it('should have the right title ', async ()=> {
        await browser.url('http://the-internet.herokuapp.com/login');
        // expect(browser.getTitle()).to.eventually.equal('The Internet');
        let title = await browser.getTitle();
        console.log(title);
       return assert.equal(title, 'The Internet');
    });
    it('should deny access with creds', ()=> {
       return LoginPage.LoginPageLibs.Login(userObj.user,userObj.password);   
    });
});

файл подкачки

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 sumbit = await browser.$(LoginPageLocator.form)
       return sumbit.click();
    },
    getText:async(elem)=>{
        let element = await browser.$(elem);
        return element.getText();
    }
}

exports.LoginPage = {LoginPageLocator,LoginPageLibs};

См. Этот снимок экрана, он успешно работает.

enter image description here

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

...