Selenium 触ってみよう

25
Selenium 触ってみよう ASP.NET 初心者向け勉強会 2015/04/11 おだ

Transcript of Selenium 触ってみよう

Page 1: Selenium 触ってみよう

Selenium 触ってみよう

ASP.NET初心者向け勉強会

2015/04/11 お だ

Page 2: Selenium 触ってみよう

自己紹介

織田 信亮(おだ しんすけ)

大阪で開発者しています

SQLWorld 代表 (http://sqlworld.org)

http://odashinsuke.hatenablog.com/

Twitter:@shinsukeoda

Page 3: Selenium 触ってみよう

Selenium ってなに?

Web アプリケーション用のテストツール

ブラウザを使って Web アプリケーションの動作確認等を行う

ブラウザの操作を Selenium が行ってくれる

本日触るのは Selenium WebDriver

Page 4: Selenium 触ってみよう

Selenium WebDriver ってなに?

http://seleniumhq.org/docs/03_webdriver.html

Selenium が WebDriver と統合された

Selenium 1.0 だと JavaScript/HTML で記述がメイン

WebDriver は、Selenium ではセキュリティで制限されていたものが回避出来る

Selenium 2.0 で統合!

Page 5: Selenium 触ってみよう

API が提供されている言語

Java

C#

Python

Ruby

PHP

Perl

JavaScript

Page 6: Selenium 触ってみよう

提供されている WebDriver

HtmlUnit Driver

Firefox Driver

Internet Explorer Driver

Chrome Driver

Opera Driver

iOS Driver

Android Driver言語によっては、提供されていない Driver もある!

Page 7: Selenium 触ってみよう

環境構築 (Visual Studio)

Visual Studio NuGet からインストール

Package Manage Console(NuGet) から

Support は便利だからいれてます

Install-Package Selenium.WebDriverInstall-Package Selenium.Supportinstall-package Selenium.IEDriver

今日のコードはこちらhttps://github.com/OdaShinsuke/20150411_ASPNET

Page 8: Selenium 触ってみよう

みんな大好き IE Driver を使う

全てのゾーンで「保護モードを有効にする」チェックの値を統一

拡大は 100% にしておく

Page 9: Selenium 触ってみよう

using System; using OpenQA.Selenium.IE; using OpenQA.Selenium;

class Program { static void Main(string[] args) { IWebDriver driver = new InternetExplorerDriver(); Console.ReadKey(); driver.Quit();

} }

とりあえず、ブラウザ起動してみる

Page 10: Selenium 触ってみよう

using System; using OpenQA.Selenium.IE; using OpenQA.Selenium;

class Program { static void Main(string[] args) { IWebDriver driver = new InternetExplorerDriver(); driver.Navigate().GoToUrl("http://www.bing.com");Console.WriteLine(driver.Title); Console.ReadKey(); driver.Quit();

} }

Bing にいってみる

Page 11: Selenium 触ってみよう

API の基本

ドライバー

IWebDriver (WebDriver)

エレメント

IWebElement (WebElement)

ロケーター

By

Page 12: Selenium 触ってみよう

ドライバー の API

ページのタイトル取得

要素の検索ISearchContext を実装FindElement, FindElements

ページ遷移INavigation を保持GoToUrl, Back, Forward

コンテキストの切り替えITargetLocator を保持Alert, Frame, Window

Page 13: Selenium 触ってみよう

using System; using OpenQA.Selenium.IE; using OpenQA.Selenium;

class Program { static void Main(string[] args) { IWebDriver driver = new InternetExplorerDriver(); driver.Navigate().GoToUrl("http://www.bing.com"); IWebElement element = driver.FindElement(By.Name("q")); element.SendKeys("セレニウム ウェブドライバー"); Console.ReadKey(); driver.Quit();

} }

テキストボックスに文字を入力

Page 14: Selenium 触ってみよう
Page 15: Selenium 触ってみよう

ロケーターには何がある?

Id

Name

TagName

ClassName

CssSelector

LinkText

PartialLinkText

XPath

Page 16: Selenium 触ってみよう

using System; using OpenQA.Selenium.IE; using OpenQA.Selenium;

class Program { static void Main(string[] args) { IWebDriver driver = new InternetExplorerDriver(); driver.Navigate().GoToUrl("http://www.bing.com"); IWebElement elementByName = driver.FindElement(By.Name("q")); elementByName.SendKeys("セレニウム ウェブドライバー"); IWebElement elementById = driver.FindElement(By.Id("sb_form_q")); elementById.SendKeys(" ID で取ったお"); IWebElement elementByCss = driver.FindElement(By.CssSelector("b_searchbox"));

elementByCss.Clear(); elementByCss.SendKeys("CssSelector で"); Console.ReadKey(); driver.Quit();

}}

色んな取り方をしてみる

Page 17: Selenium 触ってみよう

エレメント の メソッド

SendKeys

Clear

Click

GetAttribute

input タグの入力値はこれで取得する

GetCssValue

Submit

要素の検索 (ISearchContext を実装)

Page 18: Selenium 触ってみよう

エレメント の プロパティ

基本 get だけDisplayedEnabledLocationSelected

チェックボックスや Select の Option

SizeTagNameText

タグに挟まれたテキストのことテキストボックスの値じゃない!

Page 19: Selenium 触ってみよう

class Program {static void Main(string[] args) {var driver = new InternetExplorerDriver();try {driver.Navigate().GoToUrl("http://www.bing.com");var txt条件 = driver.FindElementByName("q");txt条件.SendKeys("SQLWorld");txt条件.Submit();Thread.Sleep(3000); // 次の画面に遷移するまで待つvar lbl件数 = driver.FindElementByClassName("sb_count");Console.WriteLine(lbl件数.Text);Console.ReadKey();

} finally {driver.Quit();

}}

}

検索してみる

Page 20: Selenium 触ってみよう

便利なやつ

IWait(Wait)

Selenium.Support に含まれている

OpenQA.Selenium.Support.UI.IWait

Thread.Sleep はもう古い!

指定条件を満たすまで待機する

タイムアウト指定することで、異常時には例外で終了する

Page 21: Selenium 触ってみよう

Wait

WebDriverWait が良く使われる

コンストラクタでタイムアウト時間指定

Until メソッドに Func を渡し条件指定

Func は、既定の実装が幾つかある

ExpectedConditions

Func を自前で実装も可wait.Until(d => d.Title == "Microsoft - Bing");

IWait<IWebDriver> wait = new WebDriverWait(driver, new TimeSpan(0, 0,10));

wait.Until(ExpectedConditions.TitleIs("Microsoft - Bing"));

Page 22: Selenium 触ってみよう

class Program {static void Main(string[] args) {var driver = new InternetExplorerDriver();try {driver.Navigate().GoToUrl("http://www.bing.com");var txt条件 = driver.FindElementByName("q");txt条件.SendKeys("SQLWorld");txt条件.Submit();var wait = new WebDriverWait(driver, new TimeSpan(0, 0, 10));wait.Until(ExpectedConditions.TitleIs("SQLWorld - Bing")); var lbl件数 = driver.FindElementByClassName("sb_count");Console.WriteLine(lbl件数.Text);Console.ReadKey();

} finally {driver.Quit();

}}

}

Wait 使って検索してみる

Page 23: Selenium 触ってみよう

スクリーンショット

ITakesScreenshot を実装している Driver が対象

殆どの Driver は実装している

driver.GetScreenshot().SaveAsFile(@"c:\work\hoge.png", ImageFormat.Png);

Page 24: Selenium 触ってみよう

class Program {static void Main(string[] args) {var driver = new InternetExplorerDriver();try {

driver.Navigate().GoToUrl("http://sqlworld.org/event/20150425/");var wait = new WebDriverWait(driver, new TimeSpan(0, 0, 10));wait.Until(ExpectedConditions.TitleIs("SqlWorld :: SQLWorld★大阪#30"));driver.GetScreenshot().SaveAsFile(Environment.GetFolderPath(Environment.SpecialFolder.Desktop)

+ @"\\sqlworld.png",ImageFormat.Png);

Console.ReadKey();} finally {

driver.Quit();}

}}

スクリーンショット をとってみる

Page 25: Selenium 触ってみよう

class Program {static void Main(string[] args) {var driver = new InternetExplorerDriver();try {driver.Navigate().GoToUrl("http://sqlworld.org/event/20150425/");var wait = new WebDriverWait(driver, new TimeSpan(0, 0, 10));wait.Until(ExpectedConditions.TitleIs("SqlWorld :: SQLWorld★大阪#30"));driver.FindElementByName("name").SendKeys("あなたのお名前");driver.FindElementByName("email").SendKeys("あなたのE-Mail");driver.FindElementByName("commentText").SendKeys(@"宜しくお願いします!

懇親会:参加");Console.ReadKey();driver.FindElementByName("Post Comment").Submit();Console.ReadKey();

} finally {driver.Quit();

}}

}

実践編!