How to assert an element is present after a click in Hound?

332 Views Asked by At

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```
1

There are 1 best solutions below

2
On

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"))