使用 rspec 的 Selenium 屏幕截图
我正在尝试使用 selenium-client 和 rspec 捕获测试失败的屏幕截图。我运行此命令:
$ spec my_spec.rb \
--require 'rubygems,selenium/rspec/reporting/selenium_test_report_formatter' \
--format=Selenium::RSpec::SeleniumTestReportFormatter:./report.html
当一切都通过时,它会正确创建报告,因为不需要屏幕截图。但是,当测试失败时,我收到此消息,并且报告有空白屏幕截图:
WARNING: Could not capture HTML snapshot: execution expired
WARNING: Could not capture page screenshot: execution expired
WARNING: Could not capture system screenshot: execution expired
Problem while capturing system stateexecution expired
是什么导致了此“执行已过期”错误?我的规范中是否遗漏了一些重要的内容?这是 my_spec.rb 的代码:
require 'rubygems'
gem "rspec", "=1.2.8"
gem "selenium-client"
require "selenium/client"
require "selenium/rspec/spec_helper"
describe "Databases" do
attr_reader :selenium_driver
alias :page :selenium_driver
before(:all) do
@selenium_driver = Selenium::Client::Driver.new \
:host => "192.168.0.10",
:port => 4444,
:browser => "*firefox",
:url => "http://192.168.0.11/",
:timeout_in_seconds => 10
end
before(:each) do
@selenium_driver.start_new_browser_session
end
# The system capture need to happen BEFORE closing the Selenium session
append_after(:each) do
@selenium_driver.close_current_browser_session
end
it "backed up" do
page.open "/SQLDBDetails.aspx"
page.click "btnBackup", :wait_for => :page
page.text?("Pending Backup").should be_true
end
end
I am trying to capture screenshots on test failure using selenium-client and rspec. I run this command:
$ spec my_spec.rb \
--require 'rubygems,selenium/rspec/reporting/selenium_test_report_formatter' \
--format=Selenium::RSpec::SeleniumTestReportFormatter:./report.html
It creates the report correctly when everything passes, since no screenshots are required. However, when the test fails, I get this message, and the report has blank screenshots:
WARNING: Could not capture HTML snapshot: execution expired
WARNING: Could not capture page screenshot: execution expired
WARNING: Could not capture system screenshot: execution expired
Problem while capturing system stateexecution expired
What is causing this 'execution expired' error? Am I missing something important in my spec? Here is the code for my_spec.rb:
require 'rubygems'
gem "rspec", "=1.2.8"
gem "selenium-client"
require "selenium/client"
require "selenium/rspec/spec_helper"
describe "Databases" do
attr_reader :selenium_driver
alias :page :selenium_driver
before(:all) do
@selenium_driver = Selenium::Client::Driver.new \
:host => "192.168.0.10",
:port => 4444,
:browser => "*firefox",
:url => "http://192.168.0.11/",
:timeout_in_seconds => 10
end
before(:each) do
@selenium_driver.start_new_browser_session
end
# The system capture need to happen BEFORE closing the Selenium session
append_after(:each) do
@selenium_driver.close_current_browser_session
end
it "backed up" do
page.open "/SQLDBDetails.aspx"
page.click "btnBackup", :wait_for => :page
page.text?("Pending Backup").should be_true
end
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我遇到了这个问题,并通过为驱动程序设置超时来解决它。
这可能会导致驱动程序在遇到 :after_each 之前结束浏览器会话
你用了 10 秒,我运行得很好:timeout_in_seconds => 2000年
I ran into that issue and was able to solve it by setting up the timeout for the driver.
This can cause the Driver to end the Browser session before running into :after_each
You are using 10 seconds, i am running fine with :timeout_in_seconds => 2000
为了获得错误工作的屏幕截图,我必须稍微修改一下。
我将以下代码移出spec_helper(我在C:\Ruby\lib\ruby\gems\selenium-client-1.2.18\lib\selenium\rspec\spec_helper.rb中找到):
并将其放入append_after( :每个)执行我的测试设置/拆卸部分(在 @selenium_driver.close_current_browser_session 行之前)。
希望这有帮助!
In order to get screenshots on error working I had to mod things a bit.
I moved the following code out of spec_helper (which I found in C:\Ruby\lib\ruby\gems\selenium-client-1.2.18\lib\selenium\rspec\spec_helper.rb):
and placed it into the append_after(:each) do section of my test's setup/teardown (before the line @selenium_driver.close_current_browser_session).
Hope this helps!
为什么不在 after 函数中但在关闭浏览器之前截取屏幕截图?
Why not take the screenshot in the after function, but before you close the browser?
不确定这是否有帮助,https://github.com/mattheworiordan/capybara-screenshot,虽然它是针对 Capybara 而不是 Selenium
Not sure if this would help, https://github.com/mattheworiordan/capybara-screenshot, although it's for Capybara and not Selenium
看起来好像缺少一个
"
。it looks like missing a
"
there.