Pages

Selenium4 Network Tab,Console,PerformanceMetrics Methods

 

Intercepting HTTP Response

Fore intercepting the response we will be using Network.responseReceived event. This fired when the HTTP response is available, with this we can listen to URL, response headers, response code, etc. To get the response body use the method Network.getResponseBody

 public static void validateResponse(String applicationfractionUrl) {

System.out.println("**********Getting Network Tab Response Details****************");

        final RequestId[] requestIds = new RequestId[1];

        devTools.send(Network.enable(Optional.of(100000000), Optional.empty(), Optional.empty()));

        devTools.addListener(Network.responseReceived(), responseReceived -> {

        if (responseReceived.getResponse().getUrl().contains(applicationfractionUrl)) {

                System.out.println("URL: " + responseReceived.getResponse().getUrl());

                System.out.println("Status: " + responseReceived.getResponse().getStatus());

                System.out.println("Type: " + responseReceived.getType().toJson());

                responseReceived.getResponse().getHeaders().toJson().forEach((k, v) -> System.out.println((k + ":" + v)));

                requestIds[0] = responseReceived.getRequestId();

                System.out.println("Response Body: \n" + devTools.send(Network.getResponseBody(requestIds[0])).getBody() + "\n");

        }

        });

        System.out.println("--------------end of response-----------------------------------");   

}

=============================================================

Capture Console Logs

When the application is opened, the console error logs published by the application are captured.

 public static void consoleLogs(){

System.out.println("************Getting Console Logs****************");

        devTools.send(Log.enable());


        devTools.addListener(Log.entryAdded(),


                logEntry -> {


                    System.out.println("log: "+logEntry.getText());


                    System.out.println("level: "+logEntry.getLevel());


                });


        System.out.println("--------------end of Console logs-----------------------------------");   

    }

After fetching logs disable and clear the logs

devTools.send(Log.clear()); devTools.send(Log.disable()); -------------------------------------------------------------------------------------------

Performance Metrics

With DevTools we can get performance metrics while running automation tests.

 public static void getPerformanceMetrics(){

System.out.println("************Getting Performance Metrics****************");

       devTools.send(Performance.enable(Optional.empty()));

       List<Metric> metrics = devTools.send(Performance.getMetrics());

       metrics.forEach(metric-> System.out.println(metric.getName() +" : "+ metric.getValue()));

       /**

        * for(Metric m : metricList) {

        System.out.println(m.getName() + " = " + m.getValue());

}

        */

       System.out.println("--------------end of performnce Metrics-----------------------------------");   

}   

==========================================================================================

Validating Cookies

With chrome dev tools API we can play around with browser cookies. We can clear browser cookies, delete all cookies, get and set cookie or cookies.

public void getAllCookies() {

devTools.send(Network.enable(Optional.empty(), Optional.empty(), Optional.empty()));

          //Getting all cookies

        List<Cookie> cookies = devTools.send(Network.getAllCookies());

        cookies.forEach(cookie -> System.out.println(cookie.getName()));

        List<String> cookieName = cookies.stream().map(cookie -> cookie.getName()).sorted().collect(Collectors.toList());

        Set<org.openqa.selenium.Cookie> seleniumCookie = driver.manage().getCookies();

        List<String> selCookieName = seleniumCookie.stream().map(selCookie -> selCookie.getName()).sorted().collect(Collectors.toList());

        Assert.assertEquals(cookieName, selCookieName);


        //Clearing browser cookies

        devTools.send(Network.clearBrowserCookies());

        List<Cookie> cookiesAfterClearing = devTools.send(Network.getAllCookies());

        Assert.assertTrue(cookiesAfterClearing.isEmpty());

    }

=======================================================================================


Java-Spring-can you explain me spring & springboot differences

 can you explain me spring & springboot differences:

Spring and Spring Boot are both frameworks for building Java applications. However, there are some key differences between the two:

  1. Purpose: Spring is a comprehensive framework for building Java applications, including support for web applications, data access, security, and many other features. Spring Boot, on the other hand, is a framework for building production-ready applications quickly and easily, with minimal setup and configuration.

  2. Configuration: Spring requires a significant amount of configuration to get started, which can be time-consuming and complex. With Spring Boot, the framework automatically configures itself based on the dependencies included in the project, which makes getting started much easier and faster.

  3. Default Setup: Spring provides a lot of flexibility in how you structure and build your application, but this also means that you need to configure a lot of different components to get started. Spring Boot provides a default setup with sensible defaults, so you can start building your application right away.

  4. Third-party Libraries: Spring provides support for a wide range of third-party libraries, but requires you to configure and integrate these libraries into your application. With Spring Boot, these libraries are automatically integrated into the framework, so you can start using them right away.

  5. Deployment: Spring applications can be deployed to any Java application server, but the deployment process can be complex and time-consuming. Spring Boot applications, on the other hand, can be packaged as executable JAR files, making deployment much simpler and faster.

  6. In summary, Spring Boot is a way to simplify the development and deployment of Spring applications. If you need a fast, easy-to-use framework for building Java applications, with minimal setup and configuration, then Spring Boot may be a good choice for you. But if you need a more flexible and customizable framework, with a wider range of features and options, then Spring may be a better choice.

java-streams-by-examples

Java Streams API

Java Streams