- Selenium 教程
- Selenium - 首頁
- Selenium - 概述
- Selenium - 元件
- Selenium - 自動化測試
- Selenium - 環境搭建
- Selenium - 遠端控制
- Selenium IDE 教程
- Selenium - IDE 簡介
- Selenium - 特性
- Selenium - 限制
- Selenium - 安裝
- Selenium - 建立測試
- Selenium - 建立指令碼
- Selenium - 控制流
- Selenium - 儲存變數
- Selenium - 警報和彈出視窗
- Selenium - Selenese 命令
- Selenium - 操作命令
- Selenium - 訪問器命令
- Selenium - 斷言命令
- Selenium - 斷言/驗證方法
- Selenium - 定位策略
- Selenium - 指令碼除錯
- Selenium - 驗證點
- Selenium - 模式匹配
- Selenium - JSON 資料檔案
- Selenium - 瀏覽器執行
- Selenium - 使用者擴充套件
- Selenium - 程式碼匯出
- Selenium - 程式碼輸出
- Selenium - JavaScript 函式
- Selenium - 外掛
- Selenium WebDriver 教程
- Selenium - 簡介
- Selenium WebDriver vs RC
- Selenium - 安裝
- Selenium - 第一個測試指令碼
- Selenium - 驅動程式會話
- Selenium - 瀏覽器選項
- Selenium - Chrome 選項
- Selenium - Edge 選項
- Selenium - Firefox 選項
- Selenium - Safari 選項
- Selenium - 雙擊
- Selenium - 右擊
- Python 中的 HTML 報告
- 處理編輯框
- Selenium - 單個元素
- Selenium - 多個元素
- Selenium Web 元素
- Selenium - 檔案上傳
- Selenium - 定位器策略
- Selenium - 相對定位器
- Selenium - 查詢器
- Selenium - 查詢所有連結
- Selenium - 使用者互動
- Selenium - WebElement 命令
- Selenium - 瀏覽器互動
- Selenium - 瀏覽器命令
- Selenium - 瀏覽器導航
- Selenium - 警報和彈出視窗
- Selenium - 處理表單
- Selenium - 視窗和選項卡
- Selenium - 處理連結
- Selenium - 輸入框
- Selenium - 單選按鈕
- Selenium - 複選框
- Selenium - 下拉框
- Selenium - 處理 IFrame
- Selenium - 處理 Cookie
- Selenium - 日期時間選擇器
- Selenium - 動態 Web 表格
- Selenium - Actions 類
- Selenium - Action 類
- Selenium - 鍵盤事件
- Selenium - 鍵上/下
- Selenium - 複製和貼上
- Selenium - 處理特殊鍵
- Selenium - 滑鼠事件
- Selenium - 拖放
- Selenium - 筆事件
- Selenium - 滾動操作
- Selenium - 等待策略
- Selenium - 顯式/隱式等待
- Selenium - 支援功能
- Selenium - 多選
- Selenium - 等待支援
- Selenium - 選擇支援
- Selenium - 顏色支援
- Selenium - ThreadGuard
- Selenium - 錯誤和日誌記錄
- Selenium - 異常處理
- Selenium - 其他
- Selenium - 處理 Ajax 呼叫
- Selenium - JSON 資料檔案
- Selenium - CSV 資料檔案
- Selenium - Excel 資料檔案
- Selenium - 跨瀏覽器測試
- Selenium - 多瀏覽器測試
- Selenium - 多視窗測試
- Selenium - JavaScript 執行器
- Selenium - 無頭執行
- Selenium - 捕獲螢幕截圖
- Selenium - 捕獲影片
- Selenium - 頁面物件模型
- Selenium - 頁面工廠
- Selenium - 記錄和回放
- Selenium - 框架
- Selenium - 瀏覽上下文
- Selenium - DevTools
- Selenium Grid 教程
- Selenium - 概述
- Selenium - 架構
- Selenium - 元件
- Selenium - 配置
- Selenium - 建立測試指令碼
- Selenium - 測試執行
- Selenium - 端點
- Selenium - 自定義節點
- Selenium 報告工具
- Selenium - 報告工具
- Selenium - TestNG
- Selenium - JUnit
- Selenium - Allure
- Selenium & 其他技術
- Selenium - Java 教程
- Selenium - Python 教程
- Selenium - C# 教程
- Selenium - Javascript 教程
- Selenium - Kotlin 教程
- Selenium - Ruby 教程
- Selenium - Maven 和 Jenkins
- Selenium - 資料庫測試
- Selenium - LogExpert 日誌記錄
- Selenium - Log4j 日誌記錄
- Selenium - Robot Framework
- Selenium - AutoIT
- Selenium - Flash 測試
- Selenium - Apache Ant
- Selenium - Github 教程
- Selenium - SoapUI
- Selenium - Cucumber
- Selenium - IntelliJ
- Selenium - XPath
Selenium - LogExpert 日誌記錄
Selenium Webdriver 可用於在測試執行期間記錄資訊。日誌記錄主要用於提取有關執行過程的資訊。
為什麼在 Selenium 中使用日誌記錄?
由於以下原因,日誌記錄是在 Selenium 中編寫測試時的一個重要步驟:
- 日誌記錄有助於更快地除錯故障。此外,如果設定了日誌級別,則更容易對故障進行分類。
- 大多數日誌記錄框架都是免費且開源的,我們可以在各種級別設定和抑制日誌,併發揮應用程式的最佳效能。
不同的日誌級別
日誌記錄的第一步是啟用日誌記錄,並針對每個類使用預設設定。讓我們首先啟用預設日誌記錄,該日誌記錄適用於使用根記錄器的所有記錄器。
Logger logger = Logger.getLogger("");
由於日誌記錄是針對類設定的,因此我們可以設定適用於類的日誌級別。
((RemoteWebDriver) driver).setLogLevel(Level.INFO); Logger.getLogger(SeleniumManager.class.getName()) .setLevel(Level.SEVERE);
總共有七個日誌級別 - SEVERE、WARNING、INFO、CONFIG、FINE、FINER 和 FINEST。INFO 日誌級別是預設級別,這意味著我們的程式碼中沒有任何操作項需要處理,並且純粹用於資訊目的。
Logger logger = Logger.getLogger("");
logger.setLevel(Level.INFO);
並非始終需要所有日誌級別,因此我們可以使用 setLevel() 方法根據級別過濾日誌。
Logger logger = Logger.getLogger("");
logger.setLevel(Level.WARNING);
在上面的示例中,已設定 WARNING 日誌級別,這意味著需要處理某些操作,特別是對於在我們的程式碼中使用已棄用版本。
示例 1 - 警告日誌級別
package org.example;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.remote.RemoteWebDriver;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
public class LoggingLvl {
public static void main(String[] args) throws InterruptedException {
// Initiate the Webdriver
WebDriver driver = new ChromeDriver();
// enabling log levels to Warning
((RemoteWebDriver) driver).setLogLevel(Level.WARNING);
//adding implicit wait of 12 secs
driver.manage().timeouts().implicitlyWait(12, TimeUnit.SECONDS);
// Opening the webpage
driver.get("https://tutorialspoint.tw/selenium/practice/selenium_automation_practice.php");
// getting current URL
System.out.println("Getting the Current URL: " + driver.getCurrentUrl());
// quitting the browser
driver.quit();
}
}
輸出
Feb 15, 2024 4:41:42 PM
org.openqa.selenium.remote.RemoteWebDriver log
WARNING: Executing: setTimeout
[510e0fcbc35b47f7637445d1b69bedc2, setTimeout {implicit=12000}]
Feb 15, 2024 4:41:42 PM
org.openqa.selenium.remote.RemoteWebDriver log
WARNING: Executed: setTimeout (Response: SessionID:
510e0fcbc35b47f7637445d1b69bedc2, Status: 0, Value: null)
Feb 15, 2024 4:41:42 PM
org.openqa.selenium.remote.RemoteWebDriver log
WARNING: Executing: get [510e0fcbc35b47f7637445d1b69bedc2, get {url=https://tutorialspoint.tw/selenium/practice/selenium_automation_practice.php}]
Feb 15, 2024 4:41:43 PM
org.openqa.selenium.remote.RemoteWebDriver log
WARNING: Executed: get (Response: SessionID:
510e0fcbc35b47f7637445d1b69bedc2, Status: 0, Value: null)
Feb 15, 2024 4:41:43 PM
org.openqa.selenium.remote.RemoteWebDriver log
WARNING: Executing: getCurrentUrl
[510e0fcbc35b47f7637445d1b69bedc2, getCurrentUrl {}]
Feb 15, 2024 4:41:43 PM
org.openqa.selenium.remote.RemoteWebDriver log
WARNING: Executed: getCurrentUrl (Response: SessionID:
510e0fcbc35b47f7637445d1b69bedc2, Status: 0, Value: https://tutorialspoint.tw/selenium/practice/selenium_automation_practice.php)
Feb 15, 2024 4:41:43 PM
org.openqa.selenium.remote.RemoteWebDriver log
WARNING: Executing: quit [510e0fcbc35b47f7637445d1b69bedc2, quit {}]
Getting the Current URL: https://tutorialspoint.tw/selenium/practice/selenium_automation_practice.php
Feb 15, 2024 4:41:43 PM
org.openqa.selenium.remote.RemoteWebDriver log
WARNING: Executed: quit (Response: SessionID: 510e0fcbc35b47f7637445d1b69bedc2, Status: 0, Value: null)
Process finished with exit code 0
在上面的示例中,我們獲得了 WARNING 日誌級別以及瀏覽器標題,訊息為獲取當前 URL:Selenium 自動化實踐表單。
示例 2 - 精細日誌級別
日誌資訊還包含用於查詢特定問題並進行更正的除錯資訊。這可以透過將日誌級別設定為 FINE 來完成。
Logger logger = Logger.getLogger("");
logger.setLevel(Level.FINE);
示例 3 - 嚴重日誌級別
讓我們再舉一個例子,我們將日誌級別設定為 SEVERE。
使用 SEVERE 日誌級別的程式碼實現。
package org.example;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.remote.RemoteWebDriver;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
public class LoggingLvls {
public static void main(String[] args) throws InterruptedException {
// Initiate the Webdriver
WebDriver driver = new ChromeDriver();
// enabling log levels to SEVERE
((RemoteWebDriver) driver).setLogLevel(Level.SEVERE);
//adding implicit wait of 12 secs
driver.manage().timeouts().implicitlyWait(12, TimeUnit.SECONDS);
// Opening the webpage
driver.get("https://tutorialspoint.tw/selenium/practice/selenium_automation_practice.php");
// Identify the input box with xpath locator
WebElement e = driver.findElement(By.xpath("//*[@id='name']"));
// enter text in input box
e.sendKeys("Selenium");
// getting current URL
System.out.println("Getting the Current URL: " + driver.getCurrentUrl());
//quitting the browser
driver.quit();
}
}
輸出
在上面的示例中,我們獲得了 SEVERE 日誌級別以及瀏覽器標題,訊息為獲取當前 URL:Selenium 自動化實踐表單。
在單獨的檔案中獲取日誌級別
可以透過使用處理程式將控制檯中生成的日誌寫入另一個檔案。預設情況下,所有日誌都放置在 System.err 中。
示例
package org.example;
import org.openqa.selenium.WebDriver;
import java.io.IOException;
import java.util.logging.*;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
import java.util.logging.Handler;
public class LoggingLvelFile {
public static void main(String[] args) throws InterruptedException, IOException {
// Initiate the Webdriver
WebDriver driver = new ChromeDriver();
// enabling log levels to WARNING
Logger logger = Logger.getLogger("");
logger.setLevel(Level.WARNING);
// output logging to another file Logs1.xml
Handler handler = new FileHandler("Logs1.xml");
logger.addHandler(handler);
//adding implicit wait of 12 secs
driver.manage().timeouts().implicitlyWait(12, TimeUnit.SECONDS);
// Opening the webpage
driver.get("https://tutorialspoint.tw/selenium/practice/selenium_automation_practice.php");
// getting current URL
System.out.println("Getting the Current URL: " + driver.getCurrentUrl());
//quitting the browser
driver.quit();
}
}
輸出
Getting the Current URL: https://tutorialspoint.tw/selenium/practice/selenium_automation_practice.php Process finished with exit code 0
在上面的示例中,我們在控制檯中檢索了帶有訊息的瀏覽器標題 - 獲取當前 URL:Selenium 自動化實踐表單。
最後,收到訊息程序已完成,退出程式碼為 0,表示程式碼已成功執行。
此外,專案目錄中會建立一個 Logs1.xml 檔案,其中包含日誌。
結論
本教程全面介紹了 Selenium WebDriver 日誌記錄和 LogExpert。我們首先介紹了在 Selenium 中使用日誌記錄的原因,並介紹了不同的日誌級別,並透過一個示例說明了如何將其與 Selenium 一起使用。
這使您對 LogExpert 日誌記錄有了深入的瞭解。明智的做法是不斷練習您所學到的知識,並探索與 Selenium 相關的其他知識,以加深您的理解並拓寬您的視野。
