无法在测试套件

时间:2015-12-29 16:34:40

标签: javascript node.js selenium selenium-webdriver

尝试使用selenium-webdriver和node将页面对象模式实现到我的测试套件中时,我遇到了一个奇怪的问题。

我可以使用第一页对象pageObject / admin / login / index.js。此文件具有选择器和方法,用于填写登录页面上的表单,允许用户自行登录。登录后,下一页对象完全失败。下一步应该只是验证页面上是否找到了用户名(使用适当的选择器和getText方法)。

这是我的测试套件:

var driver = require('selenium-webdriver');
var chai = require('chai');
var sinon = require('sinon');
var sinonChai = require("sinon-chai");
var expect = chai.expect;
var should = chai.should;
var config = require('../../../../variables');
var AdminPage = require('../../../../pageObject/admin/login/index.js');
var HeaderProfile = require('../../../../pageObject/admin/login/common/headerProfile/index.js');

chai.use(require('chai-as-promised'));
chai.use(sinonChai);

describe('Webdriver - Admin Tests - User Alice', function() {
  var adminVar = process.env.envVar;
  var login = config[adminVar].adminLogin;
  var user = config[adminVar].role.adminUser.name;
  var email = config[adminVar].role.adminUser.email;
  var pw = config[adminVar].role.adminUser.password;
  before(function() {
    this.timeout(50000);
    this.driver = new driver.Builder().withCapabilities(driver.Capabilities.firefox()).build();
    this.driver.get(login);
    var adminPage = new AdminPage(this.driver);

    adminPage.fillEmail(email);
    adminPage.fillPassword(pw);
    adminPage.signin();

    return this.driver.manage().timeouts().implicitlyWait(250);
  });

  after(function() {
    return this.driver.quit();
  });

  describe('would like to', function() {
    it('verify they have global access', function() {
      this.driver.manage().timeouts().implicitlyWait(500);

      var headerProfile = new HeaderProfile(this.driver);

      return expect(headerProfile.getName()).to.eventually.equal(user);
    });
  });

});

以下是我登录的页面对象:

/*jslint node: true */
"use strict";
var webdriver = require('selenium-webdriver');
var config = require('../../../variables.json');

var adminVar = process.env.envVar;
var login = config[adminVar].adminLogin;
var user = config[adminVar].role.adminUser.name;
var email = config[adminVar].role.adminUser.email;
var pw = config[adminVar].role.adminUser.password;


class AdminPage {
  constructor(driver) {
    this.driver = driver;
    this.driver.manage().timeouts().implicitlyWait(2000);

    this.emailField = this.driver.findElement({
      css: 'input[name="email"]'
    });

    this.passwordField = this.driver.findElement({
      css: 'input[name="password"]'
    });

    this.loginButton = this.driver.findElement({
      css: 'button[type="submit"]'
    });
  }

  fillEmail(email) {
    this.emailField.sendKeys(email);
  }

  fillPassword(password) {
    this.passwordField.sendKeys(password);
  }

  signin() {
    this.loginButton.click();
  }
}

module.exports = AdminPage;

和headerProfile的页面对象:

/*jslint node: true */
"use strict";
var webdriver = require('selenium-webdriver');


class HeaderProfile {
  constructor(driver) {
    this.driver = driver;
    this.driver.manage().timeouts().implicitlyWait(2000);

    this.profileName = this.driver.findElement({
      css: '#app > div > div:nth-child(1) > div > div > div:nth-child(1) > div:nth-child(2) > div:nth-child(2) > div:nth-child(1)'
    });

    this.profileMenu = this.driver.findElement({
      css: '#app > div > div:nth-child(1) > div > div > div:nth-child(1) > div:nth-child(2) > div:nth-child(3) > button'
    });

    this.profileOrg = this.driver.findElement({
      css: 'div.secondaryText'
    });
  }

  getName() {
    this.profileName.getText();
  }

  clickMenu() {
    this.profileMenu.click();
  }
}

module.exports = HeaderProfile;

使用此功能,运行代码时出现以下错误:

1) Webdriver - PC6 - Admin Tests - User Alice would like to verify they have global access:
     TypeError: Cannot read property 'then' of undefined
      at assertIsAboutPromise (node_modules/chai-as-promised/lib/chai-as-promised.js:47:38)
      at null.<anonymous> (node_modules/chai-as-promised/lib/chai-as-promised.js:65:17)
      at addProperty (node_modules/chai/lib/chai/utils/addProperty.js:42:29)
      at Context.<anonymous> (test/admin/dashboard/adminUser/pdvi-2377.js:51:48)

2) Webdriver - PC6 - Admin Tests - User Alice "after all" hook:
   Uncaught Error: ECONNREFUSED connect ECONNREFUSED 127.0.0.1:49665
    at ClientRequest.<anonymous> (node_modules/selenium-webdriver/http/index.js:174:16)
    at Socket.socketErrorListener (_http_client.js:265:9)
    at emitErrorNT (net.js:1253:8)
From: Task: WebDriver.manage().timeouts().implicitlyWait(2000)
    at webdriver.WebDriver.schedule (node_modules/selenium-webdriver/lib/webdriver/webdriver.js:362:15)
    at webdriver.WebDriver.Timeouts.implicitlyWait (node_modules/selenium-webdriver/lib/webdriver/webdriver.js:1352:23)
    at Context.<anonymous> (test/admin/dashboard/adminUser/pdvi-2377.js:57:39)

我想过不使用chai-as-promised并且使用普通的chai期望通过将我的上一个描述语句改为:

来帮助解决我的问题
describe('would like to', function() {
  it('verify they have global access', function() {
    this.driver.manage().timeouts().implicitlyWait(500);

    var headerProfile = new HeaderProfile(this.driver);

    return expect(headerProfile.getName()).to.equal(user);
  });
});

但会产生此错误:

1) Webdriver - PC6 - Admin Tests - User Alice would like to verify they have global access:
   AssertionError: expected undefined to equal 'John Doe'
    at Context.<anonymous> (test/admin/dashboard/adminUser/pdvi-2377.js:51:49)

2) Webdriver - PC6 - Admin Tests - User Alice "after all" hook:
   Uncaught Error: ECONNREFUSED connect ECONNREFUSED 127.0.0.1:57292
    at ClientRequest.<anonymous> (node_modules/selenium-webdriver/http/index.js:174:16)
    at Socket.socketErrorListener (_http_client.js:265:9)
    at emitErrorNT (net.js:1253:8)
From: Task: WebDriver.manage().timeouts().implicitlyWait(2000)
    at webdriver.WebDriver.schedule (node_modules/selenium-webdriver/lib/webdriver/webdriver.js:362:15)
    at webdriver.WebDriver.Timeouts.implicitlyWait (node_modules/selenium-webdriver/lib/webdriver/webdriver.js:1352:23)
    at Context.<anonymous> (test/admin/dashboard/adminUser/pdvi-2377.js:57:39)

但是,如果我只是将选择器放入我的describe语句中,如下所示:

describe('would like to', function() {
  it('verify they have global access', function() {
    this.driver.manage().timeouts().implicitlyWait(500);

    var userName = this.driver.findElement({
      css: '#app > div > div:nth-child(1) > div > div > div:nth-child(1) > div:nth-child(2) > div:nth-child(2) > div:nth-child(1)'
    }).getText();

    return expect(userName).to.eventually.equal(user);
  });
});

我的代码能够成功运行,所有测试都没有错误传递。

这让我相信我可能无法正确加载我的页面对象,或者selenium-webdriver可能存在一些时间问题?我的方法显然将select中的文本返回为undefined

如果我能够在我的it语句中直接使用选择器,这是不是意味着我在headerProfile / index.js页面对象中没有正确使用我的选择器?如果它们未定义,那么问题应该在页面对象中的某个位置正确吗?

我不太确定这个问题是selenium-webdriver还是vanilla javascript问题。

我使用页面对象的最初目的是为选择器提供一个中心位置,而不必更改每个测试文件中的选择器,只需更改中心位置的选择器。也许有更好的方法来实现这个目标?

有什么建议吗?

1 个答案:

答案 0 :(得分:1)

问题可能是PageObjects中的函数没有返回任何内容。默认返回值为“undefined”。

你试过了吗?

getName() {
    return this.profileName.getText();
}

这应该从具有.then方法的getText()返回promise。希望清除“无法读取属性”然后“未定义”错误。