0

我使用 Hound 作为 webdriver 框架来利用 Elixir 中的 Selenium。我正在测试 Facebook 帐户创建。在我填写了一个否定的测试(名字 = Roberto,姓氏 = 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```
4

1 回答 1

0

使用search_element而不是find_element. search_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"))
于 2020-01-23T00:02:22.737 回答