【问题标题】:The right way to fill and test a web form in ScalaTest and FluentLenium在 ScalaTest 和 FluentLenium 中填写和测试 Web 表单的正确方法
【发布时间】:2015-06-26 09:53:03
【问题描述】:

我正在尝试使用 ScalaTest 和 FluentLenium 在 Play Framework 中填写、提交和测试 Web 表单。看起来应该很简单,但是我遇到了各种各样的问题。

首先,有问题的网络表单的一部分:

<form class="signin" id="loginform" method="POST" action="/login">
    <div class="form-group">
        <label for="name">Email Address:</label>
        <input type="email" class="form-control" placeholder="Enter Email Address" id="email" name="email"  required />
        ...

这在真正的网络浏览器上运行良好。现在,当我尝试填写并提交表单时,问题就来了:

@RunWith(classOf[JUnitRunner])
@SharedDriver(deleteCookies = false)
@SharedDriver(`type` = SharedDriver.SharedType.PER_CLASS)
class TestWebsiteAuthentication extends Specification {
    "Application" should {
        "login as an administrative user on the web site" in new WithBrowser with GPAuthenticationTestUtility {
            browser.goTo(loginURL)
            browser.fill("#email").`with`(prerequisiteAccounts.head.userIdentity) must equalTo(OK)
            ...

在最后一行,我得到一个异常:

[info] x 以管理用户身份登录网站 [错误] 'org.fluentlenium.core.action.FillConstructor@1c25c183' 不等于 '200' (TestWebsiteAuthentication.scala:93) [错误] 预期:200 [错误] 实际:org.fluentlenium.core.action.FillConstructor@1c25c183

有什么想法我在这里做错了吗?

我试过去掉“必须等于(OK)”,但这只会导致表单在提交时失败——不幸的是,我找不到任何关于如何做到这一点的文档,所以我'我基本上是一点一点地拼凑起来的。指向相关文档的指针将不胜感激 - Tyrpesafe 似乎没有任何完整的东西......只是让你开始的“预告片”,但没有深度。 :-(

【问题讨论】:

    标签: forms scala playframework-2.0 scalatest fluentlenium


    【解决方案1】:

    当您编写browser.fill("#email").``with``("x@y.com") 时,您真正要做的就是告诉Fluentlenium 编辑模板以在输入标记内添加值属性。 另一方面,OK 是一个 HTTP 状态码,所以比较它们自然会得出 false。

    当您说您尝试提交表单但失败时,我假设您执行了以下操作:

    browser.fill("#email").`with`("x@y.com")
    browser.fill("#password").`with`("myPass")
    browser.click("#button")   // this should submit the form and load the page after login
    

    然后尝试做出如下断言:

    browser.title() must equalTo("next page") // fails because "next page" != "login page"
    

    一个建议是尝试这样的事情,在browser.click之前:

    browser.pageSource() must contain("xyz") // this will fail
    

    当上述断言失败时,它会将 browser.pageSource() 的内容打印到您的终端,您将能够看到 Fill 函数对 HTML 所做的修改。

    就我而言,我观察到我的 pageSource() 现在包含以下内容:

    <input type="text" id="email" name="email" value="x@y.com"/>
    <input type="password" id="password" name="password"/>
    

    注意第一个输入有一个value="x@y.com",但第二个输入仍然是空的。结果第二个是空的,因为它是密码类型的输入,但是我最终使表单登录工作。

    以下是您可以查看的内容列表:

    • 在该规范中启用了数据库
    • 用用户填充它(以防您的表单验证连接到数据库)
    • 根据我的经验,在测试中多次使用browser.goTo 无法很好地提交表单(任何人都可以确认?)

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-20
      • 2020-06-04
      • 2014-02-16
      • 1970-01-01
      • 1970-01-01
      • 2016-03-25
      • 2013-05-04
      • 1970-01-01
      相关资源
      最近更新 更多