代码之家  ›  专栏  ›  技术社区  ›  Dende

使用无头铬在钢轨中进行测试时出现不可见错误

  •  0
  • Dende  · 技术社区  · 7 年前

    我根据假设进行测试 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 . 但没用。 我的步骤定义:

    When(/^(?:|I )click on the text "([^"]*)"(?: within "([^"]*)")?$/) do |link, selector|
      begin
        page.find(:css, selector).click
      end
    end
    

    这种元素实际上是可见的,由水豚发现。

    enter image description here

    [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 在这里工作?

    编辑: 链接的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') 测试通过了。

    1 回复  |  直到 7 年前
        1
  •  0
  •   Thomas Walpole    7 年前

    因为这在无头的Chrome或Poltergeist中不起作用,所以最简单的答案是元素实际上不可见。首先移除 Capybara.ignore_hidden_elements = false 设置,因为这在测试应用程序时没有意义,并且会导致各种等待行为。二次利用 save_and_open_screenshot 在尝试单击链接之前,获取页面实际外观的图片。

    我猜你会看到链接在那一点上实际上是不可见的,你的测试缺少了一个让链接可见的步骤,是不是在一个菜单中,你必须先将鼠标悬停在一个不同的元素上?是不是在一个弹出窗口中,你必须先点击其他东西?,是否需要滚动到?等。

    最后,如果你坚持通过硒来使用铬,你通常会有更少的问题。poltergeist使用的phantomjs在很长一段时间内没有更新,也不支持现代js或css,这可能会导致各种各样的奇怪。