このページでは、ChromeDriver を使用してウェブサイトをテストする方法について説明します。 パソコン(Windows、Mac、Linux)でご利用いただけます。また、 Android スタートガイドまたは ChromeOS の概要
セットアップ
ChromeDriver は、Selenium WebDriver が Chrome を制御するために使用する独立した実行可能ファイルです。これは、WebDriver のコントリビューターのサポートのもと、Chromium チームによって保守されます。Selenium WebDriver に詳しくない場合は、Selenium のサイトをご覧ください。
ChromeDriver で実行するテストをセットアップする手順は次のとおりです。
- Chromium または Google Chrome が認識できる場所にインストールされていることを確認する <ph type="x-smartling-placeholder">
- </ph>
- ChromeDriver では、Chrome がプラットフォームのデフォルトの場所にインストールされることを想定しています。特別な機能を設定して、ChromeDriver でカスタムの位置情報を使用することもできます。
- このサイトのダウンロード セクションから、お使いのプラットフォーム用の ChromeDriver バイナリをダウンロードします
- ダウンロードした ChromeDriver の実行可能ファイルを WebDriver が検出できるようにする
以下のどの手順でも対処できます。
- ChromeDriver の場所を PATH 環境変数に含める
- (Java のみ)webdriver.chrome.driver システム プロパティを使用して場所を指定します(下記の例を参照)。
- (Python のみ)webdriver.Chrome をインスタンス化する際に ChromeDriver のパスを指定(以下のサンプルを参照)
サンプルテスト
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()
ChromeDriver のライフタイムを制御する
ChromeDriver クラスは、作成時に ChromeDriver サーバー プロセスを開始し、quit が呼び出されると終了します。そのため、テストごとに ChromeDriver インスタンスが作成される大規模なテストスイートでは、かなりの時間が浪費される可能性があります。この問題を解決するには、次の 2 つの方法があります。
- ChromeDriverService を使用する。ほとんどの言語に対応しており、自分で ChromeDriver サーバーを起動または停止できます。Java の例(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()
- テストを実行する前に ChromeDriver サーバーを個別に起動し、リモート WebDriver を使用して接続します。
ターミナル:
$ ./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(); } }