前言
Cypress 默認每個用例開始之前會清空所有的cookies,保證每個用例的獨立性和干凈的環境。
但是我們希望在一個js文件下寫多個測試用例的時候,希望只調用一次登錄, 記住cookies,后面的用例都默認是登錄狀態,這樣測試的效率高一些。
實現cookies共享有2種實現方式
- 1.Cypress.Cookies.preserveOnce('key name1', 'key name2') 保留cookies
- 2.Cypress.Cookies.defaults({whitelist: 'session_id'}) 白名單設置
詳情參看官網文檔https://docs.cypress.io/api/cypress-api/cookies.html#Defaults
保留cookies
Cypress為您提供了一個接口,用於自動保存多個測試的Cookie。默認情況下,在每次新測試開始之前,Cypress會自動清除所有cookie。
通過在每次測試前清除cookies,保證您總是從頭開始。從一個干凈的狀態開始可以防止將測試耦合到另一個測試,並防止在一個測試中對應用程序中的某些內容進行變異影響下游的另一個測試。
如果你確定需要在多個用例之間保留cookies,可以使用 Cypress.Cookies.preserveOnce()
可能有更好的方法可以做到這一點,但目前還沒有很好的記錄。每個應用程序都是不同的,沒有一個適合所有應用程序的解決方案。
目前,如果您使用的是基於會話的cookies,則此方法將起作用
describe('Dashboard', () => {
before () => {
// log in only once before any of the tests run.
// your app will likely set some sort of session cookie.
// you'll need to know the name of the cookie(s), which you can find
// in your Resources -> Cookies panel in the Chrome Dev Tools.
cy.login()
})
beforeEach () => {
// before each test, we can automatically preserve the
// 'session_id' and 'remember_token' cookies. this means they
// will not be cleared before the NEXT test starts.
//
// the name of your cookies will likely be different
// this is an example
Cypress.Cookies.preserveOnce('session_id', 'remember_token')
})
it('displays stats', () => {
// ...
})
it('can do something', () => {
// ...
})
it('opens a modal', () => {
// ...
})
})
實際案例參考這篇https://www.cnblogs.com/yoyoketang/p/12927200.html
設置Cookie白名單
您可以修改全局默認值並白名單一組cookie,這些cookie將始終在測試中保留。您在這里所做的任何更改都將在每個測試的剩余部分立即生效。
把這個配置放在您的 cypress/support/index.js 文件中是個很好的地方,因為它是在任何測試文件執行之前加載的。
whitelist 可以接收的參數:
- String 字符串
- Array 數組
- RegExp 正則
- Function 函數
Whitelist String
// now any cookie with the name 'session_id' will
// not be cleared before each test runs
Cypress.Cookies.defaults({
whitelist: 'session_id'
})
Whitelist Array
// now any cookie with the name 'session_id' or 'remember_token'
// will not be cleared before each test runs
Cypress.Cookies.defaults({
whitelist: ['session_id', 'remember_token']
})
Whitelist RegExp
// now any cookie that matches this RegExp
// will not be cleared before each test runs
Cypress.Cookies.defaults({
whitelist: /session|remember/
})
Whitelist Function
Cypress.Cookies.defaults({
whitelist: (cookie) => {
// implement your own logic here
// if the function returns truthy
// then the cookie will not be cleared
// before each test runs
}
})
使用案例
接着前面這篇https://www.cnblogs.com/yoyoketang/p/12927200.html使用cookie白名單的方式實現
先在 cypress/support/index.js 文件中添加 cookie 白名單,這個index.js文件會在測試用例執行之前加載
Cypress.Cookies.defaults({
whitelist: ['zentaosid', 'csrftoken']
})
前面代碼里面去掉 beforeEach()
這段
/**
* Created by dell on 2020/5/21.
* 作者:上海-悠悠 交流QQ群:939110556
*/
describe('登錄后-訪問首頁', function() {
before(() => {
cy.login("admin", "****123456")
})
it("訪問首頁", () =>
{
cy.visit("http://ip:8080/zentao/my/")
cy.url().should('include', '/zentao/my/')
cy.title().should('contain', '我的地盤 - 禪道')
})
it("訪問后台管理", () =>
{
cy.visit("http://49.235.92.12:8080/zentao/admin/")
cy.url().should('include', '/zentao/admin/')
cy.title().should('contain', '后台管理 - 禪道')
})
})
這樣就避免了每個js文件都在beforeEach()
加一次 Cypress.Cookies.preserveOnce('zentaosid', 'csrftoken')
很顯然這種白名單的方式更優雅一點
QQ交流群:939110556