[英]How can we get Network tab information (Header, Response, etc) using Selenium
[英]Can we use Selenium or any other jars to get network response and validate
我在這里有一個要求,我需要使用 selenium 和 java 驗證網絡響應值。
如果有任何代碼可以幫助我,那就太好了。
我要執行的操作(需要自動化)是
加載應用程序https://www.ancestry.com
加載應用程序時按 F12 並轉到網絡選項卡
選擇包含 b/ss 的相應網絡
單擊它並獲取查詢參數中的值
我在下圖中突出顯示了它們
注意:我需要為應用程序加載后發生的每個點擊事件執行此操作。
這在硒中可能嗎
這是我試圖記錄值的代碼我做得對嗎請幫忙
public static void main(String[] args) throws IOException {
System.setProperty("webdriver.chrome.driver",
new File(".").getCanonicalPath() + "\\WebDriver\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://www.ancestry.com");
//capture(driver);
driver.quit();
}
public static void capture(WebDriver driver) throws IOException {
OutputStream logfile = new FileOutputStream(new File(new
File(".").getCanonicalPath() + "\\log.txt"), true);
PrintStream printlog = new PrintStream(logfile);
LogEntries logs = driver.manage().logs().get(LogType.PERFORMANCE);
for (LogEntry entry : logs) {
if (entry.toString().contains("\"type\":\"Img\""))
System.out.println(entry.toString());
printlog.append(new Date(entry.getTimestamp()) + " " + entry.toString() + " "
+ System.getProperty("line.separator"));
printlog.append(System.getProperty("line.separator"));
printlog.append(System.getProperty("line.separator"));
}
printlog.close();
}
我們可以使用瀏覽器 mob 代理獲取所有網絡日志
private static BrowserMobProxy proxy = new BrowserMobProxyServer();
public webLib(String browser) {
logReport.logMethodStart("Launch Application");
Proxy seleniumProxy = null;
proxy.start();
seleniumProxy = ClientUtil.createSeleniumProxy(proxy);
if (browser.equalsIgnoreCase("chrome")) {
try {
System.setProperty("webdriver.chrome.driver",
new File(".").getCanonicalFile() + seperator + "WebDriver" + seperator + "chromeDriver.exe");
} catch (IOException e) {
e.printStackTrace();
}
ChromeOptions options = new ChromeOptions();
options.setCapability(CapabilityType.PROXY, seleniumProxy);
options.setAcceptInsecureCerts(true);
options.addArguments("--ignore-certificate-errors");
options.addArguments("--disable-popup-blocking");
options.addArguments("--no-sandbox");
options.addArguments("--no-sandbox");
driver = new ChromeDriver(options);
} else if (browser.equalsIgnoreCase("firefox")) {
}
driver.manage().deleteAllCookies();
driver.manage().window().maximize();
driver.manage().timeouts().pageLoadTimeout(20, TimeUnit.SECONDS);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
if (driver != null) {
logReport.logStep(Status.PASS, "Browser launched successfully", driver);
}
}
您可以參考以下站點https://www.swtestacademy.com/webdriver-browsermob-proxy/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.