【发布时间】:2014-09-23 15:00:23
【问题描述】:
我正在尝试使用量角器测试我网站上的登录页面。
如果您登录不正确,该站点会显示一条“toast”消息,该消息会弹出 5 秒钟,然后消失(使用 $timeout)。
我正在使用以下测试:
describe('[login]', ()->
it('should show a toast with an error if the password is wrong', ()->
username = element(select.model("user.username"))
password = element(select.model("user.password"))
loginButton = $('button[type=\'submit\']')
toast = $('.toaster')
# Verify that the toast isn't visible yet
expect(toast.isDisplayed()).toBe(false)
username.sendKeys("admin")
password.sendKeys("wrongpassword")
loginButton.click().then(()->
# Verify that toast appears and contains an error
toastMessage = $('.toast-message')
expect(toast.isDisplayed()).toBe(true)
expect(toastMessage.getText()).toBe("Invalid password")
)
)
)
相关标记(玉)如下:
.toaster(ng-show="messages.length")
.toast-message(ng-repeat="message in messages") {{message.body}}
问题是toastMessage 测试失败(它找不到元素)。它似乎在等待 toast 消失并然后运行测试。
我也尝试将toastMessage 测试放在then() 回调之外(我认为这几乎是多余的),但我得到了完全相同的行为。
我最好的猜测是量角器看到有一个 $timeout 正在运行,并在运行下一个测试之前等待它完成(参考 protractor control flow)。我将如何解决这个问题并确保测试在 超时期间运行?
更新:
按照下面的建议,我使用browser.wait() 等待 toast 可见,然后在 promise 解决后尝试运行测试。没用。
console.log "clicking button"
loginButton.click()
browser.wait((()-> toast.isDisplayed()),20000, "never visible").then(()->
console.log "looking for message"
toastMessage = $('.toaster')
expect(toastMessage.getText()).toBe("Invalid password")
)
console.log 语句让我看看发生了什么。这是一系列事件,[] 是我在浏览器中看到的。
clicking button
[toast appears]
[5 sec pass]
[toast disappears]
looking for message
[test fails]
为了更清楚地了解烤面包机的情况:我有一个服务,它基本上包含一系列消息。 toast 指令始终在页面上(模板是上面的玉),并监视 toast 服务中的消息。如果有新消息,它会运行以下代码:
scope.messages.push(newMessage)
# set a timeout to remove it afterwards.
$timeout(
()->
scope.messages.splice(0,1)
,
5000
)
这会将消息推送到作用域上的消息数组中 5 秒,这就是让 toast 出现的原因(通过ng-show="messages.length")。
为什么量角器要等 toast 的 $timeout 过期才能继续测试?
【问题讨论】:
标签: angularjs timeout protractor