Cypress 简明教程
Cypress - Cookies
Cypress 使用方法 Cookies.preserveOnce() 和 Cookies.defaults() 处理 Cookies。如果 Cookies 有任何更改,方法 Cookies.debug() 会在控制台生成日志。
Cypress handles cookies with the methods Cookies.preserveOnce() and Cookies.defaults(). The method Cookies.debug() produces logs to the console, if there are any changes to the cookies.
默认情况下,Cypress 会在每次测试执行前删除所有 Cookie。我们可以利用 Cypress.Cookies.preserveOnce() 来保留 Cookies 及其名称,以便在其他测试中使用。
By default, Cypress removes all cookies prior to each test execution.We can utilise Cypress.Cookies.preserveOnce() to preserve the cookies with their names to be used for other tests.
Syntax
在 Cypress 中用于与 Cookie 相关的命令的语法如下所示:
The syntax for the commands related to the cookies in Cypress are as follows −
这将在配置或清除 Cookie 值时生成控制台日志。
This will produce console logs, if cookie values are configured or cleared.
Cypress.Cookies.debug(enable, option)
在此,
Here,
-
enable – if debug of cookie should be enabled.
-
option – configure default values for cookies, for example, preserve cookies.
Cypress.Cookies.debug(true) // logs will generate if cookies are modified
cy.clearCookie('cookie1')
cy.setCookie('cookie2', 'val')
降低日志记录的级别。
To reduce the level of logging.
Cypress.Cookies.debug(true, { verbose: false })
Cypress.Cookies.debug(false) // logs will not generate if cookies are modified
下面给出的语法将保留 Cookies,并且在执行其他测试之前不会清除它们。
The syntax given below will preserve the cookies and they will not be cleared prior execution of another test.
Cypress.Cookies.preserveOnce(cookie names...)
此语法用于修改全局配置并维护为测试保留的一组 Cookie。任何修改都将适用于该特定测试(保留在 cypress/support/index.js 文件中并在测试执行前加载)。
This syntax is used to modify global configuration and to maintain a group of cookies that are preserved for a test. Any modification will be applicable for that particular test.(maintained in cypress/support/index.js file and are loaded prior to test execution).
Cypress.Cookies.defaults(option)
Cypress.Cookies.defaults({
preserve: 'cookie1'
})
此处,不会在运行测试前清除名为 cookie1 的 Cookie。
Here, the cookie named cookie1 will not be cleared before running the test.
Cookie Methods
Cypress 中的一些 Cookie 方法如下:
Some of the cookie methods in Cypress are as follows −
-
cy.clearCookies() − It removes all the cookies from present domain and subdomain.
-
cy.clearCookie(name) − It removes a cookie from the browser by name.
-
cy.getCookie(name) − It is used to obtain a cookie from the browser by name.
-
cy.getCookies() − It is used to obtain all the cookies.
-
cy.setCookie(name) − It can configure a cookie.
Implementation
下面给出了在 Cypress 中 Cookie 方法的实现:
Given below is the implementation of the cookie methods in Cypress −
describe('Tutorialspoint Test', function () {
// test case
it('Scenario 1', function (){
// launch the application
cy.visit("https://accounts.google.com");
// enable cookie logging
Cypress.Cookies.debug(true)
//set cookie
cy.setCookie('cookie1', 'value1')
//get cookie by name and verify value
cy.getCookie('cookie1').should('have.property', 'value', 'value1')
//clear cookie by name
cy.clearCookie('cookie')
//get all cookies
cy.getCookies()
//clear all cookies
cy.clearCookies()
//verify no cookies
cy.getCookies().should('be.empty')
});
});
Execution Results
Execution Results
输出如下:
The output is mentioned below −
