在nightwatch黄瓜上使用Page Object时无效的会话ID

时间:2017-08-15 08:16:59

标签: selenium cucumber nightwatch.js pageobjects

更新:问题已解决,请检查评论

我正在尝试将一些经常使用的测试转换为页面对象,我将以下内容作为 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(this),我可以看到会话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中设置页面对象有任何问题。

1 个答案:

答案 0 :(得分:0)

您是否设置了nightwatch.conf.js文件以显示您的页面对象结构?

我正在使用具有页面对象模型的nightwatch-cucumber,我的结构如下:

//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();
  })
})

我们使用的方法略有不同,但希望这可以帮助您查看测试的布局。