[英]clear cache in Nightmare.js (Electron)
I'm using nightmare js to log into a site that sets a token in local storage. 我正在使用梦魇js登录到在本地存储中设置令牌的站点。 However, any future tests I run the user is already logged in. I'm guessing the local storage wasn't cleared.
但是,我运行用户的任何未来测试都已登录。我猜测本地存储未被清除。 Is there any way to do this?
有没有办法做到这一点? My code in test.js
我在test.js中的代码
require('mocha-generators').install();
var Nightmare = require('nightmare');
var expect = require('chai').expect;
describe('test login', function() {
var nightmare = Nightmare({show: true})
after(function*() {
yield nightmare.end();
})
it('should login given right credentials', function*() {
this.timeout(50000);
console.log("running test");
var link = yield nightmare
.goto('http://127.0.0.1:3000/login')
.wait(1000)
.type('.email-field', 'username@email.com')
.type('.password-field', 'password')
.click('.login button')
.wait(1000)
});
})
I run the test using: mocha 我使用: mocha运行测试
the test runs fine and closes. 测试运行正常并关闭。 However when I run again the user starts off as logged in. Is there anyway to clear the cache or local storage in nightmarejs?
但是,当我再次运行时,用户会在登录时启动。无论如何要清除nightmarejs中的缓存或本地存储?
Electron has a way to clear session info via session.clearCache
( http://electron.atom.io/docs/v0.32.0/api/session/ ) but I don't know how to access the session object from nightmare. Electron有办法通过
session.clearCache
( http://electron.atom.io/docs/v0.32.0/api/session/ )清除会话信息,但我不知道如何从噩梦中访问会话对象。
Alright figured it out, we can use Electrons 'web-preferences' property. 好吧,我们可以使用电子的网络偏好属性。
var nightmare = Nightmare({
show: false,
webPreferences: {
partition: 'nopersist'
}
});
when initiating nightmare use 'web-prefences' partition property to handle sessions. 在启动噩梦时使用'web-prefences'分区属性来处理会话。 more info here: https://github.com/atom/electron/blob/master/docs/api/browser-window.md .
更多信息: https : //github.com/atom/electron/blob/master/docs/api/browser-window.md 。 The gist of is as follows:
要点如下:
the page will use a persistent session available to all pages in the app with the same partition.
该页面将使用可用于具有相同分区的应用程序中所有页面的持久会话。 if there is no persist: prefix, the page will use an in-memory session.
如果没有持久性:前缀,页面将使用内存中的会话。
so basically if you init nightmare as: 所以基本上如果你做噩梦:
var nightmare = Nightmare({
show: false,
webPreferences: {
partition: 'persist:derp'
}
});
then the session will persist under 'derp', this can be helpful when you are testing features inside authenticated routes. 然后会话将在'derp'下持续存在,当您在经过身份验证的路由中测试功能时,这会很有用。 (derp isn't significant, can be anything following persist: )
(DERP并不显著,可以是任何东西坚持如下:)
if you don't want session to persist don't use persist: . 如果你不希望会话持续存在,请不要使用persist : . I use nopersist but this could be any string that isn't prefixed by persist:
我使用nopersist但这可能是任何没有前缀的字符串:
EDIT: show:false
isn't significant to session, it just shows what electron (which nightmare uses) is doing if you set show:true
, but this line can be removed 编辑:
show:false
对于会话来说并不重要,它只是显示当设置show:true
电子(哪个噩梦使用)正在做什么,但这行可以删除
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.