Ruby on rails 先运行服务器,然后对rake任务运行bash命令
我正在做一个rake任务,以便使用运行测试。在我的任务中,我需要:Ruby on rails 先运行服务器,然后对rake任务运行bash命令,ruby-on-rails,ruby,bash,rake-task,Ruby On Rails,Ruby,Bash,Rake Task,我正在做一个rake任务,以便使用运行测试。在我的任务中,我需要: 运行web服务器 跑 运行我的测试 我的问题是,在运行测试之前,我需要运行web服务器和Selenium。 这是我的任务: #1: run server Thread.new { system("bundle exec rails s -p 3001 -e test") } next until server_ready? "http://localhost:3001/index" #2: run Selenium. I'm
#1: run server
Thread.new { system("bundle exec rails s -p 3001 -e test") }
next until server_ready? "http://localhost:3001/index"
#2: run Selenium. I'm trying to execute this after web server is running
Thread.new { system("webdriver-manager start") }
next until server_ready? "http://localhost:4444/wd/hub/static/resource/hub.html"
#3: run tests. I'm trying to execute this after Selenium is running
system("protractor ../web/test/protractor.conf.js")
服务器就绪功能代码。使用此方法,我将检查web服务器和selenium是否已启动并正在运行
def self.server_ready? _url
begin
url = URI.parse(_url)
req = Net::HTTP.new(url.host, url.port)
res = req.request_head(url.path)
res.code == "200"
rescue Errno::ECONNREFUSED
false
end
end
我的问题不是关于Selenium或量角器,我的问题是如何在运行下一个命令之前等待一个命令准备就绪我认为您的“忙碌”等待循环写得不正确。
尝试以下方法:
until server_ready?("http://localhost:3001/index") do
sleep 1
end
而不是下一个直到
另一个要考虑的问题是如何在测试完成后停止服务。 < P>这是一个与量角器有关的问题。我展示的代码毕竟是有效的…+1因为你的“等待循环”比我的好。我将停止服务执行:system(“kill-9$(lsof-I:3001-t)”)。与4444端口上的Selenium相同