javascript - 页面对象方法?

标签 javascript testing automated-tests e2e-testing testcafe

我在设置我的页面对象时遇到问题。这可能是一个简单的语法问题,但我一直无法找到解决方案。我正在尝试做这样的事情:

测试:

test('Verify that a user can update their billing profile', async (t) => {
  await t
    .billingPage.enterBillingInformation('4111111111111111')
    .click(billingPage.saveButton)
    .expect(billingPage.successMessage.exists).ok();
});

页面:

import { Selector, t } from 'testcafe';

export default class billingPage {
  constructor() {
    this.cardNameInput = Selector('#cc_name');
    this.cardNumberInput = Selector('#credit-card-number');
    this.saveButton = Selector('button').withText('Save Card');
    this.successMessage = Selector('div').withText('Your billing information has been successfully updated.');
  }

  async enterBillingInformation(cardNum) {
    await t
      .typeText(this.cardNameInput, 'Foo Bar')
      .typeText(this.cardNumberInput, cardNum)
  }
}

当我在测试中拥有所有函数内容时,这是有效的,但我想用无效的凭据编写第二个测试,而且,obvs,我想重用代码(实际函数更长,有更多的字段)。但我无法弄清楚我做错了什么。

我收到这个错误:

TypeError: Cannot read property 'enterBillingInformation' of undefined

有关如何在页面对象中使用方法的文档中的示例将非常有帮助!该页面似乎显示了如何设置该功能,但没有相应的代码片段来显示如何在测试中实际使用它。

http://devexpress.github.io/testcafe/documentation/test-api/test-code-structure.html#test-controller

最佳答案

“t”对象在 billingPage 类中是未知的。您需要将它从父测试传递给 enterBillingInformation 函数。完整代码如下:

index.js

import { Selector, ClientFunction } from 'testcafe';
import BillingPage from './billingPage';

const billingPage = new BillingPage();

fixture `Getting Started`
    .page `Your page`;

test('Verify that a user can update their billing profile', async t => {
    await billingPage.enterBillingInformation("4111111111111111", t);  

    await t
        .click(billingPage.saveButton)
        .expect(billingPage.successMessage.exists).ok();  
});

billingPage.js

import { Selector } from 'testcafe';

export default class BillingPage {
    constructor () {
        this.cardNameInput = Selector('#cc_name');
        this.cardNumberInput = Selector('#credit-card-number');
        this.saveButton = Selector('button').withText('Save Card');
        this.successMessage = Selector('div').withText('Your billing information has been successfully updated.');  
    }

    async enterBillingInformation(cardNum, t) {
        await t
          .typeText(this.cardNameInput, 'Foo Bar')
          .typeText(this.cardNumberInput, cardNum)
    }
}

您可以了解有关 TestCafe 页面模型的更多信息 here .

关于javascript - 页面对象方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52212863/

相关文章:

java - 当我们只有元素的标题时,如何获取某个列表元素的属性?

testing - 如何在 gnu-make 的配方中两次重置 SHELL 变量?

javascript - jQuery : zoom in web page element

php - 在 lumen 5.5 中测试文件上传

.net - 从某个数据库读取数据时,如何编写 NUNit 测试用例?

testing - 是否有实现持续改进(也称为软件强化)流程的正确方法?

javascript - 显示在移动浏览器中打印页面时出现问题,移动浏览器不支持使用 reactjs 的 window.print

javascript - sails.js node.js 在 Controller 上解析 JSON

javascript - 将提示输入保存到数组中并从中读取

android - 在android中使用不同的类进行测试和正常运行