私たちは、ウェブアプリケーションをテストするためにブラウザが必要です。Seleniumはブラウザを自動化し、さまざまなブラウザでのウェブアプリケーションテストの自動化を支援します。Selenium APIには、さまざまなタイプのブラウザやHTML要素と連携するための多くのクラスやインターフェースが用意されています。
Selenium WebDriverインターフェースとは何ですか?
Selenium WebDriverは、一連のメソッドを定義するインターフェースです。ただし、実装はブラウザ固有のクラスによって提供されます。いくつかの実装クラスには、AndroidDriver
、ChromeDriver
、FirefoxDriver
、InternetExplorerDriver
、IPhoneDriver
、SafariDriver
などがあります。WebDriverの主な機能はブラウザの制御です。また、HTMLページの要素を選択し、クリック、フォームの入力などの操作を行うのにも役立ちます。

テストケースをFirefoxブラウザで実行したい場合は、FirefoxDriver
クラスを使用する必要があります。同様に、テストケースをChromeブラウザで実行したい場合は、ChromeDriver
クラスを使用する必要があります。
Selenium WebDriverのメソッド
SearchContextはSelenium APIの最上位のインターフェースで、findElement()とfindElements()の2つのメソッドがあります。Selenium WebDriverインターフェースには、get(String url)、quit()、close()、getWindowHandle()、getWindowHandles()、getTitle()などの多くの抽象メソッドがあります。WebDriverにはWindow
、Navigation
、Timeouts
などのネストされたインターフェースがあります。これらのネストされたインターフェースは、back()、forward()などの操作を実行するために使用されます。
Method | Description |
---|---|
get(String url) | This method will launch a new browser and opens the given URL in the browser instance. |
getWindowHandle() | It is used to handle single window i.e. main window. It return type is string. It will returns browser windlw handle from focused browser. |
getWindowHandles() | It is used to handle multiple windows. It return type is Set. It will returns all handles from all opened browsers by Selenium WebDriver. |
close() | This command is used to close the current browser window which is currently in focus. |
quit() | This method will closes all the browsers windows which are currently opened and terminates the WebDriver session. |
getTitle() | This method is used to retrieve the title of the webpage the user currently working on. |
WebDriverを実装するクラスのリスト
WebDriverインターフェースの主要な実装クラスには、ChromeDriver、EdgeDriver、FirefoxDriver、InternetExplorerDriverなどがあります。それぞれのドライバクラスはブラウザに対応しています。ドライバクラスのオブジェクトを作成してそれらと連携します。
Class | Description |
---|---|
ChromeDriver | It helps you to execute Selenium Scripts on Chrome browser. |
FirefoxDriver | It helps you to execute Selenium Scripts on Firefox browser. |
InternetExplorerDriver | It helps you to execute Selenium Scripts on InternetExplorer browser. |
WebElementに対するコマンドのリスト
SeleniumのWebElementはHTML要素を表します。findElement()メソッドを使用してWebElementのインスタンスを取得し、クリック、送信などの特定のアクションを実行することができます。一部のよく使用されるWebElementメソッドは以下の通りです:
Command | Description | Syntax |
---|---|---|
findElement() | This method finds the first element within the current web page by using given locator. | WebElement element = driverObject.findElement(By.locator(“value”)); |
sendKeys() | This method enters a value in to an Edit Box or Text box. | driver.findElement(By.elementLocator(“value”)).sendkeys(“value”); |
clear() | It clears the Value from an Edit box or Text Box. | driverObject.findElement(By.locatorname(“value”)).clear(); |
click() | It clicks an Element (Button, Link, Checkbox) etc. | driverObject.findElement(By.ElementLocator(“LocatorValue”)).click(); |
Selenium WebDriverの例 – ウェブサイトのタイトルを出力する
セレンWebDriverを使用してFirefoxブラウザを起動し、ウェブサイトのタイトルを出力する簡単な例を見てみましょう。
package com.journaldev.selenium.firefox;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class GeckoDriverExample {
public static void main(String[] args) {
// Firefoxブラウザの自動化のためのGeckoDriverの場所を指定する
System.setProperty("webdriver.gecko.driver", "geckodriver");
WebDriver driver = new FirefoxDriver();
driver.get("https://journaldev.com");
String PageTitle = driver.getTitle();
System.out.println("Page Title is:" + PageTitle);
driver.close();
}
}
出力:
1551941763563 mozrunner::runner INFO Running command: "/Applications/Firefox.app/Contents/MacOS/firefox-bin" "-marionette" "-foreground" "-no-remote" "-profile" "/var/folders/1t/sx2jbcl534z88byy78_36ykr0000gn/T/rust_mozprofile.t6ZyMHsrf2bh"
1551941764296 [email protected] WARN Loading extension '[email protected]': Reading manifest: Invalid host permission: resource://pdf.js/
1551941764297 [email protected] WARN Loading extension '[email protected]': Reading manifest: Invalid host permission: about:reader*
Can't find symbol 'GetGraphicsResetStatus'.
1551941765794 Marionette INFO Listening on port 61417
1551941765818 Marionette WARN TLS certificate errors will be ignored for this session
Mar 07, 2019 12:26:05 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
Page Title is:JournalDev - Java, Java EE, Android, Python, Web Development Tutorials
1551941814652 Marionette INFO Stopped listening on port 61417

より多くのSeleniumの例は、私たちのGitHubリポジトリからチェックアウトできます。
参考文献: WebDriver GitHubコード
Source:
https://www.digitalocean.com/community/tutorials/selenium-webdriver