LoginSignup
2
2

More than 5 years have passed since last update.

Protractor で起動したブラウザで任意の JavaScirpt コードを実行する

Last updated at Posted at 2015-05-28

Protractor でブラウザのコンソールを使ってアサーションする

目的

E2E テストの為に起動したブラウザで任意の JavaScript コードを実行して callback を取得する。

方法

executeAsyncScript を使う。

describe('ブラウザから JavaScript を実行するよ', function() {
  it('例えばある API を取得してレスポンスをアサーションする', function() {
    // given
    browser.get('http://localhost:8000/');
    browser.waitForAngular();

    // when
    browser.executeAsyncScript(function() {
      var callback = arguments[arguments.length -1];
      $.ajax({
        url: 'http://localhost:5000/api/',
        method:'GET',
        xhrFields:{withCredentials: true},
        crossDomain: true
      }).success(callback);
    }).then(function(res) {
      // Then
      expect(res.status).toEqual(200); // status code 
      expect(res.responseJSON).toEqual({  // response body
        status: 'ok'
      });
    });
  });

以上、ブラウザの保持するセッションによって API のレスポンスをアサーションしたかったのを調べた時のチップスでした。

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2