尝试禁用或删除 Cucumber 测试的本地存储。
我尝试了以下方法:
page.driver.execute_script("localStorage.clear()")
但我收到以下错误
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)
最佳答案
我得到了同样的错误。问题是我试图在没有先访问页面的情况下执行脚本。这会引发上面 Justin 所解释的错误:
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
这有效:
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
关于Cucumber:使用 Poltergeist (PhantomJS) 禁用或删除本地存储,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19646359/