开发者

capybara, selenium interact with hidden or display: none css properties

开发者 https://www.devze.com 2023-04-13 03:28 出处:网络
I\'m using similar construction: <div class=\"edit\" style=\"visibility: hidden;\"> <a href=\"some_path\" id=\"edit_item\">Edit</a>

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号