【问题标题】:Invalid session ID when using Page Object on nightwatch cucumber在守夜黄瓜上使用页面对象时会话 ID 无效
【发布时间】:2017-08-15 08:16:59
【问题描述】:

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

我正在尝试将一些经常使用的测试转换为页面对象,并且我有以下 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 和上下文为 null :

在我的 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 中设置页面对象。

【问题讨论】:

  • 问题解决:在测试中使用页面对象后,需要使用Pageobject.api调用函数,不能做client.whateveritis()。例如logon.password(username).api.elements(blablabla)
  • 您能否将您的评论转换为答案并接受它。这样,其他人也可以从您的发现中受益,我们会改进 SO。

标签: selenium cucumber nightwatch.js pageobjects


【解决方案1】:

您是否设置了 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'
      }
    }
  }
}

这是登录页面:

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

这是登录步骤:

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

我们使用了一种稍微不同的方法来执行此操作,但希望这可以帮助您了解测试的布局。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-22
    • 1970-01-01
    相关资源
    最近更新 更多