capybara, selenium interact with hidden or display: none css properties
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?
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;
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.
精彩评论