1

我为 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 秒才能运行。

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

4

1 回答 1

2

find_element的第三个参数是重试次数,默认为5Hound 尝试查找间隔为retry_time毫秒的元素的次数,默认为 250。由于页面已经被先前的find_element调用加载,您可以通过将重试次数设置为来加快此断言0

assert_raise Hound.NoSuchElementError, ~r/No element found for class/, fn ->
  find_element(:class, "header__button__register", 0)
end
于 2018-02-04T15:02:36.990 回答