我已经使用 Capybara 编写了一套 TC。所有测试都成功运行,没有出现任何问题。驱动定义如下:
Capybara.default_driver = :selenium
但是当我尝试使用 Capybara Poltergeist 驱动程序运行相同的 TC 时 - 我面临的问题是 Poltergeist 无法找到某些元素。例如 Poltergeist 找不到这样的 xpath:
"//div[./ul[@class='pricing-plan list-unstyled']/li[contains(., 'Basic')]]//li/a[contains(., 'Get Started')]"
驱动定义如下:
require 'capybara/poltergeist'
Capybara.default_driver = :poltergeist
Capybara.javascript_driver = :poltergeist
Capybara.current_driver = :poltergeist
Capybara.register_driver :poltergeist do |app|
Capybara::Poltergeist::Driver.new(app,
:phantomjs => File.join("E:/Programming/phantomjs-2.1.1-windows/phantomjs-2.1.1-windows/phantomjs.exe"),
:js_errors => false,
:phantomjs_options => ['--ignore-ssl-errors=yes'],
:phantomjs_logger => File.open("F:/Programming/VLoop/Project/03.06.216/webapp/webapp/log/test_phantomjs.log", 'w+')
)
end
问题是为什么 Capybara 和 Poltergeist 驱动之间存在差异,以及如何处理 Poltergeist 驱动无法找到某些元素的问题?
最佳答案
尝试通过在注册时将 window_size 选项传递给驱动程序来调整 poltergeist/phantomjs 使用的窗口大小,使其大小足以显示您的页面
Capybara.register_driver :poltergeist do |app|
Capybara::Poltergeist::Driver.new(app, :phantomjs => File.join("E:/Programming/phantomjs-2.1.1-windows/phantomjs-2.1.1-windows/phantomjs.exe"),
:js_errors => false,
:window_size => [1300,2000], #set whatever size you need here
:phantomjs_options => ['--ignore-ssl-errors=yes'],
:phantomjs_logger => File.open("F:/Programming/VLoop/Project/03.06.216/webapp/webapp/log/test_phantomjs.log", 'w+')
)
end
使页面变大可能意味着本来会被隐藏的元素因为它们溢出了它们的包含元素可能会在更大的页面中可见。这不一定是您可以通过滚动页面来解决的问题(如果需要/可以,驱动程序会自动滚动页面),因为这取决于元素如何/为什么被隐藏,如果没有看到您的实际 HTML 和 CSS,没人知道.
关于ruby - 为什么 Poltergeist 驱动程序找不到 Capybara (Ruby) 成功找到的某些元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37639318/