Selenium 简明教程
Selenium - LogExpert Logging
Selenium Webdriver 可用于记录测试执行期间的信息。记录主要用于提取有关执行如何发生的信息。
Selenium Webdriver can be used for logging information during test execution. The logging is mainly used to extract information about how the execution took place.
Why use Logging in Selenium?
记录在 Selenium 中编写测试时是一个重要的步骤,原因如下:
The logging is an important step while writing tests in Selenium because of the below reasons −
-
Logging helps in debugging failures at a faster rate. Also, if the logging levels are set then it is easier to categorize failures.
-
Most of the logging frameworks are free, and open-source where we can set and suppress logs at various levels and bring out the optimal performance of an application.
Different Logging Levels
实现日志记录的第一步是针对每个类启用具有默认设置的日志记录。让我们首先启用适用于所有记录器的默认日志记录,它使用根记录器。
The first step towards logging comes with enabling logging with default settings with respect to each class. Let us first enable the default logging, which is applicable to all loggers using the root logger.
Logger logger = Logger.getLogger("");
由于日志记录是针对类进行设置的,因此我们可以设置适合类的记录级别。
Since logging is set with respect to a class, we can set the logging level applicable to a class.
((RemoteWebDriver) driver).setLogLevel(Level.INFO);
Logger.getLogger(SeleniumManager.class.getName())
.setLevel(Level.SEVERE);
共有七个记录级别:SEVERE、WARNING、INFO、CONFIG、FINE、FINER 和 FINEST。INFO 记录级别是默认级别,这意味着我们的代码中没有需要处理的动作项,纯粹出于信息目的。
There are a total seven log levels - SEVERE, WARNING, INFO, CONFIG, FINE, FINER, and FINEST. The INFO log level is the default one which means there are no action items to be taken care of in our code and purely used for informational purposes.
Logger logger = Logger.getLogger("");
logger.setLevel(Level.INFO);
总是需要所有记录级别,因此我们可以使用 setLevel() 方法根据级别来过滤日志。
Always, all logging levels are not required, so we can filter out the logs based on the levels using the setLevel() method.
Logger logger = Logger.getLogger("");
logger.setLevel(Level.WARNING);
在上面的示例中,已设置 WARNING 记录级别,这意味着需要解决一些操作,特别是用于在我们代码中使用已弃用的版本。
In the above example, log level of WARNING has been set, meaning that some actions need to be taken care, specifically for using deprecated versions in our code.
Example 1 - Warning Log Levels
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://www.tutorialspoint.com/selenium/practice/selenium_automation_practice.php");
// getting current URL
System.out.println("Getting the Current URL: " + driver.getCurrentUrl());
// quitting the browser
driver.quit();
}
}
Output
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://www.tutorialspoint.com/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://www.tutorialspoint.com/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://www.tutorialspoint.com/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 记录级别以及带有消息 Getting the Current URL: Selenium Automation Practice Form 的浏览器标题。
In the above example, we have obtained the WARNING log levels along with the browser title with the message as Getting the Current URL: Selenium Automation Practice Form.
Example 2 - Fine Log Levels
记录信息还包含用于调查特定问题并更正问题的调试信息。这可以通过将记录级别设置为 FINE 来实现。
The logging information also contains debug information for looking into a specific issue and correcting it. This can be done by setting the log level to FINE.
Logger logger = Logger.getLogger("");
logger.setLevel(Level.FINE);
Example 3 - Severe Log Levels
让我们看另一个示例,我们将记录级别设置为 SEVERE。
Let us take another example, where we would set the log level to SEVERE.
采用 SEVERE 记录级别的代码实现。
Code Implementation with SEVERE log levels.
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://www.tutorialspoint.com/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();
}
}
Output
在上面的示例中,我们获得了 SEVERE 记录级别以及带有消息 Getting the Current URL: Selenium Automation Practice Form 的浏览器标题。
In the above example, we have obtained the SEVERE log levels along with the browser title with the message as Getting the Current URL: Selenium Automation Practice Form.
Obtain Log Levels in Separate Files
可以使用一个处理程序将控制台中生成的对数写入另一个文件中。默认情况下,所有日志都放置在 System.err 中。
The logs generated in the console can be written to another file by using a handler. All the logs are placed in System.err by default.
Example
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://www.tutorialspoint.com/selenium/practice/selenium_automation_practice.php");
// getting current URL
System.out.println("Getting the Current URL: " + driver.getCurrentUrl());
//quitting the browser
driver.quit();
}
}
Output
Getting the Current URL:
https://www.tutorialspoint.com/selenium/practice/selenium_automation_practice.php
Process finished with exit code 0
在上面的示例中,我们在控制台中使用消息检索了浏览器标题 - Getting the Current URL: Selenium Automation Practice Form 。
In the above example, we retrieved the browser title with the message in the console - Getting the Current URL: Selenium Automation Practice Form.
最后,收到了消息 Process finished with exit code 0 ,表示代码成功执行。
Finally, the message Process finished with exit code 0 was received, signifying successful execution of the code.
而且,在项目目录中将创建一个 Logs1.xml 文件,其中包含日志。
Also, a Logs1.xml file would get created in the project directory with the logs.
Conclusion
总结一下我们对 Selenium WebDriver Logging 和 LogExpert 教程的全面总结。我们首先描述了在 Selenium 中使用日志的原因,并使用一个说明如何将其与 Selenium 结合使用的示例,介绍了不同的日志级别。
This concludes our comprehensive take on the tutorial on Selenium WebDriver Logging & LogExpert. We’ve started with describing why we use logging in Selenium and walked through different logging levels with an example illustrating how to use it along with Selenium.
这让你对 LogExpert 日志记录有了深入的了解。明智的做法是继续实践你学到的知识,并探索其他与 Selenium 相关的知识,以加深你的理解并拓宽你的视野。
This equips you with in-depth knowledge of the LogExpert logging. It is wise to keep practicing what you’ve learned and exploring others relevant to Selenium to deepen your understanding and expand your horizons.