2017-06-15 58 views
0

我已經使用nightwatch.js爲我的產品自動化e2e測試用例。它在Chrome,Firefox和其他基於UI的瀏覽器上運行得非常好。然而,我需要在phantom.js上運行它作爲自動化的無頭瀏覽器運行它作爲Jenkins的一部分。nightwatch.js不適用於phantom.js

我試過,但測試腳本不能與phantom.js一起使用。

測試腳本:

describe('TEST PHANTOMJS#',function() { 

    afterEach((client,done) => { 
    client.end(() => done()); 
    }); 

    it('successful test google.com',(client)=> { 

    // Launch google.com 
    client.url('https://www.google.com').resizeWindow(1000,800); 
    console.log('Launched Google') 
    client.expect.element('body1').to.be.present.before(1000); // test error 
    console.log('Completed testing') 
    }); 
}); 

我nightwatch.json配置:

{ 
    "src_folders": [ 
    "tests" 
    ], 
    "output_folder": "reports", 
    "custom_commands_path": "", 
    "custom_assertions_path": "", 
    "page_objects_path": "", 
    "selenium": { 
    "start_process": true, 
    "server_path": "./bin/selenium/selenium-server-standalone-3.0.1.jar", 
    "log_path": "", 
    "port": 4444, 
    "cli_args": { 
     "webdriver.chrome.driver": "./bin/chrome/chromedriver", 
     "webdriver.gecko.driver": "./bin/firefox/geckodriver", 
     "webdriver.edge.driver": "./bin/ie/IEDriverServer.exe" 
    } 
    }, 
    "test_settings": { 
    "default": { 
     "selenium_port": 4444, 
     "selenium_host": "localhost", 
     "default_path_prefix": "/wd/hub", 
     "silent": true, 
     "screenshots": { 
     "enabled": true, 
     "on_failure": true, 
     "path": "./screen-shots" 
     }, 
     "desiredCapabilities": { 
     "browserName": "phantomjs", 
     "javascriptEnabled": true, 
     "acceptSslCerts": true, 
     "phantomjs.binary.path": "./node_modules/phantomjs-prebuilt/bin/phantomjs", 
     "phantomjs.page.settings.userAgent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.80 Safari/537.36", 
     "phantomjs.cli.args": [] 
     }, 
     "test_runner": { 
     "type": "mocha", 
     "options": { 
      "ui": "bdd", 
      "reporter": "list" 
     } 
     } 
    } 
    } 
} 

運行./node_modules/.bin/nightwatch --env qa --verbose後,我看到下面的日誌

> nightwatch --env qa --verbose 

Starting selenium server... started - PID: 11037 

    TEST PHANTOMJS# successful test google.com: Launched Google 
Completed testing 
INFO Request: POST /wd/hub/session 
- data: {"desiredCapabilities":{"browserName":"phantomjs","javascriptEnabled":true,"acceptSslCerts":true,"platform":"ANY","phantomjs.binary.path":"./node_modules/phantomjs-prebuilt/bin/phantomjs","phantomjs.page.settings.userAgent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.80 Safari/537.36","phantomjs.cli.args":[]}} 
- headers: {"Content-Type":"application/json; charset=utf-8","Content-Length":372} 
INFO Response 200 POST /wd/hub/session (1409ms) { state: null, 
    sessionId: 'd16c7439-18ec-4b67-85eb-e3dda6fe0075', 
    hCode: 1253002783, 
    value: 
    { applicationCacheEnabled: false, 
    rotatable: false, 
    handlesAlerts: false, 
    databaseEnabled: false, 
    version: '2.1.1', 
    platform: 'MAC', 
    browserConnectionEnabled: false, 
    proxy: { proxyType: 'direct' }, 
    nativeEvents: true, 
    acceptSslCerts: false, 
    driverVersion: '1.2.0', 
    'webdriver.remote.sessionid': 'd16c7439-18ec-4b67-85eb-e3dda6fe0075', 
    'phantomjs.page.settings.userAgent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.80 Safari/537.36', 
    locationContextEnabled: false, 
    webStorageEnabled: false, 
    browserName: 'phantomjs', 
    takesScreenshot: true, 
    driverName: 'ghostdriver', 
    javascriptEnabled: true, 
    cssSelectorsEnabled: true }, 
    class: 'org.openqa.selenium.remote.Response', 
    status: 0 } 
INFO Got sessionId from selenium d16c7439-18ec-4b67-85eb-e3dda6fe0075 
INFO Request: POST /wd/hub/session/d16c7439-18ec-4b67-85eb-e3dda6fe0075/url 
- data: {"url":"https://www.google.com"} 
- headers: {"Content-Type":"application/json; charset=utf-8","Content-Length":32} 

理想的情況下,它應該完成與報告測試一個錯誤。然而,它卡住了,並沒有進一步移動。

任何幫助,將不勝感激。

+0

太奇怪了,這不是由於代理髮行:-(現在試圖找出如何爲幻影JS設置代理 – joy

回答

0

當您使用PhantomJS在網站上與HTTPS,您通常需要與ignore-ssl-errors選項運行你的腳本。否則,您經常會遇到麻煩......如果您的腳本適用於所有圖形瀏覽器,但不適用於PhantomJS,那麼您的問題很可能與SSL/TLS相關。

nightwatch.json,其中配置PhantomJS,確保添加命令行選項:

"phantomjs.cli.args": ["--ignore-ssl-errors=true"] 

下面的腳本不無選項工作(它不打印的頁面標題),但它工作時你添加它。

module.exports = { 
    'PhantomJS': function(browser) { 
    browser 
     .url('https://www.google.com') 
     .waitForElementVisible('body', 1000) 
     .getTitle(function (title) { 
     console.log(title); 
     }) 
     .end(); 
    } 
}; 
+0

正如我懷疑工作,這是一個代理問題,我加「代理」:{ 「proxyType」:「manual」, 「httpProxy」:「http://proxy.mycompany.com:8080/」 }。它的工作。但現在我有另一個問題。我的網址被重定向到另一個網址,如何支持phantomjs重定向? – joy