selenium チュートリアル
セレンを使い始める
サーチ…
備考
Seleniumは、プログラマがブラウザの相互作用を自動化するための多言語(C#、Haskell、Java、JavaScript、Objective-C、Perl、PHP、Python、R、およびRuby)の強力なコマンドライブラリです。これは開発者がアプリケーションをテストするのに非常に便利です。
Seleniumは以下の方法を提供します。
- Webページでの要素の検索
- 要素をクリック
- 要素に文字列を送る
- ウェブページに移動する
- 同じブラウザウィンドウ内の別のタブに変更する
- ウェブページのスクリーンショットを撮る
これらのメソッドを使用すると、開発者は自動的にテストをチェックできます。
- 要素がページ内にあり、ユーザーに表示されている場合
- 検索フォームまたはログインフォーム
- ボタンまたはインタラクティブな要素
- 要素の値または属性をチェックする
SeleniumはWebdriverで動作しますが、これは通常のWebブラウザに似ていますが、Seleniumと対話することができます。セレンテストは、通常、開発者がテストしているブラウザの新しいドライバインスタンスを開きます。これは常にクリーンスレートです。このように、Seleniumテストを実行する場合、開発者は以前のCookieやアプリケーションの結果に影響するブラウザキャッシュについて心配する必要はありません。
Seleniumは、ヘッドレスモードでwebdriverを実行しているときにも動作します。
バージョン
バージョン | 発売日 |
---|---|
3.4.0 | 2017-04-11 |
3.3 | 2017-04-07 |
3.2 | 2017-02-27 |
3.1 | 2017-02-13 |
3.0.1 | 2016-11-19 |
3.0 | 2016-10-11 |
Javaでのシンプルなセレンテスト
以下のコードは、セレンを使用した簡単なJavaプログラムです。以下のコードの旅は
- Firefoxブラウザを開く
- Googleのページを開く
- Googleページのタイトルを印刷する
- 検索ボックスの場所を見つける
- 検索ボックスに値をSeleniumとして渡します
- フォームを送信する
- ブラウザをシャットダウンする
package org.openqa.selenium.example;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import java.util.concurrent.TimeUnit;
public class Selenium2Example {
public static void main(String[] args) {
// Create a new instance of the Firefox driver
WebDriver driver = new FirefoxDriver();
// An implicit wait is to tell WebDriver to poll the DOM for a certain amount of time
// when trying to find an element or elements if they are not immediately available.
// The default setting is 0. Once set, the implicit wait is set for the life of the WebDriver object instance.
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
// Maximize the browser window to fit into screen
driver.manage().window().maximize();
// Visit Google
driver.get("http://www.google.com");
// Check the title of the page
System.out.println("Page title is: " + driver.getTitle());
// Find the text input element by its name
WebElement element = driver.findElement(By.name("q"));
// Enter something to search for
element.sendKeys("Selenium!");
// Now submit the form. WebDriver will find the form for us from the element
element.submit();
//Close the browser
driver.quit();
}
}
Pythonでのシンプルなセレンテスト
from selenium import webdriver
# Create a new chromedriver
driver = webdriver.Chrome()
# Go to www.google.com
driver.get("https://www.google.com")
# Get the webelement of the text input box
search_box = driver.find_element_by_name("q")
# Send the string "Selenium!" to the input box
seach_box.send_keys("Selenium!")
# Submit the input, which starts a search
search_box.submit()
# Wait to see the results of the search
time.sleep(5)
# Close the driver
driver.quit()
Python Seleniumを端末経由でセットアップする(BASH)
一番簡単な方法は、 pipとVirtualEnvを使うことです 。セレンにはpython 3も必要です。
以下を使用してvirtualenvをインストールします。
$: pip install virtualenv
Seleniumファイル用のディレクトリを作成/入力します:
$: cd my_selenium_project
Seleniumファイルのディレクトリに新しいVirtualEnvを作成します。
$: virtualenv -p /usr/bin/python3.0 venv
VirtualEnvを有効にする:
$: source venv/bin/active
各bash行の先頭に(venv)が表示されます。 pipを使用してSeleniumをインストールします。
$: pip install selenium
SeleniumにはデフォルトでFireFoxドライバが付属しています。
Google ChromeでSeleniumを実行する場合は、次のようにします。
$: pip install chromedriver
バージョン管理されたVirtualEnvが完成しました。すべてが正しく設定されていることを確認するには:
Pythonを起動する:
$: python
プリントアウト:
Python 2.7.10 (default, Jul 14 2015, 19:46:27)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.39)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
新しいウェブドライバ(この場合はクロムドライブ)を作成し、www.google.comにアクセスしてください:
>>> from selenium import webdriver
>>> driver = webdriver.Chrome()
>>> driver.get("https://www.google.com")
ドライバとPythonインタプリタを閉じます:
>>> driver.quit()
>>> quit()
VirtualEnvを無効にする:
$: deactivate
line driver = webdriver.Chrome()
がエラーをスローしている場合:
- クロムブラウザもインストールされていることを確認してください。そうしなければ、SeleniumクロムドライバはChromeバイナリにアクセスできません。
- webdriver.Chrome()は、クロムドライブの場所のパラメータを取ることもできます。 pipを使用してインストールした場合は、(Macの場合)
driver = webdriver.Chrome("./venv/selenium/webdriver/chromedriver")
。
C#での単純なセレンの例
//Create a new ChromeDriver
IWebDriver driver = new ChromeDriver();
//Navigate to www.google.com
driver.Navigate().GoToUrl("https://www.google.com");
//Find the WebElement of the search bar
IWebElement element = driver.FindElement(By.Name("q"));
//Type Hello World into search bar
element.SendKeys("Hello World");
//Submit the input
element.Submit();
//Close the browser
driver.Quit();