セレニウムウェブドライバー
Webアプリケーションをテストするためには、ブラウザが必要です。Seleniumはブラウザを自動化し、異なるブラウザでのWebアプリケーションテストを自動化するのに役立ちます。Selenium APIには、さまざまな種類のブラウザやHTML要素と連携するための多くのクラスやインターフェースが提供されています。
セレニウムのウェブドライバーインタフェースとは何ですか?
Selenium WebDriverは、一連のメソッドを定義するインタフェースです。ただし、実装はブラウザ固有のクラスによって提供されます。実装クラスの一部には、AndroidDriver、ChromeDriver、FirefoxDriver、InternetExplorerDriver、IPhoneDriver、SafariDriverなどがあります。WebDriverの主な機能は、ブラウザを制御することです。また、HTMLページの要素を選択し、クリックやフォームの入力などの操作を実行するのにも役立ちます。
![Selenium WebDriver](https://cdn.silicloud.com/blog-img/blog/img/65649ae4daa94e2bdf7c140e/3-0.png)
もし私たちがFirefoxブラウザでテストケースを実行したい場合、FirefoxDriverクラスを使用する必要があります。同様に、Chromeブラウザでテストケースを実行したい場合には、ChromeDriverクラスを使用する必要があります。
セレンのウェブドライバーメソッド
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の例 – ウェブサイトのタイトルをプリントする
シンプルな例として、Selenium WebDriverを使用してFirefoxブラウザを起動し、ウェブサイトのタイトルを表示する方法を見てみましょう。
package com.scdev.selenium.firefox;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class GeckoDriverExample {
public static void main(String[] args) {
//specify the location of GeckoDriver for Firefox browser automation
System.setProperty("webdriver.gecko.driver", "geckodriver");
WebDriver driver = new FirefoxDriver();
driver.get("https://scdev.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 addons.webextension.screenshots@mozilla.org WARN Loading extension 'screenshots@mozilla.org': Reading manifest: Invalid host permission: resource://pdf.js/
1551941764297 addons.webextension.screenshots@mozilla.org WARN Loading extension 'screenshots@mozilla.org': 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 WebDriver Example](https://cdn.silicloud.com/blog-img/blog/img/65649ae4daa94e2bdf7c140e/19-0.png)
私たちのGitHubリポジトリから、さらに多くのSeleniumの例をチェックアウトすることができます。
参照先:WebDriverのGitHubのコード