Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions packages/driver/src/cy/commands/network.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module.exports = (Commands, Cypress) => {
Commands.addAll({
network: (options = {}) => {
Cypress.automation('remote:debugger:protocol', {
command: 'Network.enable',
})

Cypress.automation('remote:debugger:protocol', {
command: 'Network.emulateNetworkConditions',
params: {
offline: options.offline,
'latency': 0,
'downloadThroughput': 0,
'uploadThroughput': 0,
'connectionType': 'none',
},
})
},
})
}
1 change: 1 addition & 0 deletions packages/driver/src/cypress/commands.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ builtInCommands = [
require("../cy/commands/local_storage")
require("../cy/commands/location")
require("../cy/commands/misc")
require("../cy/commands/network")
require("../cy/commands/popups")
require("../cy/commands/navigation")
require("../cy/commands/querying")
Expand Down
16 changes: 16 additions & 0 deletions packages/driver/test/cypress/fixtures/network.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!DOCTYPE html>
<html>
<head>
<title>Network Fixture</title>
</head>
<body>
<p>
We are currently <span id="network-status"></span>.
</p>
<script>
window.addEventListener('offline', function () { document.getElementById('network-status').innerText = 'offline' })
window.addEventListener('online', function () {document.getElementById('network-status').innerText = 'online' })
document.getElementById('network-status').innerText = window.navigator.onLine ? 'online' : 'offline'
</script>
</body>
</html>
13 changes: 13 additions & 0 deletions packages/driver/test/cypress/integration/commands/network_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
describe('src/cy/commands/network', () => {
before(() => {
cy.visit('/fixtures/network.html')
})

it('switches between offline and online', () => {
cy.contains('We are currently online.')
cy.network({ offline: true })
cy.contains('We are currently offline.')
cy.network({ offline: false })
cy.contains('We are currently online.')
})
})