0

我在 Intern 上为 Web 应用程序编写功能测试。我有一个文件,其中描述了测试中的所有操作,并且还有一个测试,其中调用了这些操作

例如:

有一个 Action.ts 文件

在其中按顺序调用测试中的函数

//1 
//open the registration window
openRegistration(): Command<void> {
    return Action.openRegistration(this.parent);
}

static openRegistration(command: Command<any>): Command<void> {
    return command
        // click on the authorization menu
        .setPageLoadTimeout (10000)
        .get(intern.args.url)
        .end()
}

//2
inputTextByCssSelector(selector: string, value: string): Command <void> {
    return Input.inputTextByCssSelector(this.parent, selector, value);
}

static inputTextByCssSelector(
    command: Command<any>,
    selector: string, 
    value: string
): Command<void> {
    return command
        .setFindTimeout(10000)
        .findByCssSelector(selector)
        .click()
        .type(value)
        .end()
        .end()
}

像这样

.then(() => action.openRegistration())
.then(() => input.inputTextByCssSelector(
    "input [name = userName]", 
    intern.args.username
))
.then(() => input.inputTextByCssSelector(
    "input [name = password]", 
    intern.args.password
))

但是当我运行测试时,它会下降。

如果我在 openRegistration 结束时设置显式延迟,例如这样

openRegistration(): Command<void> {
    return Action.openRegistration(this.parent);
}

static openRegistration(command: Command<any>): Command<void> {
    return command
        .setPageLoadTimeout(10000)
        .get(intern.args.url)
        .sleep(7000)
        .end()
}

然后一切正常

为什么不工作setFindTimeout(10000)inputTextByCssSelectorsleep(7000)openRegistration工作

4

2 回答 2

0

当我第一次开始学习如何使用实习生并遇到一些与您所遇到的类似的问题时,我尝试了类似的事情(只是不使用 TypeScript)。

对我来说,问题是Promise在执行测试时链条没有得到正确维护。您应该尝试对代码进行如下细微更改,以增加Promise链的一致性。

例如,在我们开始之前,您的测试脚本是这样的:

return this.remote
    .then(() => action.openRegistration())
    .then(() => input.inputTextByCssSelector("input[name = userName]", intern.args.username))
    .then(() => input.inputTextByCssSelector("input[name = password]", intern.args.password))

您需要做的第一件事是删除那些箭头功能。我在使用箭头函数时遇到了几个问题,即this.remote当我这样做时,leadfoot/Session 在方法之间没有一致地传递。

所以你的第一.then()条语句调用了一个名为 的方法openRegistration(),对吧?编辑您的方法,改为返回 afunction执行您正在寻找的步骤:

static openRegistration(): Command<void> {
    return function () {
        return this.parent
            .setPageLoadTimeout (10000)
            .get(intern.args.url)
            .end()
   };
}

所以现在你的测试脚本看起来像这样(假设你对你调用的所有方法都重复这个模式):

return this.remote
    .then(action.openRegistration())
    .then(input.inputTextByCssSelector("input[name = userName]", intern.args.username))
    .then(input.inputTextByCssSelector("input[name = password]", intern.args.password))

这应该可以解决您的问题。

于 2017-11-21T14:27:29.797 回答
0

你说的“它掉下来”是什么意思?测试是否引发超时错误?

一个潜在的问题是组件的可见性。在页面加载和您尝试与之交互的元素变得可见之间是否有一些延迟(例如,JS 淡入动画)?命令返回第findBy一个找到的元素,但该元素可能不可见。如果它不可见,则 Intern 无法与之交互,并且类似的命令type将失败。要等到元素可见,请使用findDisplayedByCssSelector.

请注意,间距在 CSS 选择器中很重要。选择器"input [name = userName]"实际上是在寻找具有name=userName包含在元素中的属性的input元素。假设实际意图是选择具有特定名称属性的输入,则应将其格式化为'input[name="userName"]'.

另请注意,end()命令仅在find命令之后需要,并且通常不需要在辅助命令中的命令链末尾(从 开始this.parent)。因此,例如,在inend之后不需要一个,最多需要一个 in (对于命令)。getopenRegistrationendinputTextByCssSelectorfindByCssSelector

于 2017-09-27T01:50:28.317 回答