27123

How to clear a browser cache in Protractor

Question:

In one of my scenario I need to clear a browser cache. I have tried with below one but it won't work.

browser.driver.manage().deleteAllCookies(); browser.executeScript('window.sessionStorage.clear();'); browser.executeScript('window.localStorage.clear();');

Can anyone help me on this?

Answer1:

I use it this way:

function LoginPage() { this.get = function() { browser.get('#/Login'); browser.executeScript('window.localStorage.clear();'); browser.executeScript('window.sessionStorage.clear();'); browser.driver.manage().deleteAllCookies(); } } module.exports = new LoginPage;

then I use this function in any of my test script which every time clears out cookies, session/local storage.

var loginPage = require('.././LoginPage'); loginPage.get();

Answer2:

First of all I suggest doing it in forked browsers which will simplify your debugging efforts

var fork = browser.forkNewDriverInstance(); fork.get('page1');

If you still insist on clearing everything you can restart browser

browser.restart(); // Restart the browser clears everything (careful as it returns Promise) browser.restartSync(); // Same as previous but synchronous == await browser.restart()

Or even better restart fork

fork = fork.restartSync(); fork.get('page2'); // 'page2' gotten by restarted fork

Info: <a href="https://github.com/angular/protractor/blob/master/lib/browser.ts#L537" rel="nofollow">https://github.com/angular/protractor/blob/master/lib/browser.ts#L537</a>

P.S. Not sure which parts you can't clear, but as a last resort I can suggest is incognito mode

capabilities: { 'browserName': 'chrome', 'chromeOptions': { 'args': ['incognito'] } },

Info: <a href="https://github.com/angular/protractor/blob/master/docs/browser-setup.md#adding-chrome-specific-options" rel="nofollow">https://github.com/angular/protractor/blob/master/docs/browser-setup.md#adding-chrome-specific-options</a>

Recommend

  • Celery will not flush memory when task has ended
  • Javascript Arrays merging using indexes
  • how to create .htaccess for ruby on rails?
  • How to make R legend with 2 columns?
  • AWS: Automating queries in redshift
  • How to draw a circle of lines
  • Can't subscribe to Push notification
  • Importing Excel Charts from Excel to PowerPoint causes `RPC_E_SERVERFAULT` on some machines
  • Render Images in tic tac toe app using reactjs
  • Multiple User Types In Django
  • How to pivot table for year in SQL?
  • Jenkins job DSL plugin - hidden parameter
  • How to detect user changing sheet?
  • How to concatenate data.frame inside lists by using names?
  • How can I detect mongodb reconnect failed event
  • Detect when MathJax has finished loading in UIWebView
  • Insert statement not working using execute(array()) of PDO Extension
  • Git for windows has stopped working
  • Sorting Custom Listview Items Using Spinner Android
  • Running iPhone crash Logs from testers on XCode
  • Use awk to convert GPS Position to Latitude & Longitude
  • Reader monad - how does it conform to Monad interface?
  • Defer unused CSS
  • Adding native code to an existing Worklight hybrid app
  • Unable to start a WebView from an AsyncTask
  • playing mp3 from nsbundle
  • time column in sqlite using gorm
  • Calculate time from document
  • How to include associated objects using gon in Rails/jQuery
  • jQuery scrollTop if URL has hash
  • Google App Engine backend servlet not responding
  • Computing the discrete fourier transform of audio data with FFTW
  • Make checkout phone field optional for specific countries in WooCommerce
  • Excel VBA : conditional formatting of sheet1 cells from sheet2 values in excel 2007
  • VLOOKUP in IMPORTRANGE
  • Jersey serializes character value to ASCII equivalent numeric string
  • How to use FirstOrDefault inside Include