How to get the all the name, URL and status code from console of a web page using devtools in selenium java

112 Views Asked by At

At present i use the below code in cucumber, I am able to print the url status code but since its lamda expression i am not able to format it porperly, i need the out put like below sample format is need is : Status = 304, Request url = xxxxxxxx, Failre = Network error Status = 200, Request url = xxxxxxxx, Failre = na . . .

...

package basicSanity;

import configurations.Constants;
import configurations.browserConfig;
import io.cucumber.java.en.*;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.devtools.DevTools;
import org.openqa.selenium.devtools.v116.network.Network;
import org.openqa.selenium.devtools.v116.network.model.Request;
import org.openqa.selenium.support.ui.WebDriverWait;
import utils.Utils;
import java.time.Duration;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import static org.openqa.selenium.devtools.v116.network.Network.loadingFailed;


public class SanityTests {

    public WebDriver driver;
    public WebDriverWait wait;
    public HomePage homePage;
    public Utils utils;
    public LoginPage loginPage;

    @When("HomePage is loaded")
    public void home_page_is_loaded() {
        driver = browserConfig.getDriver();
        wait = new WebDriverWait(driver, Duration.ofSeconds(20),Duration.ofSeconds(2));
        loginPage = new LoginPage(driver);
        loginPage.portal(Constants.AutoUser,Constants.AutoPwd);
        homePage = new HomePage(driver);

    }
    @Then("verify homePage has no errors")
    public void verify_home_page_has_no_errors() {

        DevTools devTools = ((ChromeDriver) driver).getDevTools();
        devTools.createSession();

        // Enable the network domain
        devTools.send(Network.enable(Optional.of(1000000), Optional.of(1000000), Optional.of(1000000)));

        devTools.addListener(Network.requestWillBeSent(),request ->{
            Request req = request.getRequest();
            System.out.println(req.getUrl());

        });

        devTools.addListener(Network.responseReceived(),response ->{
                System.out.println( "Status code = "+ response.getResponse().getStatus()/*+"  Status url = "+ response.getResponse().getUrl()*/);
        });

        devTools.addListener(loadingFailed(), loadingFailed -> {
            System.out.println(" | Error: " + loadingFailed.getErrorText());
        });

        driver.get("https://the-internet.herokuapp.com/notification_message_rendered");
        utils = new Utils(driver);
        utils.wait(8);


    }

}

'''

current output is like below im not abel to print the status code and error right next 
to the failed url :

Testing started at 11:51 AM ...
Browser is = chrome
SLF4J: No SLF4J providers were found.
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See https://www.slf4j.org/codes.html#noProviders for further details.
opening chrome
Dec 11, 2023 11:51:42 AM org.openqa.selenium.devtools.CdpVersionFinder findNearestMatch
WARNING: Unable to find an exact match for CDP version 119, so returning the closest version found: 116
https://the-internet.herokuapp.com/notification_message_rendered
Status code = 200
Status code = 200
https://the-internet.herokuapp.com/js/vendor/298279967.js
https://the-internet.herokuapp.com/css/app.css
https://the-internet.herokuapp.com/js/foundation/foundation.alerts.js
https://the-internet.herokuapp.com/css/font-awesome.css
https://the-internet.herokuapp.com/js/foundation/foundation.js
https://the-internet.herokuapp.com/js/vendor/jquery-1.11.3.min.js
https://the-internet.herokuapp.com/js/vendor/jquery-ui-1.11.4/jquery-ui.js
https://the-internet.herokuapp.com/img/forkme_right_green_007200.png
Status code = 200
Status code = 200
Status code = 200
Status code = 200
https://298279967.log.optimizely.com/event?a=298279967&d=298279967&y=false&n=https%3A%2F%2Fthe-internet.herokuapp.com%2Fnotification_message_rendered&u=oeu1702275730936r0.7314612437605936&wxhr=true&t=1702275730939&f=298349752,318188263
Status code = 200
Status code = 200
 | Error: net::ERR_EMPTY_RESPONSE
Status code = 200
Status code = 200
https://the-internet.herokuapp.com/favicon.ico
Status code = 404
In teardown method

1 Scenarios (1 passed)
2 Steps (2 passed)
0m41.070s
'''
1

There are 1 best solutions below

1
On

Try this:

ResourceRequest resource = request.getRequest();
System.out.println("Name: " + resource.getUrl());