如何在 Hound 中单击后断言元素存在?



我正在使用Hound作为Webdriver框架来利用Elixir中的Selenium。我正在测试Facebook帐户的创建。在我填写阴性测试后(名字 = 罗伯托,姓氏 = asdlkfj;(我单击提交,我试图收到一个错误,因为没有正确的姓氏。问题是 Hound 没有等待元素显示并返回未找到元素错误。我如何处理这个问题并让测试等到元素加载完毕?这是我的代码:

test "Last name with random characters" do
counter = :rand.uniform(100)
navigate_to "https://www.facebook.com"
first_name = find_element(:name, "firstname")
fill_field(first_name, "Jorge")
last_name = find_element(:name, "lastname")
fill_field(last_name, "asdfja;lsdf")
email_input = "robbie#{counter}@gmail.com"
email = find_element(:name, "reg_email__")
fill_field(email, email_input)
confirm_email = find_element(:name, "reg_email_confirmation__")
fill_field(confirm_email, email_input)
password_input = "123456Test"
password = find_element(:name, "reg_passwd__")
fill_field(password, password_input)
#Birthday
birth_month = find_element(:css, "#month > option:nth-child(5)")
birth_day = find_element(:css, "#day > option:nth-child(25)")
birth_year = find_element(:css, "#year > option:nth-child(22)")
click(birth_month)
click(birth_day)
click(birth_year)
#gender
select_gender = find_element(:css, "#u_0_s > span:nth-child(2)")
click(select_gender)
sign_up_button = find_element(:name, "websubmit")
click(sign_up_button)
search = find_element(:id, "#reg_error_inner")
# found = element_displayed?("#reg_error_inner")
IO.puts(search)
# assert found == true
# :timer.sleep(10000)
end```

使用search_element而不是find_elementsearch_element将在成功时返回{:ok, element},在失败时返回{:error, error}。如果您只想断言该元素存在,则可以:

assert {:ok, _} = search_element(:id, "#reg_error_inner")

如果您还想将其放在变量中以供进一步处理,则:

assert {:ok, element} = search_element(:id, "#reg_error_inner")

如果要将其转换为布尔值,则:

match?({:ok, _}, search_element(:id, "#reg_error_inner"))

相关内容

  • 没有找到相关文章

最新更新