使用定位器找不到任何元素:By(css选择器,* [id =“用户名”])

时间:2018-10-09 05:49:31

标签: protractor cucumber

我在这里尝试非常基本的量角器黄瓜脚本。

步骤定义文件具有以下代码。

var {setDefaultTimeout} = require('cucumber');
var {defineSupportCode} = require('cucumber');
setDefaultTimeout(60 * 1000);
var LoginPage = require('../../PageObjects/loginPage.js');
var chai = require('chai');
var chaiAsPromised = require('chai-as-promised');

chai.use(chaiAsPromised);
var expect = chai.expect;

defineSupportCode(function ({And, But, Given, Then, When}) {

    When(/^I enter "([^"]*)" as user name$/, async (username) => {
        await LoginPage.setUserName(username);
       // callback.pending();
    });
    When(/^I enter "([^"]*)" as password$/, async (password) => {
      await LoginPage.setPassword(password);
       // callback.pending();
    });
    When(/^I click login button$/, async () => {
        await LoginPage.login();
       // callback.pending();

    });

    Given(/^I login into tutorialpoint"$/, async () => {
        await LoginPage.loadpage('https://www.tutorialspoint.com/angularjs/angularjs_login_application.htm');

    });

});

LoginPage.Js具有以下代码。

var LoginPage = function() {
    var username = element(By.id('username'));
    var password = element(By.id('password'));
    var loginform = element(By.id('form'));


    this.loadpage = function(site) {
        browser.ignoreSynchronization = true;

        browser.get(site);
    };

    this.setUserName = function(name) {
        username.sendKeys(name);
    };

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

    this.login = function() {
      loginform.submit();
    };
};
module.exports = new LoginPage();

尽管演示登录站点中存在元素,但我总是遇到错误。

[16:42:12] E /启动程序-使用定位器未找到任何元素:By(css选择器,* [id =“用户名”]) [16:42:12] E / launcher-NoSuchElementError:使用定位器找不到任何元素:By(css选择器,* [id =“用户名”])

这仅仅是因为代码中的问题吗?

1 个答案:

答案 0 :(得分:0)

您应该更改为LoginPage.js中的函数的返回值,如下所示:

this.loadpage = function(site) {
    browser.ignoreSynchronization = true;

    browser.get(site);

    // sleep 10 seconds to wait page loading
    // change to browser.wait() for production script.
    return browser.sleep(10*1000) 
};

this.setUserName = function(name) {
    return username.sendKeys(name);
};

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

this.login = function() {
  return loginform.submit();
};