在 rails 中使用无头 chrome 进行测试时出现 ElementNotVisibleError
ElementNotVisibleError when using headless chrome for testing in rails
我用 capybara/headless_chrome 测试以下假设 And I click on the text "2018/2019" within ".year"
并不断得到错误
element not visible
(Session info: headless chrome=67.0.3396.87)
(Driver info: chromedriver=2.40.565386 (45a059dc425e08165f9a10324bd1380cc13ca363),platform=Mac OS X 10.13.5 x86_64) (Selenium::WebDriver::Error::ElementNotVisibleError)
我已经按照建议 here 尝试调整 window 尺寸。但它没有用。
我的步骤定义:
When(/^(?:|I )click on the text "([^"]*)"(?: within "([^"]*)")?$/) do |link, selector|
begin
page.find(:css, selector).click
end
end
该元素实际上是可见的并且被 Capybara 找到了
[1] pry(#<Cucumber::Rails::World>)> page.find(:css, ".year")
=> #<Capybara::Node::Element tag="a" path="/html/body/div[2]/div[2]/section/div[2]/div/div[1]/div[1]/div[2]/div/div[2]/ul/li/a">
但是点击失败
[2] pry(#<Cucumber::Rails::World>)> page.find(:css, ".year").click
Selenium::WebDriver::Error::ElementNotVisibleError: element not visible
为什么 click
在这里不起作用?
编辑:
link 的 HAML 是
%ul.facet_values
- unselected.each do |facet_value|
%li.filtered{data: {hayf: {text: facet_value.name.downcase}}}
= link_to facet_value.path, title: facet_value.name, class: 'year' do
=truncate("#{facet_value.name}", length: 24)
- if facet.has_counts?
%span.small="(#{facet_value.count})"
我尝试使用 Poltergeist
进行无头测试并得到 Poltergeist detected another element with CSS selector 'html body header div.container-fluid' at this position
并通过 .trigger('click')
解决并且测试通过。
由于这在非无头 Chrome 或 Poltergeist 中不起作用,因此最简单的答案是该元素实际上不可见。首先删除 Capybara.ignore_hidden_elements = false
设置,因为这在测试应用程序时毫无意义,并且会搞砸各种等待行为。其次,在尝试单击 link 之前,使用 save_and_open_screenshot
获取页面实际外观的图片。
我猜您会发现 link 此时实际上并不可见,并且您的测试缺少用户必须执行的步骤才能使 link 可见, 它是否在您必须首先将鼠标悬停在不同元素上的菜单中?它是在弹出窗口中,您必须先单击其他内容吗?是否需要滚动到?等等
最后,如果您坚持使用 Chrome 通过 Selenium,通常问题会更少。 Poltergeist 使用的 PhantomJS 已经有一段时间没有更新了,并且不支持现代 JS 或 CSS 这会导致各种奇怪的事情。
我用 capybara/headless_chrome 测试以下假设 And I click on the text "2018/2019" within ".year"
并不断得到错误
element not visible
(Session info: headless chrome=67.0.3396.87)
(Driver info: chromedriver=2.40.565386 (45a059dc425e08165f9a10324bd1380cc13ca363),platform=Mac OS X 10.13.5 x86_64) (Selenium::WebDriver::Error::ElementNotVisibleError)
我已经按照建议 here 尝试调整 window 尺寸。但它没有用。 我的步骤定义:
When(/^(?:|I )click on the text "([^"]*)"(?: within "([^"]*)")?$/) do |link, selector|
begin
page.find(:css, selector).click
end
end
该元素实际上是可见的并且被 Capybara 找到了
[1] pry(#<Cucumber::Rails::World>)> page.find(:css, ".year")
=> #<Capybara::Node::Element tag="a" path="/html/body/div[2]/div[2]/section/div[2]/div/div[1]/div[1]/div[2]/div/div[2]/ul/li/a">
但是点击失败
[2] pry(#<Cucumber::Rails::World>)> page.find(:css, ".year").click
Selenium::WebDriver::Error::ElementNotVisibleError: element not visible
为什么 click
在这里不起作用?
编辑: link 的 HAML 是
%ul.facet_values
- unselected.each do |facet_value|
%li.filtered{data: {hayf: {text: facet_value.name.downcase}}}
= link_to facet_value.path, title: facet_value.name, class: 'year' do
=truncate("#{facet_value.name}", length: 24)
- if facet.has_counts?
%span.small="(#{facet_value.count})"
我尝试使用 Poltergeist
进行无头测试并得到 Poltergeist detected another element with CSS selector 'html body header div.container-fluid' at this position
并通过 .trigger('click')
解决并且测试通过。
由于这在非无头 Chrome 或 Poltergeist 中不起作用,因此最简单的答案是该元素实际上不可见。首先删除 Capybara.ignore_hidden_elements = false
设置,因为这在测试应用程序时毫无意义,并且会搞砸各种等待行为。其次,在尝试单击 link 之前,使用 save_and_open_screenshot
获取页面实际外观的图片。
我猜您会发现 link 此时实际上并不可见,并且您的测试缺少用户必须执行的步骤才能使 link 可见, 它是否在您必须首先将鼠标悬停在不同元素上的菜单中?它是在弹出窗口中,您必须先单击其他内容吗?是否需要滚动到?等等
最后,如果您坚持使用 Chrome 通过 Selenium,通常问题会更少。 Poltergeist 使用的 PhantomJS 已经有一段时间没有更新了,并且不支持现代 JS 或 CSS 这会导致各种奇怪的事情。