2017-08-15 27 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的测试。

相关问题