我是 Webproxy 的新手,所以需要专家的一些指导!我正在使用 Selenium 远程 Web 驱动程序在 Saucelabs 中运行我的 UI 自动化测试。当客户端浏览器(在 Saucelabs 中)尝试打开 URL“https://abc.xyz.com/login”时,它会发出 XHR 请求以从服务器获取 JSON 文件内容。然后,它会根据此 JSON 文件中的值显示 UI 元素。JSON文件看起来像这样..
{
"Login.Company": "Company",
"Login.Username": "User name",
"Login.Password": "Password"
}
我希望在 XHR 响应被客户端浏览器处理之前修改这些键的值,以便客户端会看到类似这样的内容......
{
"Login.Company": "NewCompany",
"Login.Username": "NewUser name",
"Login.Password": "NewPassword"
}
我的代码看起来像这样......
public class WebProxyTests {
Proxy proxy;
BrowserMobProxyServer proxyServer;
private Proxy proxyConfig;
WebDriver driver;
@BeforeClass
void setup() {
proxyServer = new BrowserMobProxyServer();
proxyServer.start();
// capture content as a HAR (HTTP Archive)
// to process when test is complete
proxyServer.enableHarCaptureTypes(CaptureType.REQUEST_CONTENT,
CaptureType.RESPONSE_CONTENT);
proxyServer.newHar();
proxyConfig = ClientUtil.createSeleniumProxy(proxyServer);
FirefoxOptions options = new FirefoxOptions();
options.setProxy(proxyConfig);
System.setProperty("webdriver.gecko.driver", "C:\\Selenium\\webdrivers\\geckodriver.exe");
driver = new FirefoxDriver(options);
}
@Test
public void captureTraffic() throws InterruptedException {
driver.get("https://abc.xyz.com/login");
final Har httpMessages = proxyServer.getHar();
for(HarEntry httpMessage : httpMessages.getLog().getEntries()){
String url = httpMessage.getRequest().getUrl();
if (url.contains("en-us.json")) {
// Get the response data from XHR request
HarContent data = httpMessage.getResponse().getContent();
if (data != null) {
System.out.println("DATA>>>>>>>>>" + data.getText());
}
}
}
}
@AfterClass
/***
*
*/
void teardown() {
driver.quit();
proxyServer.abort();
}
有人对我将如何做这件事有任何建议吗?任何代码示例真的会受到赞赏吗?