2

I'm using similar construction:

<div class="edit" style="visibility: hidden;">
  <a href="some_path" id="edit_item">Edit</a>
</div>

Then I hover mouse this element become visible, but I have difficult to interact this actions with tests(using cucumber, capybara, selenium).

I get an error

Element is not currently visible and so may not be interacted with (Selenium::WebDriver::Error::ElementNotDisplayedError)

I tried to use Element.trigger(event) with mouseover, but it doesn't work in selenium... How I can interact with this element?

2 Answers 2

4

I solved this problem using execute_script from capybara:

When /^I hover element "([^""]*)"(?: within "([^""]*)")?$/ do |id,selector|
  with_scope(selector) do
    page.execute_script("$('#{id}').mouseover();")
  end
end

When /^I click in hide element "([^""]*)"(?: within "([^""]*)")?$/ do |id,selector|
  with_scope(selector) do
    page.execute_script("$('#{id}').click();")
  end
end

but this solution doesn't work with css - display: none;

Sign up to request clarification or add additional context in comments.

1 Comment

Be aware that with_scope has no effect on execute_script.
0

For future reference, have in mind that execute_script can be called in elements, which provides better async guarantees:

When /^I hover element "([^""]*)"(?: within "([^""]*)")?$/ do |id,selector|
  element = within(selector) { find_by_id(id) }
  element.execute_script('this.mouseover()')
end

When /^I click in hide element "([^""]*)"(?: within "([^""]*)")?$/ do |id,selector|
  within(selector) { find_by_id(id) }.execute_script('this.click()')
end

Specifying visible: :all is another way to find hidden elements:

find('.edit', visible: :all).hover
click_link('Edit')

That said, interacting with hidden elements may hide real issues in the UI that won't allow a user to perform an interaction, so it's better to use it sparingly.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.