【问题标题】:Set cookie in after() hook nightwatch在 after() hook nightwatch 中设置 cookie
【发布时间】:2018-02-24 08:11:10
【问题描述】:

我试图在 nightwatch after() 钩子函数中设置 cookie,但显然它不起作用。这个想法是如果测试失败,我想将 cookie 的值设置为“失败”,如果测试通过,则设置为“成功”。

export = {
  '@tags': [ 'heboh' ],

  after(browser) {
    browser
      .setCookie({ name: 'mycookie', value: 'success' })
      .getCookie('mycookie', function callback(result) {
        console.log(result); // print null
      })
      .end();
  },

  'create heboh'(browser) {
    browser
      .url('http://www.google.com')
      .waitForElementVisible('body', 1000)
      .assert.title('Facebook'); // intended to make it failed
  }
}

我指定了 --verbose,这就是我得到的

FAILED:  1 assertions failed and 1 passed (5.446s)
INFO Request: POST /wd/hub/session/null/cookie
 - data:  {"cookie":{"name":"mycookie","value":"true"}}
 - headers:  {"Content-Type":"application/json; charset=utf-8","Content-Length":45}
INFO Response 404 POST /wd/hub/session/null/cookie (19ms) { sessionId: 'null',
  value:
   { error: 'invalid session id',
     message: 'No active session with ID null',
     stacktrace: '' },
  status: 6 }
LOG     → Completed command cookie (22 ms)
INFO Request: GET /wd/hub/session/null/cookie
 - data:
 - headers:  {"Accept":"application/json"}
INFO Response 404 GET /wd/hub/session/null/cookie (15ms) { sessionId: 'null',
  value:
   { error: 'invalid session id',
     message: 'No active session with ID null',
     stacktrace: '' },
  status: 6 }
null
LOG     → Completed command cookie (16 ms)
LOG     → Completed command end (0 ms)

看起来 after() 函数中没有会话。

【问题讨论】:

    标签: nightwatch.js


    【解决方案1】:

    试试这个,我相信你需要在你的后钩上调用 done。

      after: function (browser, done) {
        browser
          .setCookie({name: 'mycookie', value: 'success'})
          .getCookie('mycookie', function callback(result) {
            console.log(result);
          })
          .end()
          .perform(function () {
            done();
          });
      }
    

    【讨论】:

    • 对,我忘了调用 done()。谢谢@schroedes 提供的线索。
    猜你喜欢
    • 1970-01-01
    • 2013-11-30
    • 2015-12-31
    • 1970-01-01
    • 1970-01-01
    • 2014-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多