Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

find_element with :link_text does not work with phantomjs #116

Open
thiagogabriel opened this issue Jun 28, 2016 · 9 comments
Open

find_element with :link_text does not work with phantomjs #116

thiagogabriel opened this issue Jun 28, 2016 · 9 comments

Comments

@thiagogabriel
Copy link

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

@danhper
Copy link
Collaborator

danhper commented Jun 29, 2016

@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?

@thiagogabriel
Copy link
Author

@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
Copy link
Author

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.

@danhper
Copy link
Collaborator

danhper commented Jun 29, 2016

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

@thiagogabriel
Copy link
Author

@tuvistavie is this a good solution for Elixir 1.2? thiagogabriel/phoenix_hound_issue_116@00a19bd

@danhper
Copy link
Collaborator

danhper commented Jun 29, 2016

This solution seems perfect to me 👍

@HashNuke
Copy link
Owner

HashNuke commented Jun 30, 2016

@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 HashNuke reopened this Jun 30, 2016
@HashNuke
Copy link
Owner

HashNuke commented Jul 2, 2016

@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.

@miskolc
Copy link

miskolc commented Aug 19, 2016

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants