首页 > 解决方案 > 无法使用 WebdriverIO 和 Mocha 执行页面对象模式

问题描述

我正在尝试在我的测试中使用页面对象模式。我真的遇到了很多问题,例如使用 Mocha 我不应该在做断言时“最终”使用,但是没有它我的测试会失败。难道我做错了什么?当我在做任何控制台日志时,我得到的信息是我的承诺是 {state: pending}

这是我的第一个测试:test1.spec.js:

import chai from 'chai';
import cartPage from '../pageobjects/cart.page';
import regeneratorRuntime from "regenerator-runtime";

const chaiAsPromised = require("chai-as-promised");
const { expect } = chai;
const { assert } = chai;

chai.use(chaiAsPromised);
chai.should();


describe('Test1 - All elements exist on the Cart Page', function () {
  before(function () {
    cartPage.open();
  });

  it('should validate if the Add button exists', function (done) {
    expect(cartPage.addButtonText.getText()).eventually.equal('Add').notify(done);
  });

  it('should validate if the Remove button exists', function (done) {
         expect(cartPage.removeButtonText.getText()).eventually.equal('Remove').notify(done);
  });
});

我的 cart.page.js 看起来是这样的:

import Page from './page';

class CartPage extends Page {

  get addButtonText() { return browser.element('.button'); }
  // getAddTextButton() { return this.addButtonText.getText(); }

  get removeButtonText() { return browser.element('.remove_button'); }

  open() {
    super.open('/shopping-cart');
    browser.pause(5000);
  }
}

export default new CartPage();

我正在尝试编写第二个测试:

import chai from 'chai';
import cartPage from '../pageobjects/cart.page';
import regeneratorRuntime from "regenerator-runtime";

const chaiAsPromised = require("chai-as-promised");
const { expect } = chai;
const { assert } = chai;

chai.use(chaiAsPromised);
chai.should();


describe('Test2 - All elements are clickable on the Cart Page', function () {
  before(function () {
    cartPage.open();
  });

  it('should validate if the Add button is clickable', function (done) {
cartPage.addButtonText.click();

我看不到按钮被点击。我在尝试 console.log 时得到 {status:pending}。我无法在文本字段中设置任何值。我正在使用 wdio 运行测试,尝试使用 sync: true、false 或将其注释掉,但我看不出有任何区别

我不确定我的方法是否可行。我已经尝试了许多使用页面对象模式的存储库,但每次都失败了

标签: javascriptseleniumselenium-webdriverautomated-testswebdriver-io

解决方案


推荐阅读