selenium 触ってみよう

Post on 17-Jul-2015

249 Views

Category:

Technology

4 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Selenium 触ってみよう

ASP.NET初心者向け勉強会

2015/04/11 お だ

自己紹介

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

大阪で開発者しています

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

http://odashinsuke.hatenablog.com/

Twitter:@shinsukeoda

Selenium ってなに?

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

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

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

本日触るのは Selenium WebDriver

Selenium WebDriver ってなに?

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

Selenium が WebDriver と統合された

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

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

Selenium 2.0 で統合!

API が提供されている言語

Java

C#

Python

Ruby

PHP

Perl

JavaScript

提供されている WebDriver

HtmlUnit Driver

Firefox Driver

Internet Explorer Driver

Chrome Driver

Opera Driver

iOS Driver

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

環境構築 (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

みんな大好き IE Driver を使う

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

拡大は 100% にしておく

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

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

} }

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

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 にいってみる

API の基本

ドライバー

IWebDriver (WebDriver)

エレメント

IWebElement (WebElement)

ロケーター

By

ドライバー の API

ページのタイトル取得

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

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

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

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();

} }

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

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

Id

Name

TagName

ClassName

CssSelector

LinkText

PartialLinkText

XPath

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();

}}

色んな取り方をしてみる

エレメント の メソッド

SendKeys

Clear

Click

GetAttribute

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

GetCssValue

Submit

要素の検索 (ISearchContext を実装)

エレメント の プロパティ

基本 get だけDisplayedEnabledLocationSelected

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

SizeTagNameText

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

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();

}}

}

検索してみる

便利なやつ

IWait(Wait)

Selenium.Support に含まれている

OpenQA.Selenium.Support.UI.IWait

Thread.Sleep はもう古い!

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

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

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"));

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 使って検索してみる

スクリーンショット

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

殆どの Driver は実装している

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

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();}

}}

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

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();

}}

}

実践編!

top related