cypress-example-kitchensink
Version:
This is an example app used to showcase Cypress.io testing. For a full reference of our documentation, go to docs.cypress.io
190 lines (172 loc) • 7.3 kB
HTML
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="Kitchen Sink | Cypress Example">
<meta name="author" content="Cypress.io">
<meta name="copyright" content="Cypress.io Copyright (c) 2016">
<title>Cypress.io: Kitchen Sink</title>
<link rel="icon" href="/assets/img/favicon.ico" type="image/x-icon">
<link rel="stylesheet" href="/assets/css/vendor/bootstrap.min.css">
<link rel="stylesheet" href="/assets/css/vendor/fira.css">
<link rel="stylesheet" href="/assets/css/styles.css">
</head>
<body>
<nav class="navbar navbar-inverse">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="/">cypress.io</a>
</div>
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button">Commands <span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a href="/commands/querying">Querying</a></li>
<li><a href="/commands/traversal">Traversal</a></li>
<li><a href="/commands/actions">Actions</a></li>
<li><a href="/commands/window">Window</a></li>
<li><a href="/commands/viewport">Viewport</a></li>
<li><a href="/commands/location">Location</a></li>
<li><a href="/commands/navigation">Navigation</a></li>
<li><a href="/commands/assertions">Assertions</a></li>
<li><a href="/commands/misc">Misc</a></li>
<li><a href="/commands/connectors">Connectors</a></li>
<li><a href="/commands/aliasing">Aliasing</a></li>
<li><a href="/commands/waiting">Waiting</a></li>
<li><a href="/commands/network-requests">Network Requests</a></li>
<li><a href="/commands/fixtures">Fixtures</a></li>
<li><a href="/commands/local-storage">Local Storage</a></li>
<li><a href="/commands/cookies">Cookies</a></li>
</ul>
</li>
<li><a href="/utilities">Utilities</a></li>
<li>
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button">Cypress API <span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a href="/cypress-api/config">config</a></li>
<li><a href="/cypress-api/env">env</a></li>
<li><a href="/cypress-api/commands">Commands</a></li>
<li><a href="/cypress-api/cookies">Cookies</a></li>
<li><a href="/cypress-api/dom">Dom</a></li>
<li><a href="/cypress-api/server">Server</a></li>
</ul>
</ul>
</div>
</div>
</nav>
<div class="banner">
<div class="container">
<h1>Network Requests</h1>
<p>Examples of handling AJAX or XHR requests in Cypress, for a full reference of commands, go to <a href="https://on.cypress.io/api" target="_blank">docs.cypress.io</a>
</p>
</div>
</div>
<div class="container content-container">
<div id="network-requests">
<div class="row">
<div class="col-xs-7">
<h4>cy.server()</h4>
<p>To control the behavior of network requests and responses, use the <a href="https://on.cypress.io/api/server"><code>cy.server()</code></a> command.</p>
<pre><code class="javascript">cy.server().then(function(server){
// the default options on server
// you can override any of these options
expect(server.delay).to.eq(0)
expect(server.method).to.eq('GET')
expect(server.status).to.eq(200)
expect(server.headers).to.be.null
expect(server.response).to.be.null
expect(server.onRequest).to.be.undefined
expect(server.onResponse).to.be.undefined
expect(server.onAbort).to.be.undefined
// These options control the server behavior
// affecting all requests
expect(server.enable).to.be.true
expect(server.force404).to.be.false
expect(server.whitelist).to.be.a('function')
})
cy
.server({
method: "POST",
delay: 1000,
status: 422,
response: {}
})</code></pre>
</div>
<div class="col-xs-5">
</div>
<div class="col-xs-12"><hr></div>
<div class="col-xs-7">
<h4>cy.request()</h4>
<p>To make an XHR request, use the <a href="https://on.cypress.io/api/request"><code>cy.request()</code></a> command.</p>
<pre><code class="javascript">cy
.request('http://jsonplaceholder.typicode.com/comments').then(function(response){
expect(response.status).to.eq(200)
expect(response.body).to.have.length(500)
expect(response).to.have.property('headers')
expect(response).to.have.property('duration')
})</code></pre>
</div>
<div class="col-xs-5">
</div>
<div class="col-xs-12"><hr></div>
<div class="col-xs-7">
<h4>cy.route()</h4>
<p>To route responses to matching requests, use the <a href="https://on.cypress.io/api/route"><code>cy.route()</code></a> command.</p>
<pre><code class="javascript">cy
.server()
.route(/comments\/1/).as('getComment')
.get('.network-btn').click()
// Wait for a specific resource to resolve
// continuing to the next command
.wait('@getComment').its('status').should('eq', 200)
// Specify the route to listen to method 'POST'
.route('POST', '/comments').as('postComment')
.get('.network-post').click()
.wait('@postComment')
.get('@postComment').then(function(xhr){
expect(xhr.requestBody).to.include('email')
expect(xhr.requestHeaders).to.have.property('Content-Type')
expect(xhr.responseBody).to.have.property('name', 'Using POST in cy.route()')
})
message = 'whoa, this comment doesn\'t exist'
// Stubbed PUT comment route
cy
.route({
method: 'PUT',
url: /comments\/\d+/,
status: 404,
response: {error: message},
delay: 500
}).as('putComment')
.get('.network-put').click()
.wait('@putComment')
.get('.network-put-comment').should('contain', message)
</code></pre>
</div>
<div class="col-xs-5">
<div class="well">
<button class="network-btn btn btn-primary">Get Comment</button>
<div class="network-comment"></div>
<button class="network-post btn btn-success">Post Comment</button>
<div class="network-post-comment"></div>
<button class="network-put btn btn-warning">Update Comment</button>
<div class="network-put-comment"></div>
</div>
</div>
<div class="col-xs-12"><hr></div>
</div>
</div>
</div>
<script src="/assets/js/vendor/jquery-1.12.0.min.js"></script>
<script src="/assets/js/vendor/bootstrap.min.js"></script>
<script src="/assets/js/vendor/highlight.pack.js"></script>
<script src="/assets/js/scripts.js"></script>
</body>
</html>