在 after() hook nightwatch 中设置 cookie

Set cookie in after() hook nightwatch

我尝试在 nightwatch after() 挂钩函数中设置 cookie,但显然它没有用。我的想法是,如果测试失败,我想将 cookie 的值设置为 "failed",如果测试通过,则设置为 "success"。

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() 函数中似乎没有会话。

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

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