0

I'm using Hound as webdriver framework for leveraging Selenium in Elixir. I'm testing facebook account creation. After I fill out a negative test (firstname = Roberto, lastname = asdlkfj;)I click submit and I am trying to get an error for not having a correct last name. The problem is Hound isn't waiting for the element to be displayed and returns an element not found error. How can I handle this and have the test wait until the element is loaded? Here's my code:

  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```
Jackson
  • 51
  • 9
  • 1
    https://stackoverflow.com/questions/37106480/elixir-hound-wait-for-page-to-load does this help? – CEH Jan 22 '20 at 22:11

1 Answers1

0

Use search_element instead of find_element. search_element will return {:ok, element} on success and {:error, error} on failure. If you just want to assert the element exists, then you can:

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

If you want to also have it in a variable for further processing, then:

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

If you want to convert it to a boolean, then:

match?({:ok, _}, search_element(:id, "#reg_error_inner"))
Paweł Obrok
  • 22,568
  • 8
  • 74
  • 70
  • I tried this but got an error. `1) test Password too short (ElixirSdetExerciseTest) test/elixir_sdet_exercise_test.exs:144 match (=) failed code: assert {:ok, _} = search_element(:id, "#reg_error") right: {:error, :no_such_element} stacktrace: test/elixir_sdet_exercise_test.exs:172: (test)` Same kind of testing criteria applies – Jackson Jan 23 '20 at 21:39
  • That means the element is not present, which I thought you wanted to assert? If you just want a boolean, then see the updated answer. – Paweł Obrok Jan 23 '20 at 22:44