简体   繁体   English

在rake任务上运行bash命令之前运行服务器

[英]Run server before to run bash command on rake task

I'm doing a rake task in order to run tests using protractor . 我正在执行一项rake任务,以便使用量角器运行测试。 On my task I need: 在我的任务上,我需要:

  1. Run web server 运行网络服务器
  2. Run Selenium 运行
  3. Run my tests 运行我的测试

My problem is that I need to run web server and Selenium before to run my tests. 我的问题是,在运行测试之前,我需要运行Web服务器和Selenium。 This is my rake task: 这是我的耙任务:

#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 function code. server_ready功能代码。 With this method I'm checking if web server and selenium are up and running 通过这种方法,我正在检查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

My problem is not about Selenium or Protractor, my problem is how to wait one command is ready before running the next 我的问题不是关于硒或量角器,我的问题是如何在运行下一个命令之前等待一个命令就绪

I think your "busy" waiting loop is not written correctly. 我认为您的“忙”等待循环未正确编写。 try something like: 尝试类似:

until server_ready?("http://localhost:3001/index") do
  sleep 1
end

instead of next until. 而不是下一个直到。

Another thing to consider is how to stop your services after your tests did run. 要考虑的另一件事是在测试运行后如何停止服务。

It was a problem related with Protractor. 这是一个与量角器有关的问题。 The code that I'm showing works after all... 我显示的代码毕竟可以工作...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM