How to obtain native logger in Selenium WebDriver

后端 未结 4 1541
悲&欢浪女
悲&欢浪女 2020-12-02 15:49

Is it possible to obtain the logger somehow that Selenium WebDriver uses? I want to capture a transcript of all the commands that were issued (eg: open, wait, click, etc). I

相关标签:
4条回答
  • 2020-12-02 16:25

    Try

    driver.manage().logs()
    

    You will get Logs interface that has methods to get logs and log types. See Logs interface docs

    0 讨论(0)
  • 2020-12-02 16:32

    Enable logging in the driver you're using, select which log types you're interested in and the log level (I'm using FirefoxDriver, enabling all types of logs and collecting all log messages)

    LoggingPreferences logs = new LoggingPreferences();
    logs.enable(LogType.BROWSER, Level.ALL);
    logs.enable(LogType.CLIENT, Level.ALL);
    logs.enable(LogType.DRIVER, Level.ALL);
    logs.enable(LogType.PERFORMANCE, Level.ALL);
    logs.enable(LogType.PROFILER, Level.ALL);
    logs.enable(LogType.SERVER, Level.ALL);
    
    DesiredCapabilities desiredCapabilities = DesiredCapabilities.firefox();
    desiredCapabilities.setCapability(CapabilityType.LOGGING_PREFS, logs);
    
    WebDriver driver = new FirefoxDriver(desiredCapabilities);
    

    Then, after running the test you can collect the logs (I'm only collecting the DRIVER logs, but you can do the same for any type of log)

    Logs logs = driver.manage().logs();
    LogEntries logEntries = logs.get(LogType.DRIVER);
    
    for (LogEntry logEntry : logEntries) {
        System.out.println(logEntry.getMessage());
    }
    
    0 讨论(0)
  • 2020-12-02 16:37

    I'm using log4j for logging as utils logger is the most easiest and straight forward one that, that can be used (IMHO).

    POM dependencies:

    <dependency>
              <groupId> org.apache.cassandra</groupId>
              <artifactId>cassandra-all</artifactId>
              <version>0.8.1</version>
          </dependency>
    
          <dependency>
              <groupId>org.slf4j</groupId>
              <artifactId>slf4j-api</artifactId>
              <version>1.6.6</version>
          </dependency>
          <dependency>
              <groupId>org.slf4j</groupId>
              <artifactId>jcl-over-slf4j</artifactId>
              <version>1.6.6</version>
              <scope>runtime</scope>
          </dependency>
          <dependency>
              <groupId>org.slf4j</groupId>
              <artifactId>jul-to-slf4j</artifactId>
              <version>1.6.6</version>
              <scope>runtime</scope>
          </dependency>
          <dependency>
              <groupId>org.slf4j</groupId>
              <artifactId>log4j-over-slf4j</artifactId>
              <version>1.6.6</version>
              <scope>runtime</scope>
          </dependency>
    

    imports are following:

    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    

    usage:

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

    and then just use it as such:

    logger.info ("butonCLick");
    driver.findElement(By.id("blablabla")).click();
    

    Hope this works for you.

    0 讨论(0)
  • 2020-12-02 16:38

    Try this -

    import logging
    logging.basicConfig(filename = log_filename, level = logging.DEBUG)
    

    Reference - Selenium unit tests in Python -- where is my log file?

    0 讨论(0)
提交回复
热议问题