简体   繁体   English

我们可以使用 Selenium 或任何其他 jar 来获取网络响应并进行验证吗?

[英]Can we use Selenium or any other jars to get network response and validate

I have a requirement here that I need to verify the network response values using selenium & java.我在这里有一个要求,我需要使用 selenium 和 java 验证网络响应值。

It would be great if there is any code that could help me out.如果有任何代码可以帮助我,那就太好了。

The action that I want to perform (which is needed to be automated) is我要执行的操作(需要自动化)是

  1. load the application https://www.ancestry.com加载应用程序https://www.ancestry.com

  2. press F12 and go to network tab when application is loading加载应用程序时按 F12 并转到网络选项卡

  3. select a respective network containing b/ss选择包含 b/ss 的相应网络

  4. click on it and get the values in the query parameters单击它并获取查询参数中的值

I have highlighted them in the below image我在下图中突出显示了它们

Note: I need to perform this action for every click event that happen after the application is loaded.注意:我需要为应用程序加载后发生的每个点击事件执行此操作。

Is this possible in selenium这在硒中可能吗

网络请求

网络请求

This is the code that I tried to get the values recorded am I doing it right please help这是我试图记录值的代码我做得对吗请帮忙

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();
}

We can get all the network logs using browser mob proxy我们可以使用浏览器 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);
        }

    }

You can reffer the following site https://www.swtestacademy.com/webdriver-browsermob-proxy/您可以参考以下站点https://www.swtestacademy.com/webdriver-browsermob-proxy/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我们如何使用Selenium获取“网络”选项卡信息(标题,响应等) - How can we get Network tab information (Header, Response, etc) using Selenium 我们可以在soapui中使用apache poi罐吗? - Can we use apache poi jars in soapui? 我们可以在单独的服务中而不是在liferay中使用Liferay罐吗? - Can we use Liferay jars in standalone services not inside liferay? 如何使用Ivy获取JAR以外的工件? - How to use Ivy to get artifacts other than JARs? 我们可以在 Selenium WebDriver 中使用抽象类吗? - Can we use Abstract Class in Selenium WebDriver? 在springboot中发送响应时,我们是否有任何注释来验证响应属性/字段不是null? - Do we have any annotations to validate the response attribute/fields are not null while sending the response in springboot? 是否可以使用 JSCover 或任何其他工具来获得在浏览器中运行 Java Selenium WebDriver 测试的 JavaScript 代码覆盖率? - Is it possible to use JSCover or any other tool to get JavaScript code coverage running Java Selenium WebDriver tests in Browser? 我们如何使用XStream或任何其他解析器以以下格式获取json? - How can we get json in below format using XStream or any other parser? 使用IVY获得Selenium独立罐的方法 - Way to get Selenium standalone jars using IVY 我们如何在构建路径中用jar执行-javaagent? - How can we execute -javaagent with jars in the buildpath?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM