问题
I use cucumber framework for testing web application, as i develop my scripts i have to launch the browser again and again every time after adding something to my code. My question is "Is it possible to execute a command on already opened browser from a ruby(.rb ) file" ??. Which will save me a lot of time . My framework is based on cucumber, capybara and selenium webdriver.
Thanks In advance
回答1:
It's not possible to use existing browser with Selenium Webdriver.
However, there drivers (Capybara-Webkit, Poltergeist, Webdriver's HtmlUnitDriver) that are faster and have less startup time than browsers. Maybe, they will be good for you.
回答2:
Not sure what you want exactly... if you have to "F5" everytime you change something in the UI-Code, you may consider using the guard gem.
guard + guard-livereload and the rack-livereload included in your app automatically updates the browser if you edit "views".
回答3:
I've been using a monkey-patched version of Selenium Webdriver, which keeps PhantomJS browser open between test runs. See this blog post: http://blog.sharetribe.com/2014/04/07/faster-cucumber-startup-keep-phantomjs-browser-open-between-tests/
The trick is to stub the Selenium::WebDriver::PhantomJS::Service.create_process method and tell the Selenium Driver to use the default port 8910 even though it is reserved.
You can add this piece of code to your config/test.rb file:
class Selenium::WebDriver::PortProber
def self.free?(port)
true
end
end
class Selenium::WebDriver::PhantomJS::Service
def create_process(args)
puts "Starting monkey-patched PhantomJS Selenium Webdriver"
Struct.new("ChildProcessFake") do
def start() end
def exited?() true end
end.new
end
end
Now you can start PhantomJS in a terminal tab:
phantomjs --webdriver=8910
And when you have PhantomJS running, you can start your Cucumber tests.
Obviously this works only for PhantomJS, but something similar could work with other browsers too.
来源:https://stackoverflow.com/questions/14229764/how-do-i-execute-a-command-on-already-opened-browser-from-a-ruby-file