2017-03-03 162 views
0

BLUF:Ruby腳本在命令行上正常工作,但在Jenkins中失敗。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) 

我有我使用的Watir運行Ruby腳本。當我在命令行上運行它時工作正常,但是當我通過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'安裝的。我還仔細檢查了每個安裝寶石的版本。我將沿着命令行傳遞的變量硬編碼到腳本中(而不是使用參數化構建)。以下是正在執行的腳本中的代碼:

#!/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

看起來像'瀏覽器'沒有初始化。你有沒有安裝'watir-webdriver'在Jenkins運行的機器上啓動Chrome? – Uzbekjon

+0

@Uzbekjon我加了需求。 fwiw/fyi,watir 6只需要「要求」watir'「,不再需要」* -webdriver「。也默認選擇了chrome。注意:安裝了chromedriver。 –

回答

0

答案:Chrome無法以超級用戶身份運行。詹金斯以root身份運行。當它運行一個構建時,構建以root身份運行。在使用以下命令來更改腳本運行的用戶。

su -c 'ruby /opt/mysite/ci/regression.rb someSiteName dev false true' jenkins 
相關問題