hound icon indicating copy to clipboard operation
hound copied to clipboard

find_element with :link_text does not work with phantomjs

Open thiagogabriel opened this issue 9 years ago • 9 comments

I'm trying to click on an element based on the link name but the element is not found even with find_element(:link_text, "Home"). I'm using Elixir 1.2.3, Phoenix 1.1.6 and PhantomJS 2.1.1.

When I replaced the driver with selenium, find_element(:link_text, ...) and click({:link_text, ...}) started working. Is this a known problem and is there a way to solve this?

My configurations are:

# mix.exs
  defp deps do
    [
     # ...
     {:hound, "~> 1.0", only: :test},
     # ...
    ]
  end
# config/test.exs
config :hound, driver: "phantomjs"

Running phantomjs --wd on a different tmux tab.

defmodule PhoenixApp.NewPostTest do
  use PhoenixApp.ConnCase

  # Import Hound helpers
  use Hound.Helpers

  # Start a Hound session
  hound_session

  test "GET /post/new" do
    # manual login omitted
    navigate_to "/posts/new"
    fill_field({:id, "post_message"}, "This is my first post!")
    submit_element({:id, "post_message"})

    click({:link_text, "All posts"})  # Not working using PhantomJS

    assert page_source =~ "This is my first post!"
  end
end

I'm running the tests using: mix test

thiagogabriel avatar Jun 28 '16 21:06 thiagogabriel

@thiagogabriel Hi, thanks for reporting. I was unable to reproduce your issue, so it is probably not just that link_text is not working. I created a repository to try to reproduce this:

https://github.com/tuvistavie/hound-issue-116

Could it be that the form is not properly submitted when using PhantomJS?

danhper avatar Jun 29 '16 00:06 danhper

@tuvistavie I'm digging deeper and found out that the problem happens on navbar from twitter bootstrap. I created a Phoenix application here where the last commit is the only one that makes a real difference. It works fine when I try to get a link outside navbar.

thiagogabriel avatar Jun 29 '16 02:06 thiagogabriel

I believe the menu get's collapsed when the page is opened on phantomjs. Is it possible to set a configuration on test.exs to define a bigger window, or the only solution is use something like Hound.Helpers.Window.html#maximize_window before very test?

Thanks.

thiagogabriel avatar Jun 29 '16 02:06 thiagogabriel

I don't think there is any way to set the window size from the configuration when using phantomjs. @HashNuke: Do you know if it is possible?

Otherwise, I just tried with maximize_window and it worked just fine, so you can just write a setup that will start the session and set the window_size for you, for example:

  def custom_hound_session(context \\ %{}) do
    Hound.start_session(Map.get(context, :hound_options, []))
    current_window_handle |> maximize_window
  end

  setup :custom_hound_session

Note that this example requires Elixir 1.3

danhper avatar Jun 29 '16 06:06 danhper

@tuvistavie is this a good solution for Elixir 1.2? https://github.com/thiagogabriel/phoenix_hound_issue_116/commit/00a19bd8f2da95ce063b10921e5d2fbfd52fde8f

thiagogabriel avatar Jun 29 '16 17:06 thiagogabriel

This solution seems perfect to me :+1:

danhper avatar Jun 29 '16 23:06 danhper

@tuvistavie I think we do allow passing custom options to browsers/drivers. But maximizing window after starting the browser is a better & and more generic way of doing it.

HashNuke avatar Jun 30 '16 00:06 HashNuke

@thiagogabriel Just checked the commit you linked to. current_window_handle should already be available when use Hound.Helpers is added. So it would suffice if current_window_handle is called without the module namespace.

HashNuke avatar Jul 02 '16 21:07 HashNuke

I have the same problem with links hidden in the navbar. Links outside the navbar can be found by their text, but even in that case I wasn't able to find links who had other elements inside them such as span( details bellow). @thiagogabriel 's solution above doesn't work for me :(

  home_link = find_element(:link_text, "Home")

 ** (Hound.NoSuchElementError) No element found for link_text 'Home'
 stacktrace:
   (hound) lib/hound/helpers/page.ex:51: Hound.Helpers.Page.find_element/3
   test/integration/navbar_links.exs:16: (test)

I've also tried set_window_size with no result

  Hound.Helpers.Window.current_window_handle
  |> Hound.Helpers.Window.set_window_size(2300, 1000)

It works however if I use the =~ operator

 assert page_source =~ "Home"

Or if I set an id on the link and find the element by that id

 <a href="<%= page_path(@conn, :index) %>" id="home">Home</a>

 home_link = find_element(:id, "home")

Regarding the case where I had <span>'s inside a's,

         <a class="subCategoryTitle"><span>Baby</span></a>

I'm also unable to find them by text, I tried using both the inner plain text of the link:

         home_link = find_element(:link_text, "Baby")

and the full html inside the link

         home_link = find_element(:link_text, "<span>Baby</span>")

Both can't find the elements, but If I get rid of the <span>'s and use home_link = find_element(:link_text, "Baby") it works. I think though there should be a way for find_element to work even if the link has inner HTML tags.

miskolc avatar Aug 19 '16 17:08 miskolc