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

Trang này trình bày cách bắt đầu sử dụng ChromeDriver để kiểm thử trang web trên máy tính (Windows/Mac/Linux). Bạn cũng có thể đọc các bài viết Bắt đầu sử dụng 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 mà Selenium WebDriver sử dụng để kiểm soát Chrome. Trình duyệt này được nhóm Chromium duy trì với sự trợ giúp của các cộng tác viên WebDriver. Nếu chưa hiểu rõ về Selenium WebDriver, bạn nên xem trang web của Selenium.

Làm theo các bước sau để thiết lập hoạt động kiểm thử chạy bằng ChromeDriver:

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

Bất kỳ bước nào trong số các bước sau đây đều có thể khắc phục được vấn đề:

  1. đưa thông tin 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 thiết bị bằng thuộc tính hệ thống webdriver.chrome.driver (xem mẫu dưới đây)
  3. (Chỉ dành cho Python) bao gồm đường dẫn đến ChromeDriver khi tạo thực thể cho webdriver.Chrome (xem ví dụ dưới đây)

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 khi quá trình thoát được gọi. Việc này có thể lãng phí một lượng thời gian đáng kể cho các bộ kiểm thử lớn mà trong đó, một phiên bản ChromeDriver được tạo cho mỗi lượt kiểm thử sẽ bị lãng phí. Có hai lựa chọn để khắc phục vấn đề này:

  1. Dùng ChromeDriverService. Tính năng này dùng được 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. Hãy xem ví dụ về Java tại đây (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.

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();
  }
}