To evaluate this capability I went about automating the 3 scenarios found below,
- Intercepting and waiting for a network resource attached to a web page to be received to the client side.
- Intercepting and simulating 400 and 500 errors.
- Intercepting and changing response payloads.
Cypress Examples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <reference types='cypress'/> | |
describe("Intercepting network resources", ()=>{ | |
it("Intercepting and waiting for a URL", ()=>{ | |
cy.intercept({ | |
method: "GET", | |
url: "https://the-internet.herokuapp.com/css/app.css" | |
}).as("slowResource") | |
cy.visit("https://the-internet.herokuapp.com/dropdown"); | |
cy.wait("@slowResource").its('response.statusCode').should('eq', 200); | |
}) | |
it("Intercepting and throwing a network error for a resource", ()=> { | |
cy.intercept({ | |
method: "GET", | |
url: "https://the-internet.herokuapp.com/dropdown" | |
},{ | |
forceNetworkError: true | |
}).as("slowResource") | |
cy.visit("https://the-internet.herokuapp.com/dropdown"); | |
}) | |
it.skip("Intercepting and loging the request object", ()=>{ | |
cy.intercept({ | |
method: "GET", | |
url: "https://the-internet.herokuapp.com/dropdown" | |
}, (req) => { | |
console.log(req); | |
}).as("slowResource") | |
cy.visit("https://the-internet.herokuapp.com/dropdown"); | |
cy.wait("@slowResource").its('response.statusCode'); | |
}) | |
it("Intercepting and replying with a static response", ()=>{ | |
cy.intercept('GET','https://the-internet.herokuapp.com/dropdown', {statusCode: 200, body: "<html><body>test</body></html>"}).as("slowResource") | |
cy.visit("https://the-internet.herokuapp.com/dropdown"); | |
cy.wait("@slowResource").its('response.statusCode').should('eq', 200); | |
}) | |
}) |
Selenium Examples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.dumiduh.other; | |
import io.github.bonigarcia.wdm.WebDriverManager; | |
import org.openqa.selenium.chrome.ChromeDriver; | |
import org.openqa.selenium.devtools.NetworkInterceptor; | |
import org.openqa.selenium.remote.http.Contents; | |
import org.openqa.selenium.remote.http.HttpResponse; | |
import org.openqa.selenium.remote.http.Route; | |
import org.testng.annotations.AfterClass; | |
import org.testng.annotations.BeforeClass; | |
import org.testng.annotations.Test; | |
import static org.openqa.selenium.remote.http.HttpMethod.GET; | |
public class NetworkInterceptorTest { | |
ChromeDriver driver; | |
@BeforeClass | |
public void setup(){ | |
WebDriverManager.chromedriver().setup(); | |
} | |
@Test | |
public void responseInterceptionError() { | |
driver = new ChromeDriver(); | |
Route route = Route.matching(req -> GET == req.getMethod() && req.getUri() | |
.endsWith("/dropdown")) | |
.to(() -> req -> new HttpResponse().setStatus(500)); | |
new NetworkInterceptor(driver, route); | |
driver.get("https://the-internet.herokuapp.com/dropdown"); | |
driver.getPageSource() | |
.contains("HTTP ERROR 500"); | |
} | |
@Test | |
public void responseInterceptionBody() { | |
driver = new ChromeDriver(); | |
Route route = Route.matching(req -> GET == req.getMethod() && req.getUri() | |
.endsWith("/dropdown")) | |
.to(() -> req -> new HttpResponse().setContent(Contents.utf8String("<html><body>test</body></html>"))); | |
new NetworkInterceptor(driver, route); | |
driver.get("https://the-internet.herokuapp.com/dropdown"); | |
} | |
@AfterClass | |
public void cleanup(){ | |
driver.quit(); | |
} | |
} |