4

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();

  }

}
Community
  • 1
  • 1

2 Answers2

0

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 {

       }
Muthukumaran
  • 126
  • 1
  • 4
0

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.

SiKing
  • 10,003
  • 10
  • 39
  • 90
  • Which version of serenity supporting this? as I am using version "2.0.30" and getting "Caused by: net.thucydides.core.webdriver.UnsupportedDriverException: Could not instantiate new WebDriver instance of type class org.openqa.selenium.chrome.ChromeDriver (java.lang.String cannot be cast to org.openqa.selenium.logging.LoggingPreferences" – qaWork4 Oct 22 '21 at 14:13
  • @qaWork4 Not sure what the version was that I created this in - you could check what was released when I made the above post. Anyway, version 2.3.12 still works. – SiKing Oct 22 '21 at 22:09
  • Now able to working with browser logs but working for performance when using like "chrome.capabilities.loggingPrefs = { performance : all }". Is there any possible way to enable performance log? – qaWork4 Oct 28 '21 at 06:44
  • @qaWork4 Have not tried. But you could start [here](https://chromedriver.chromium.org/logging/performance-log#h.p_ID_34). Note the comment "Performance logging is NOT enabled by default." But this sounds like an entirely new question. – SiKing Oct 28 '21 at 18:13