Selenium 简明教程
Selenium WebDriver - Headless Execution
Selenium Webdriver 可用于在任何浏览器上进行测试的无头执行。无头执行是在浏览器不可见的情况下进行测试执行的一种方式。
Selenium Webdriver can be used for headless execution of tests on any browser. A headless execution is the one in which the test execution takes place without the browser being visible.
What is Headless Execution?
现在首选无头执行,因为在这种情况下测试用例的执行速度更快,因为在此过程中不会呈现网页。此外,在无头模式下执行测试时,对系统资源的要求较少。当我们运行测试以从 Web 应用程序中抓取数据时,通常首选无头执行。
Headless execution is preferred now, since execution of test cases are faster there as the web pages are not rendered during this process. Also, system resources are less required while executing tests in headless mode. Headless execution is mostly preferred while we are running tests to scrape data from a web application.
此外,如果我们触发涉及多个浏览器的并行执行,那么如果我们进行无头执行,则资源利用就会受到限制。
Also, in case we are triggering a parallel execution involving multiple browsers, resource utilization is limited if we are doing a headless execution.
但是,我们无法使用无头执行模式对测试进行实时调试,也无法向开发者报告使用无头执行模式的失败测试中的问题。
However, we would not be able to perform live debugging of the tests and reporting an issue to the developers on the failure tests using the headless mode of execution.
Example 1
让我们以以下页面为例,在该页面中,我们在 headless 模式下使用 Chrome 浏览器和 4 以上版本的 Selenium 启动带有 URL 的应用程序。可以使用 Selenium Webdriver 中的 ChromeOptions 类实现 headless 执行。
Let us take an example of the page below, where we would launch an application with a URL in a headless mode in the Chrome browser with the Selenium version above 4. The headless execution can be implemented using the ChromeOptions class in Selenium Webdriver.
然后,获得其页面标题: Selenium Practice - Student Registration Form 。接下来,我们在 Name 标签旁边的输入框中输入文本 Selenium 。
Then, obtain its page title: Selenium Practice - Student Registration Form. Next we would enter the text Selenium in the input box beside the Name label.
然后,我们将点击 Login 按钮,然后我们将导航到另一个页面,该页面的浏览器标题为 Selenium Practice - Login 。
Then, we would click on the Login button, after which we would be navigated to another page having a browser title as Selenium Practice - Login.
代码实现
Code Implementation
package org.example;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.chrome.ChromeOptions;
public class Headless {
public static void main(String[] args) throws InterruptedException {
// setting Chrome options to browser in headless mode
ChromeOptions opt = new ChromeOptions();
opt.addArguments("--headless=new");
// Initiate the Webdriver
WebDriver driver = new ChromeDriver(opt);
// adding implicit wait of 20 secs
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
// Opening the webpage
driver.get("https://www.tutorialspoint.com/selenium/practice/selenium_automation_practice.php");
// getting page title
System.out.println("Getting the page title: " + driver.getTitle());
// identify edit box then enter text
WebElement e = driver.findElement(By.xpath("//*[@id='name']"));
e.sendKeys("Selenium");
// get test entered
System.out.println("Value entered: " + e.getAttribute("value"));
// perform click
WebElement b = driver.findElement(By.xpath("//*[@id='collapseTwo']/div/ul/li[2]/a"));
b.click();
// getting page title after click
System.out.println("Getting the current title after click: " + driver.getTitle());
// Quitting browser
driver.quit();
}
}
在 pom.xml 文件中添加的依赖项:
Dependencies added in pom.xml file −
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>SeleniumJava</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>16</maven.compiler.source>
<maven.compiler.target>16</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.11.0</version>
</dependency>
</dependencies>
</project>
Getting the page title: Selenium Practice - Student Registration Form
Value entered: Selenium
Getting the current title after click: Selenium Practice - Login
Process finished with exit code 0
在以上示例中,我们在浏览器上的 headless 模式下启动了一个 URL,并使用控制台中显示的消息获得了浏览器页面标题 - Getting the page title: Selenium Practice - Student Registration Form 。然后,我们在输入框中输入文本 Selenium,并使用控制台中显示的消息检索输入的值 - Value entered: Selenium 。之后,我们点击登录链接,并获得了导航到下一页面后的当前页面标题以及控制台中的消息: Getting the current title after click: Selenium - Selenium Practice - Login 。
In the above example, we had launched a URL in headless mode on a browser and obtained the browser page title with the message in the console - Getting the page title: Selenium Practice - Student Registration Form. Then, we entered the text Selenium in an input box and retrieved the value entered with the message in the console - Value entered: Selenium. After that, we clicked on the Login link and got the current page title after navigation to the next page with the message in console: Getting the current title after click: Selenium - Selenium Practice - Login.
Example 2
让我们以以下页面为例,在该页面中,我们将启动一个应用程序,以在 Chrome 浏览器中以 headless 模式打开 URL,并获取其页面标题 - Selenium Practice - Date Picker 。这将通过使用 ChromeOptions 类来完成。然后将值 --headless=new 传递给 addArguments 方法。
Let us take an example of the page below, where we would launch an application with open a URL in headless mode in the Chrome browser, and obtain its page title - Selenium Practice - Date Picker This will be done by using the ChromeOptions class. Then pass the value --headless=new to the addArguments method.
代码实现
Code Implementation
package org.example;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.chrome.ChromeOptions;
public class HeadlessBrow {
public static void main(String[] args) throws InterruptedException {
// setting Chrome options to browser in headless mode
ChromeOptions opt = new ChromeOptions();
opt.addArguments("--headless=new");
// Initiate the Webdriver
WebDriver driver = new ChromeDriver(opt);
// adding implicit wait of 20 secs
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
// Opening the webpage
driver.get("https://www.tutorialspoint.com/selenium/practice/date-picker.php");
// getting page title
System.out.println("Getting the page title: " + driver.getTitle());
// Quitting browser
driver.quit();
}
}
Getting the page title: Selenium Practice - Date Picker
在以上示例中,我们在浏览器上的 headless 模式下启动了一个 URL,并使用控制台中显示的消息获得了浏览器页面标题 - Getting the page title: Selenium Practice - Date Picker 。
In the above example, we had launched a URL in headless mode on a browser and obtained the browser page title with the message in the console - Getting the page title: Selenium Practice - Date Picker.
Conclusion
这总结了我们对 Selenium Webdriver headless 执行教程的全面介绍。我们从描述 headless 执行是什么开始,并逐步介绍了如何使用 Selenium Webdriver 处理 headless 执行的示例。这使您对 Selenium Webdriver Headless 执行有了深入的了解。明智的做法是继续练习你学到的知识并探索与 Selenium 相关的其他内容,以加深你的理解并拓宽你的视野。
This concludes our comprehensive take on the tutorial on Selenium Webdriver Headless Execution. We’ve started with describing what is headless execution, and walked through examples of how to handle headless execution with Selenium Webdriver. This equips you with in-depth knowledge of the Selenium Webdriver Headless Execution. It is wise to keep practicing what you’ve learned and exploring others relevant to Selenium to deepen your understanding and expand your horizons.