【问题标题】:The function setFindTimeout does not work for me函数 setFindTimeout 对我不起作用
【发布时间】:2017-09-25 16:05:56
【问题描述】:

我在 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)inputTextByCssSelector 中不起作用,而sleep(7000)openRegistration 中起作用

【问题讨论】:

    标签: javascript intern leadfoot


    【解决方案1】:

    你所说的“它会掉下来”是什么意思?测试是否抛出超时错误?

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

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

    还要注意end() 命令仅在find 命令之后才需要,并且通常不需要在辅助命令中的命令链末尾(从this.parent 开始)。因此,例如,在openRegistration 中的get 之后不需要end,在inputTextByCssSelector 中最多需要一个end(用于findByCssSelector 命令)。

    【讨论】:

      【解决方案2】:

      当我第一次开始学习如何使用实习生并遇到一些与您遇到的类似的问题时,我尝试了类似的方法(只是不使用 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.remoteleadfoot/Session 在方法之间没有一致地传递。

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

      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))
      

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-12-08
        • 2020-11-11
        • 2013-02-13
        相关资源
        最近更新 更多