Kev
Kev

Reputation: 306

Selenium C# GetLog returns NullReferenceException

I have dusted off some old code with I know used to work but no longer appears to function. Namely

var browserLogs = Driver.Manage().Logs.GetLog(LogType.Browser);

This used to return any console log entries but now I get the following enter image description here

I last used this code about 2 years ago so my questions are:

What has changed in Chrome? What do I need to change on my code to get this working again?

I am using Chrome 85.x etc with matching ChromeDriver, C# and Selenium. Driver is correctly initialised and a valid web page has rendered. Also I have this as in my driver options

options.SetLoggingPreference(LogType.Browser, LogLevel.All);

Any ideas folks?

More code below

        public static void BeforeFeature(int server, string title)
    {
        if (server == 1) ResetReportVariables(TestUrls.DomainLive, title);
        if (server == 2) ResetReportVariables(TestUrls.Domain, title);
        var options = new ChromeOptions();
        options.AddArguments("disable-browser-side-navigation");
        options.AddArguments("disable-infobars");
        options.AddArgument("ignore-certificate-errors");
        options.AddArgument("ignore-ssl-errors");
        options.AddArgument("disable-popup-blocking");
        options.AddArguments("start-maximized");
        options.AddArguments("no-sandbox");
        options.SetLoggingPreference(LogType.Browser, LogLevel.All);
        Driver = new ChromeDriver(options);
        Driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(TestValues.DelayShort);
        Driver.Manage().Timeouts().AsynchronousJavaScript = TimeSpan.FromSeconds(TestValues.DelayShort);
        Driver.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(TestValues.DelayShort);
        Driver.Manage().Cookies.DeleteAllCookies();
    }


Scenario Outline: Validate the following pages on live site
Given that I browse to "<url>" page on "1" server
Then  no console errors were detected

Examples:
  | url             |
  | /               |


        [Given(@"that I browse to ""(.*)"" page on ""(.*)"" server")]
    public void GivenThatIBrowseToPageOnServer(string url, int server)
    {
        Visit(url, server);
    }


    [Then(@"no console errors were detected")]
    public void ThenNoConsoleErrorsWereDetected()
    {
        ValidateTheConsoleResults();
    }

        protected void Visit(string ext, int server)
    {
        WriteToReport(GetTheCurrentMethod());
        if (server == 1)
            ThisUrl = LiveUrl;
        else
            ThisUrl = PageUrl;

        WriteToReport("Load page " + ThisUrl);
        Driver.Navigate().GoToUrl(ThisUrl + ext);
        WebDriverWait wait = new WebDriverWait(Driver, TimeSpan.FromSeconds(10));
        wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.UrlContains(ThisUrl));
    }


        protected void ValidateTheConsoleResults()
    {
        WriteToReport(GetTheCurrentMethod());
        Visit();
        var errors = 0;
        //now we check the logs for errors
        var browserLogs = Driver.Manage().Logs.GetLog(LogType.Browser);
        if (browserLogs.Count > 0)
        {
            foreach (var log in browserLogs)
            {
                if (log.Level.Equals(LogLevel.Warning))
                {
                    WriteToReport("Logged Warning - " + log);
                    continue;
                }
                else
                {
                    WriteToReport("Logged Error - " + log);
                    errors++;
                }
            }
        }
        if (errors != 0) AssertFalse(errors + " Console errors detected");
        else AssertTrue("No console errors detected");
    }

Upvotes: 0

Views: 610

Answers (1)

Kev
Kev

Reputation: 306

@JimEvans you are a star That solved it, many thanks

Upvotes: 1

Related Questions