2017-08-15 26 views
1

更新:問題解決了,檢查評論無效的會話ID在使用上nightwatch黃瓜頁面對象

我試圖將一些經常使用的測試頁面對象和我有作爲pg.js以下:

var myCommands ={ 
    security:function(username){ 
     this.click('@logon') 
      .waitForElementVisible('@id',20000) 
      .click('@id') 
      .setValue('@id',username) 
      .click('@device') 
    }, 
    password:function(username){ 
     console.log(this) 
     return this.useXpath() 
        .navigate() 
        .assert.elementPresent('@logon', 20000) 
        .click('@logon') 
        .waitForElementVisible('@id',20000) 
        .click('@id') 
        .setValue('@id',username) 
        .click('@password') 
        .waitForElementVisible('//input[@name="Answer"]', 20000); 
    } 
}; 

module.exports={ 
    url : 'https://mywebsite', 
    commands :[myCommands], 
    elements:{ 

     logon:{ 
      locateStrategy: 'xpath', 
      selector:'//a[@title="Log on"]' 
     }, 
     id:{ 
      locateStrategy: 'xpath', 
      selector:'//input[@name="userid"]' 
     }, 
     device:{ 
      locateStrategy: 'xpath', 
      selector:'//a[text() = "Login with PIN"]' 
     }, 
     password:{ 
      locateStrategy: 'xpath', 
      selector:'//a[text() = "Login with passwords"]' 
     } 
    } 

}; 

從執行console.log(本),我可以看到會話ID和上下文爲空: enter image description here

在我test.js,我有這樣兩條線:

var logon=client.page.pg() 
.... 
logon.password(username) 

當我運行測試,它顯示

Error: Creating screenshot was not successful. Response was: 
{ status: -1, 
    value: 
    { error: 'invalid session id', 
    message: 'No active session with ID null', 
    stacktrace: '' }, 
    errorStatus: 6, 
    error: '' } 

我的問題是,爲什麼會是空?如果在pg.js或test.js中設置頁面對象有任何問題。

+0

問題解決:在測試中使用頁面對象後,需要使用Pageobject.api來調用函數,並且不能執行client.whateveritis()。例如'logon.password(用戶名).api.elements(blablabla)' – Raymond

回答

0

您是否設置了nightwatch.conf.js文件來顯示您的頁面對象結構?

我使用nightwatch黃瓜與頁面的對象模型和我的結構如下:

//nightwatch.conf.js 
const seleniumServer = require('selenium-server') 
const phantomjs = require('phantomjs-prebuilt') 
const chromedriver = require('chromedriver') 

require('nightwatch-cucumber')({ 
    cucumberArgs: ['--require', 'hooks.js', '--require', 'features/step_definitions', '--format', 'pretty', '--format', 'json:reports/cucumber.json', 'features'] 
}) 

module.exports = { 
    output_folder: 'reports', 
    live_output: false, 
    disable_colors: false, 
    page_objects_path: 'pages', 
    test_workers: false, 
    selenium: { 
    start_process: true, 
    server_path: seleniumServer.path, 
    log_path: '', 
    host: '127.0.0.1', 
    port: 4444 
    }, 
    test_settings: { 
    default: { 
     launch_url: 'whatevertheurlis.com', 
     selenium_port: 4444, 
     selenium_host: '127.0.0.1', 
     globals: { 
     base_url: 'baseurl.com' 
     }, 
     desiredCapabilities: { 
     browserName: 'chrome', 
     javascriptEnabled: true, 
     acceptSslCerts: true 
     }, 
     selenium: { 
     cli_args: { 
      'webdriver.chrome.driver': chromedriver.path 
     } 
     }, 
     screenshots: { 
     enabled : true, 
     on_failure : true, 
     path : 'screenshots/default' 
     } 
    } 
    } 
} 

這是loginPage:

//pages/loginPage.js 
module.exports = { 
    url: function() { 
    return this.api.globals.base_url + '/#/login'; 
    }, 

    elements: { 
    body: 'body', 
    error: 'div.error', 
    header: '.header-title', 
    emailInput: '#field_email', 
    passwordInput: '#field_password', 
    submitButton: '.btn-success' 
    }, 

    commands: [{ 
    goTo: function() { 
     return this.navigate() 
       .waitForElementVisible('@body', 3000); 
    } 
    }] 
} 

這是loginSteps:

//features/step_definitions/loginSteps.js 
const { client } = require('nightwatch-cucumber') 
const { defineSupportCode } = require('cucumber') 

const loginPage = client.page.loginPage(); 
const navbar = client.page.navbar(); 
const resetPasswordPage = client.page.resetPasswordPage(); 
const shared = client.page.shared(); 

defineSupportCode(({ Given, Then, When }) => { 
    Given(/^I go to login page$/,() => { 
    return loginPage 
     .goTo(); 
    }) 
}) 

我們正在使用稍微不同的方法來做到這一點,但希望這可以幫助您看到layou t的測試。

相關問題