Skip to content

Commit

Permalink
feat: Add new cookie commands (#582)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisbreiding authored Dec 13, 2022
1 parent d538683 commit f79751a
Show file tree
Hide file tree
Showing 4 changed files with 18,153 additions and 59 deletions.
65 changes: 63 additions & 2 deletions app/commands/cookies.html
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ <h4 id="getCookie"><a href="https://on.cypress.io/getcookie">cy.getCookie()</a><

<div class="col-xs-7">
<h4 id="getCookies"><a href="https://on.cypress.io/getcookies">cy.getCookies()</a></h4>
<p>To get all browser cookies, use the <a href="https://on.cypress.io/getcookies"><code>cy.getCookies()</code></a> command.</p>
<p>To get browser cookies for the current domain, use the <a href="https://on.cypress.io/getcookies"><code>cy.getCookies()</code></a> command.</p>
<pre><code class="javascript">cy.getCookies().should('be.empty')

cy.get('#getCookies .set-a-cookie').click()
Expand All @@ -121,6 +121,42 @@ <h4 id="getCookies"><a href="https://on.cypress.io/getcookies">cy.getCookies()</
</div>
</div>

<div id="getAllCookies">
<div class="row">

<div class="col-xs-7">
<h4 id="getAllCookies"><a href="https://on.cypress.io/getallcookies">cy.getAllCookies()</a></h4>
<p>To get all browser cookies, use the <a href="https://on.cypress.io/getallcookies"><code>cy.getAllCookies()</code></a> command.</p>
<pre><code class="javascript">cy.getAllCookies().should('be.empty')

cy.setCookie('key', 'value')
cy.setCookie('key', 'value', { domain: 'example.com' })

// cy.getAllCookies() yields an array of cookies
cy.getAllCookies().should('have.length', 2).should((cookies) => {
// each cookie has these properties
expect(cookies[0]).to.have.property('name', 'key')
expect(cookies[0]).to.have.property('value', 'value')
expect(cookies[0]).to.have.property('httpOnly', false)
expect(cookies[0]).to.have.property('secure', false)
expect(cookies[0]).to.have.property('domain')
expect(cookies[0]).to.have.property('path')

expect(cookies[1]).to.have.property('name', 'key')
expect(cookies[1]).to.have.property('value', 'value')
expect(cookies[1]).to.have.property('httpOnly', false)
expect(cookies[1]).to.have.property('secure', false)
expect(cookies[1]).to.have.property('domain', 'example.com')
expect(cookies[1]).to.have.property('path')
})</code></pre>
</div>
<div class="col-xs-5">
</div>

<div class="col-xs-12"><hr></div>
</div>
</div>

<div id="setCookie">
<div class="row">

Expand Down Expand Up @@ -171,7 +207,7 @@ <h4 id="clearCookie"><a href="https://on.cypress.io/clearcookie">cy.clearCookie(

<div class="col-xs-7">
<h4 id="clearCookies"><a href="https://on.cypress.io/clearcookies">cy.clearCookies()</a></h4>
<p>To clear all browser cookies, use the <a href="https://on.cypress.io/clearcookies"><code>cy.clearCookies()</code></a> command.</p>
<p>To clear browser cookies for the current domain, use the <a href="https://on.cypress.io/clearcookies"><code>cy.clearCookies()</code></a> command.</p>
<pre><code class="javascript">cy.getCookies().should('be.empty')

cy.get('#clearCookies .set-a-cookie').click()
Expand All @@ -192,6 +228,31 @@ <h4 id="clearCookies"><a href="https://on.cypress.io/clearcookies">cy.clearCooki
<div class="col-xs-12"><hr></div>
</div>
</div>

<div id="clearAllCookies">
<div class="row">

<div class="col-xs-7">
<h4 id="clearAllCookies"><a href="https://on.cypress.io/clearallcookies">cy.clearAllCookies()</a></h4>
<p>To clear all browser cookies, use the <a href="https://on.cypress.io/clearallcookies"><code>cy.clearAllCookies()</code></a> command.</p>
<pre><code class="javascript">cy.getAllCookies().should('be.empty')

cy.setCookie('key', 'value')
cy.setCookie('key', 'value', { domain: 'example.com' })

cy.getAllCookies().should('have.length', 1)

// cy.clearAllCookies() yields null
cy.clearAllCookies()

cy.getAllCookies().should('be.empty')</code></pre>
</div>
<div class="col-xs-5">
</div>

<div class="col-xs-12"><hr></div>
</div>
</div>
</div>

<script src="/assets/js/vendor/jquery-1.12.0.min.js"></script>
Expand Down
45 changes: 43 additions & 2 deletions cypress/e2e/2-advanced-examples/cookies.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ context('Cookies', () => {
cy.getCookie('token').should('have.property', 'value', '123ABC')
})

it('cy.getCookies() - get browser cookies', () => {
it('cy.getCookies() - get browser cookies for the current domain', () => {
// https://on.cypress.io/getcookies
cy.getCookies().should('be.empty')

Expand All @@ -37,6 +37,32 @@ context('Cookies', () => {
})
})

it('cy.getAllCookies() - get all browser cookies', () => {
// https://on.cypress.io/getallcookies
cy.getAllCookies().should('be.empty')

cy.setCookie('key', 'value')
cy.setCookie('key', 'value', { domain: '.example.com' })

// cy.getAllCookies() yields an array of cookies
cy.getAllCookies().should('have.length', 2).should((cookies) => {
// each cookie has these properties
expect(cookies[0]).to.have.property('name', 'key')
expect(cookies[0]).to.have.property('value', 'value')
expect(cookies[0]).to.have.property('httpOnly', false)
expect(cookies[0]).to.have.property('secure', false)
expect(cookies[0]).to.have.property('domain')
expect(cookies[0]).to.have.property('path')

expect(cookies[1]).to.have.property('name', 'key')
expect(cookies[1]).to.have.property('value', 'value')
expect(cookies[1]).to.have.property('httpOnly', false)
expect(cookies[1]).to.have.property('secure', false)
expect(cookies[1]).to.have.property('domain', '.example.com')
expect(cookies[1]).to.have.property('path')
})
})

it('cy.setCookie() - set a browser cookie', () => {
// https://on.cypress.io/setcookie
cy.getCookies().should('be.empty')
Expand All @@ -61,7 +87,7 @@ context('Cookies', () => {
cy.getCookie('token').should('be.null')
})

it('cy.clearCookies() - clear browser cookies', () => {
it('cy.clearCookies() - clear browser cookies for the current domain', () => {
// https://on.cypress.io/clearcookies
cy.getCookies().should('be.empty')

Expand All @@ -74,4 +100,19 @@ context('Cookies', () => {

cy.getCookies().should('be.empty')
})

it('cy.clearAllCookies() - clear all browser cookies', () => {
// https://on.cypress.io/clearallcookies
cy.getAllCookies().should('be.empty')

cy.setCookie('key', 'value')
cy.setCookie('key', 'value', { domain: '.example.com' })

cy.getAllCookies().should('have.length', 2)

// cy.clearAllCookies() yields null
cy.clearAllCookies()

cy.getAllCookies().should('be.empty')
})
})
Loading

0 comments on commit f79751a

Please sign in to comment.