Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enabling "Preserve Log" in Chrome Developer Tools Console via Selenium #304

Open
PalakAShah opened this issue Dec 5, 2023 · 0 comments
Open

Comments

@PalakAShah
Copy link

PalakAShah commented Dec 5, 2023

Hello Chrome Community,
I hope this message finds you well. I am currently working on a Selenium automation project where I am using ChromeDriver to interact with the Chrome browser. One specific requirement I have is to enable the "Preserve Log" option in the Chrome Developer Tools Console via Selenium.

So basically, I want that when I launch the driver, it automatically have Preserve Logs - console settings enabled.

I want to ensure that the entire test execution's console logs are captured and saved when the Chrome driver is launched. This means that even if I navigate to different pages during the test, the logs should not be cleared. At the end of the test, I should be able to access and review the complete set of logs for the entire test run.

I have tried various approaches, including using the AddUserProfilePreference method in ChromeOptions and executing JavaScript through IJavaScriptExecutor. Despite my attempts, I have not been successful in programmatically enabling "Preserve Log" in the Console tab.
Here is a snippet of the code I am currently using:

​private static IWebDriver GetChromeDriver(string userAgent)
{
    var options = new ChromeOptions();
    //options.AddArgument("incognito");
    options.AddArguments("disable-infobars");
    // options.AddArguments("--headless");

    //Session 0 limit - 1024 x 768
    options.AddArguments("--window-size=1024,768");
    options.AddArguments("--window-position=0,0");
    options.AddArgument($"--user-agent= {userAgent}");
    options.SetLoggingPreference(LogType.Browser, LogLevel.All);

    //To disable PDF viewer plugins
    options.AddUserProfilePreference("plugins.always_open_pdf_externally", true);

    options.AddExtension(@"C:\Users\PalakS\Downloads\ezyZip - Copy.crx");

    // Disable download bubble and download bubble v2 using command-line switch - For Auto Download
    options.AddArgument("--disable-features=DownloadBubble");
    options.AddArgument("--disable-features=DownloadBubbleV2");

    
    options.AddArgument("--disable-backgrounding-occluded-windows");

  

    var driver = new ChromeDriver(Directory.GetCurrentDirectory(), options, TimeSpan.FromMinutes(3));

    var devToolsSession = driver.GetDevToolsSession();

    IDevTools devTools = driver as IDevTools;
    DevToolsSession session = devTools.GetDevToolsSession();

    // Enable the Page domain
    var enablePageCommandSettings1 = new OpenQA.Selenium.DevTools.V119.Page.EnableCommandSettings();
    session.SendCommand(enablePageCommandSettings1);

    // Enable the Console domain
    var enableConsoleCommand = new OpenQA.Selenium.DevTools.V119.Console.EnableCommandSettings();
    session.SendCommand(enableConsoleCommand);

    // Create the script to preserve console logs
    string script = "console.preserveLog();";

    // Inject the script into every new document
    session.SendCommand(new OpenQA.Selenium.DevTools.V119.Page.AddScriptToEvaluateOnNewDocumentCommandSettings()
    {
        Source = script
    });

    // Inject the script into every new document
    session.SendCommand(new OpenQA.Selenium.DevTools.V119.Page.AddScriptToEvaluateOnLoadCommandSettings()
    {
        ScriptSource = script
    });

    return driver;
}

Unfortunately, the above approaches haven't yielded the desired results, and the "Preserve Log" option in the Console tab remains unchecked.
I would greatly appreciate any guidance, insights, or alternative approaches that the community might have in achieving this. If there's a specific preference, command-line argument, or technique that I might be overlooking, please let me know.
Thank you in advance for your time and assistance!
Environment Details:

Chrome Version: latest 119
Selenium Version: Selenium 4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant