Bắt đầu sử dụng ChromeDriver

Trang này trình bày cách bắt đầu sử dụng ChromeDriver để thử nghiệm trang web của bạn trên máy tính để bàn (Windows/Mac/Linux). Bạn cũng có thể đọc Bắt đầu với Android hoặc Bắt đầu sử dụng ChromeOS.

Thiết lập

ChromeDriver là một tệp thực thi riêng biệt mà Selenium WebDriver sử dụng để kiểm soát Chrome. Tính năng này được nhóm Chromium duy trì với sự trợ giúp của những người đóng góp WebDriver. Nếu chưa hiểu rõ về Selenium WebDriver, bạn nên tham khảo trang web của Selenium.

Hãy làm theo các bước sau để thiết lập các thử nghiệm chạy bằng ChromeDriver:

  • Đảm bảo bạn đã cài đặt Chromium/Google Chrome ở vị trí nhận dạng được
  • Tải tệp nhị phân ChromeDriver xuống cho nền tảng của bạn trong phần tải xuống trên trang web này
  • Giúp WebDriver tìm tệp thực thi ChromeDriver đã tải xuống

Bạn nên thực hiện một trong các bước sau đây:

  1. đưa vị trí ChromeDriver vào biến môi trường PATH của bạn
  2. (Chỉ dành cho Java) chỉ định vị trí của trình duyệt bằng cách sử dụng thuộc tính hệ thống webdriver.chrome.driver (xem mẫu bên dưới)
  3. (Chỉ dành cho Python) bao gồm đường dẫn đến ChromeDriver khi tạo thực thể webdriver.Chrome (xem mẫu bên dưới)

Kiểm thử mẫu

Java:

import org.openqa.selenium.*;
import org.openqa.selenium.chrome.*;
import org.junit.Test;
public class GettingStarted {   
@Test   
public void testGoogleSearch() throws InterruptedException {
  // Optional. If not specified, WebDriver searches the PATH for chromedriver.
  // System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
  // WebDriver driver = new ChromeDriver();
  driver.get("http://www.google.com/"); 
  Thread.sleep(5000);  // Let the user actually see something!
  WebElement searchBox = driver.findElement(By.name("q"));
  searchBox.sendKeys("ChromeDriver");
  searchBox.submit(); 
  Thread.sleep(5000);  // Let the user actually see something!
  driver.quit();  
 }
}

Python:

import time
from selenium import webdriver

driver = webdriver.Chrome('/path/to/chromedriver')  # Optional argument, if not specified will search path.
driver.get('http://www.google.com/');
time.sleep(5) # Let the user actually see something!
search_box = driver.find_element_by_name('q')
search_box.send_keys('ChromeDriver')
search_box.submit()
time.sleep(5) # Let the user actually see something!
driver.quit()

Kiểm soát vòng đời của ChromeDriver

Lớp ChromeDriver khởi động quy trình máy chủ ChromeDriver khi tạo và chấm dứt quy trình này khi thoát được gọi. Điều này có thể làm lãng phí một lượng thời gian đáng kể đối với các bộ kiểm thử lớn mà trong đó một thực thể ChromeDriver được tạo cho mỗi lượt kiểm thử. Có hai lựa chọn để khắc phục vấn đề này:

  1. Sử dụng ChromeDriverService. Tính năng này có sẵn cho hầu hết các ngôn ngữ và cho phép bạn tự khởi động hoặc dừng máy chủ ChromeDriver. Xem tại đây để biết ví dụ về Java (với JUnit 4):
import java.io.*;
import org.junit.*;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.*;
import org.openqa.selenium.remote.*;
public class GettingStartedWithService {
  private static ChromeDriverService service;
  private WebDriver driver;
  @BeforeClass
  public static void createAndStartService() throws IOException {
      service = new ChromeDriverService.Builder()
              .usingDriverExecutable(new File("/path/to/chromedriver"))
              .usingAnyFreePort()
              .build();
      service.start();
  }
  
  @AfterClass   
  public static void stopService() {
    service.stop();
  }

  @Before   
  public void createDriver() {
    driver = new RemoteWebDriver(service.getUrl(), new ChromeOptions());
  }

  @After   public void quitDriver() {
    driver.quit();
  }

  @Test   
  public void testGoogleSearch() {
    driver.get("http://www.google.com");
    // rest of the test...
  }
}

Python:

import time
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
service = Service('/path/to/chromedriver')
service.start()
driver = webdriver.Remote(service.service_url)
driver.get('http://www.google.com/');
time.sleep(5) # Let the user actually see something!
driver.quit()
  1. Khởi động riêng máy chủ ChromeDriver trước khi chạy kiểm thử và kết nối với máy chủ đó bằng Remote WebDriver.

Nhà ga:

$ ./chromedriver
Starting ChromeDriver
76.0.3809.68 (...) on port 9515
...

Java:

import java.net.*;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.*;
import org.openqa.selenium.remote.*;  

public class GettingStartedRemote {

  public static void main(String[] args) throws MalformedURLException {
    WebDriver driver = new RemoteWebDriver(
        new URL("http://127.0.0.1:9515"),
        new ChromeOptions());
    driver.get("http://www.google.com");
    driver.quit();
  }
}