【问题标题】:Hound ExUnit: assert_raise seems to considerably slow down the test suiteHound ExUnit:assert_raise 似乎大大减慢了测试套件的速度
【发布时间】:2018-02-04 23:02:36
【问题描述】:

我为 phoenix 应用程序添加了以下测试:

navigate_to("/")

registration_page_link = find_element(:class, "header__button__register")
registration_page_link |> click()

form                   = find_element(:class, "form__register")
email_field            = find_within_element(form, :class, "input--email")
first_name_field       = find_within_element(form, :class, "input--first_name")
last_name_field        = find_within_element(form, :class, "input--last_name")
password_field         = find_within_element(form, :class, "input--password")
confirm_password_field = find_within_element(form, :class, "input--confirm_password")
submit_button          = find_within_element(form, :class, "form__submit")

email_field            |> fill_field("john.doe@email.com")
first_name_field       |> fill_field("John")
last_name_field        |> fill_field("Doe")
password_field         |> fill_field("12345678")
confirm_password_field |> fill_field("12345678")
submit_button          |> submit_element()

alert      = find_element(:class, "alert--success")
alert_text = visible_text(alert)
name       = find_element(:class, "header__user_name")
name_text  = visible_text(name)

assert alert_text  == "Congratulations John! You have successfuly registered."
assert name_text   == "John D."
assert_raise Hound.NoSuchElementError, ~r/No element found for class/, fn ->
  find_element(:class, "header__button__register")
end

它测试用户是否可以使用有效凭据进行注册。
如果我注释掉最后一个断言,测试需要 0.7 秒才能运行。
如果我单独使用最后一个断言,测试需要 6.1 秒才能运行。

为什么这个断言运行得这么慢?有没有更好的方法来测试一个元素不再出现在页面上?

【问题讨论】:

  • 如果您将0 作为第三个参数传递:find_element(:class, "header__button__register", 0)

标签: elixir phoenix-framework ex-unit


【解决方案1】:

find_element 的第三个参数是重试次数,默认为 5,是 Hound 尝试查找元素 with a gap of retry_time milliseconds which defaults to 250 的次数。由于页面已经被之前的find_element 调用加载,您可以通过将重试次数设置为0 来加快此断言:

assert_raise Hound.NoSuchElementError, ~r/No element found for class/, fn ->
  find_element(:class, "header__button__register", 0)
end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-06-23
    • 1970-01-01
    • 1970-01-01
    • 2019-06-11
    • 2020-03-04
    • 1970-01-01
    • 2015-06-14
    相关资源
    最近更新 更多