我正在执行 rake 任务,以便使用 protractor 运行测试.在我的任务中,我需要:
- 运行网络服务器
- 运行 Selenium
- 运行我的测试
我的问题是我需要先运行 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 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")
server_ready 函数代码。使用这种方法,我检查 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 或 Protractor,我的问题是如何在运行下一个命令之前等待一个命令准备就绪
最佳答案
我认为你的“忙”等待循环写得不正确。 尝试类似的东西:
until server_ready?("http://localhost:3001/index") do
sleep 1
end
而不是下一个直到。
另一件需要考虑的事情是如何在测试运行后停止服务。
关于ruby-on-rails - 在 rake 任务上运行 bash 命令之前运行服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22542544/