Cucumber: disable or delete local storage using Poltergeist (PhantomJS)

Attempting to disable or remove local storage for the Cucumber test.

I tried the following:

page.driver.execute_script("localStorage.clear()")

but I get the following error:

 One or more errors were raised in the Javascript code on the page. If you don't care about these errors, you can ignore them by setting js_errors: false in your Poltergeist configuration (see documentation for details).

  SECURITY_ERR: DOM Exception 18: An attempt was made to break through the security policy of the user agent.
  at phantomjs://webpage.evaluate():1
  at phantomjs://webpage.evaluate():1
  at phantomjs://webpage.evaluate():1
  at phantomjs://webpage.evaluate():1 (Capybara::Poltergeist::JavascriptError)
+4
source share
2 answers

I was getting the same error. The problem was that I was trying to execute the script without visiting the page in the first place. This causes an error, as Justin explained above:

shared_context '@user is logged in' do
  before do
    # visit '/'
    credentials = @user.create_new_auth_token
    token = credentials['access-token']
    page.execute_script "localStorage.setItem('token', '#{token}')"
  end
end

It works:

shared_context '@user is logged in' do
  before do
    visit '/'
    credentials = @user.create_new_auth_token
    token = credentials['access-token']
    page.execute_script "localStorage.setItem('token', '#{token}')"
  end
end
+1
source

What am I doing:

  visit '<the_url_needed>'
  page.execute_script('if (localStorage && localStorage.clear) localStorage.clear()')

Hope this helps.

0
source

All Articles