How to Set Logging Preferences for Chrome whilst using Serenity BDD?

Viewed 2024

I'm trying to extract the console log from Chrome using the initial set up requiring parameters being set up for chrome webdriver (from Capturing browser logs with Selenium) :

System.setProperty("webdriver.chrome.driver", "c:\\path\\to\\chromedriver.exe");        
DesiredCapabilities caps = DesiredCapabilities.chrome();
LoggingPreferences logPrefs = new LoggingPreferences();
logPrefs.enable(LogType.BROWSER, Level.ALL);
caps.setCapability(CapabilityType.LOGGING_PREFS, logPrefs);

How do i get it to work whilst having Serenity BDD managing my WebDriver using @Managed, from (net.thucydides.core.annotations.Managed) ?

I am using (JUnit + Selenium + Serenity BDD) and my current layout test for example is as follows:

@RunWith(SerenityRunner.class) 
public class UserLoginSuccessfulIT {

@Managed(driver="chrome")                              
WebDriver driver;

@Steps                                                                       
LoginSuccessfulSteps user;

  @Test
  public void user_logs_in_successfully() throws IOException{
     //GIVEN
     user.opens_loginpage();
     //WHEN
     user.userTypesUserPass("user", "pass");
     user.logs_in();
     //THEN
     user.sees_welcomepage();
     user.check_for_dead_links_from_file();

  }

}
2 Answers

You can have your own runner in serenity and instantiate driver with your desired capabilities for it. By doing so you can configure and redirect logs.

@RunWith(CucumberWithSerenity.class)
@CucumberOptions(
        plugin = {"pretty"},
        features = "<feature file path>",
        glue = {"<steps path as comma separated values"}
)
public class MyRunner {

       }

I needed exactly this today. Here is how I did it.

In my serenity.properties I added:

chrome.capabilities.loggingPrefs = { browser : all }

Based on information from here, here, and here.

Then I created a Serenity Task:

public class DumpLogs implements Task {

    private static final Logger log = LoggerFactory.getLogger(DumpLogs.class);

    public DumpLogs() {}

    public static DumpLogs all() {

        return Tasks.instrumented(DumpLogs.class);
    }

    @Override
    @Step("dump all browser logs")
    public <T extends Actor> void performAs(T actor) {

        WebDriver driver = BrowseTheWeb.as(actor).getDriver();
        LogEntries entries = driver.manage().logs().get(LogType.BROWSER);
        for (LogEntry entry : entries) {
            if (entry.getLevel().equals(Level.SEVERE))
                log.error(entry.getMessage());
            else if (entry.getLevel().equals(Level.WARNING))
                log.warn(entry.getMessage());
            else if (entry.getLevel().equals(Level.INFO))
                log.info(entry.getMessage());
            else
                log.debug(entry.getMessage());
        }
    }
}

I call this from my @After method, so that stuff gets logged at the end (pass or fail) of each test.

Rest of my test code is pretty much the same as your example.

Related