Skip to content

Commit

Permalink
Network emulation for Chrome
Browse files Browse the repository at this point in the history
  • Loading branch information
EtienneBruines committed Apr 3, 2020
1 parent 3b65143 commit f5e4a5a
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 0 deletions.
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.')
})
})

0 comments on commit f5e4a5a

Please sign in to comment.