ラベル Protractor selenium の投稿を表示しています。 すべての投稿を表示
ラベル Protractor selenium の投稿を表示しています。 すべての投稿を表示

2016年7月1日金曜日

Protractorでセッションをクリアできないときの対策

以下のスクリプトでセッションをクリアしてみたら、
browser.executeScript('localStorage.clear();');
 思うようにクリアしてくれない、

 理由はpageちゃっとロード完了できていなかったみたい

以下のソースを使えばその問題を解消できます。



  function getWindowLocation() {
    return window.location;
  }

  function clearStorage() {
    window.sessionStorage.clear();
    window.localStorage.clear();
  }

  return browser.executeScript(getWindowLocation).then(function(location) {
    // NB If no page is loaded in the scneario then calling clearStorage will cause exception
    // so guard against this by checking hostname (If no page loaded then hostname == '')
    if (location.hostname.length > 0) {
      return browser.executeScript(clearStorage);
    }
    else {
      return Promise.resolve();
    }
  });