如何透過 Firefox logging 在 Selenium 中獲取


執行測試完成後,將生成日誌記錄,因為 Firefox 登入使用 geckodriver 登入。Firefox 生成的日誌記錄可透過設定某些引數停用。

我們可以停止這些日誌記錄在控制檯中記錄並將其捕獲到一個不同的檔案中。藉助 System.setProperty 方法可以實現這一目標。在上圖中,我們可以在控制 臺中看到生成的 geckodriver 日誌記錄。

語法

System.setProperty(FirefoxDriver.SystemProperty.DRIVER_USE_MARIONETTE, "true"); // turning off logs

System.setProperty(FirefoxDriver.SystemProperty.BROWSER_LOGFILE, "<path of file>"); // record logs in another file

示例

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import java.util.concurrent.TimeUnit;
public class LogsDisable{
   public static void main(String[] args) {
      System.setProperty("webdriver.gecko.driver",
      "C:\Users\ghs6kor\Desktop\Java\geckodriver.exe");

      //logging disable
      System.setProperty
      (FirefoxDriver.SystemProperty.DRIVER_USE_MARIONETTE, "true");

      //record log in Firefoxlogs.txt file
      System.setProperty(FirefoxDriver.SystemProperty.BROWSER_LOGFILE,
      "C:\Users\ghs6kor\Desktop\DebomitaJava\Logs\Firefoxlogs.txt");
      WebDriver driver = new FirefoxDriver();

      //implicit wait
      driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);

      //URL launch
      driver.get("https://tutorialspoint.tw/about/about_careers.htm");
      System.out.println("Page loaded");
      driver.quit();
   }
}

輸出

Firefoxlogs.txt 檔案

檔案內容

更新於: 2022 年 2 月 8 日

2K+ 次瀏覽

助你開啟 職業生涯

完成課程,獲得認證

開始
廣告