BLUF:Ruby脚本在命令行上运行正常,但在Jenkins中失败。
环境信息:
Ubuntu 16.04
Watir 6.1.0
Jenkins 2.4
Rubygems 2.6.10
*** LOCAL GEMS **
bigdecimal (default: 1.2.8)
childprocess (0.6.2)
colorize (0.8.1)
dbi (0.4.5)
did_you_mean (1.0.0)
ffi (1.9.17)
headless (2.3.1)
io-console (default: 0.4.5)
json (default: 1.8.3)
mail (2.6.4)
mime-types (3.1)
mime-types-data (3.2016.0521)
mini_portile2 (2.1.0)
minitest (5.10.1, 5.8.4)
mysql2 (0.4.5)
net-ssh (4.1.0)
net-ssh-gateway (2.0.0)
net-telnet (0.1.1)
nokogiri (1.7.0.1)
power_assert (1.0.1, 0.2.7)
psych (default: 2.0.17)
rake (12.0.0, 10.5.0)
rdoc (5.1.0, default: 4.2.1)
rubygems-update (2.6.10, 2.6.8)
rubyzip (1.2.1)
selenium-webdriver (3.2.2)
test-unit (3.2.3, 3.1.7)
watir (6.2.0, 6.1.0)
websocket (1.2.4)
我有一个ruby脚本我正在使用watir运行。当我在命令行上运行它时它工作正常,但是当我通过Jenkins中的作业/构建运行它时失败。它始终失败,因为设置浏览器不起作用。以下是以下错误:
Started by user anonymous
Building in workspace /ebs/jenkins/jobs/test watir/workspace
[workspace] $ /bin/sh -xe /tmp/hudson6395492977367774608.sh
+ ruby /opt/mysite/ci/regression.rb someSiteName dev false true
Running headless.
Running browser.
/opt/mysite/ci/regression.rb:166:in `<main>': undefined method `close' for nil:NilClass (NoMethodError)
Did you mean? clone
Net::ReadTimeout. /usr/lib/ruby/2.3.0/net/protocol.rb:158:in `rbuf_fill'
<br />/usr/lib/ruby/2.3.0/net/protocol.rb:136:in `readuntil'
<br />/usr/lib/ruby/2.3.0/net/protocol.rb:146:in `readline'
<br />/usr/lib/ruby/2.3.0/net/http/response.rb:40:in `read_status_line'
<br />/usr/lib/ruby/2.3.0/net/http/response.rb:29:in `read_new'
<br />/usr/lib/ruby/2.3.0/net/http.rb:1437:in `block in transport_request'
<br />/usr/lib/ruby/2.3.0/net/http.rb:1434:in `catch'
<br />/usr/lib/ruby/2.3.0/net/http.rb:1434:in `transport_request'
<br />/usr/lib/ruby/2.3.0/net/http.rb:1407:in `request'
<br />/usr/lib/ruby/2.3.0/net/http.rb:1400:in `block in request'
<br />/usr/lib/ruby/2.3.0/net/http.rb:853:in `start'
<br />/usr/lib/ruby/2.3.0/net/http.rb:1398:in `request'
<br />/var/lib/gems/2.3.0/gems/selenium-webdriver-3.2.2/lib/selenium/webdriver/remote/http/default.rb:124:in `response_for'
<br />/var/lib/gems/2.3.0/gems/selenium-webdriver-3.2.2/lib/selenium/webdriver/remote/http/default.rb:78:in `request'
<br />/var/lib/gems/2.3.0/gems/selenium-webdriver-3.2.2/lib/selenium/webdriver/remote/http/common.rb:61:in `call'
<br />/var/lib/gems/2.3.0/gems/selenium-webdriver-3.2.2/lib/selenium/webdriver/remote/bridge.rb:671:in `raw_execute'
<br />/var/lib/gems/2.3.0/gems/selenium-webdriver-3.2.2/lib/selenium/webdriver/remote/bridge.rb:110:in `create_session'
<br />/var/lib/gems/2.3.0/gems/selenium-webdriver-3.2.2/lib/selenium/webdriver/remote/bridge.rb:71:in `initialize'
<br />/var/lib/gems/2.3.0/gems/selenium-webdriver-3.2.2/lib/selenium/webdriver/chrome/bridge.rb:42:in `initialize'
<br />/var/lib/gems/2.3.0/gems/selenium-webdriver-3.2.2/lib/selenium/webdriver/common/driver.rb:61:in `new'
<br />/var/lib/gems/2.3.0/gems/selenium-webdriver-3.2.2/lib/selenium/webdriver/common/driver.rb:61:in `for'
<br />/var/lib/gems/2.3.0/gems/selenium-webdriver-3.2.2/lib/selenium/webdriver.rb:87:in `for'
<br />/var/lib/gems/2.3.0/gems/watir-6.2.0/lib/watir/browser.rb:46:in `initialize'
<br />/opt/mysite/ci/regression.rb:50:in `new'
<br />/opt/mysite/ci/regression.rb:50:in `<main>'
Build step 'Execute shell' marked build as failure
Finished: FAILURE
我从'apt'安装的模块中仔细检查了我正在使用的模块。我还仔细检查了每个安装gem的版本。我将沿命令行传递的变量硬编码到脚本中(而不是使用参数化构建)。以下是正在执行的脚本中的代码:
#!/usr/bin/env ruby
require 'watir'
require 'headless'
argSite = ARGV[0]
argEnvironment = ARGV[1]
argSendEmail = ARGV[2]
argHeadless = ARGV[3]
begin
if argHeadless.to_s == 'true'
puts "Running headless."
headless = Headless.new
headless.start
end
puts "Running browser."
# vvv reason tests fail
browser = Watir::Browser.new(:chrome)
browser.window.resize_to(1200, 1000)
browser.driver.manage.timeouts.implicit_wait = 15
...tests go here...
rescue => e
puts ("#{e}. "+ e.backtrace.join("\n<br />"))
ensure
# vvv script fails here
browser.close
if argHeadless.to_s == 'true'
headless.destroy
end
end
答案 0 :(得分:0)
答案:Chrome无法以root用户身份运行。 Jenkins以root身份运行。运行构建时,构建以root身份运行。使用以下命令更改脚本运行的用户。
su -c 'ruby /opt/mysite/ci/regression.rb someSiteName dev false true' jenkins