Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Selenium入門(2023年版)
Search
onozaty
September 15, 2023
Technology
230
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Selenium入門(2023年版)
onozaty
September 15, 2023
More Decks by onozaty
See All by onozaty
Dev Containers のススメ
onozaty
0
49
リモートワーク中に買って良かったものベスト3
onozaty
0
200
情報を表現するときのポイント
onozaty
0
37
チームで開発するための環境を整える
onozaty
1
360
40歳過ぎてもエンジニアでいるためにやっていること
onozaty
0
83
Java8から17へ
onozaty
0
32
今からでも遅くないDBマイグレーション - Flyway と SchemaSpy の紹介 -
onozaty
0
290
Redmine issue assign notice plugin の紹介
onozaty
0
290
最近作ったもの
onozaty
0
42
Other Decks in Technology
See All in Technology
FPGAが実現する遠方宇宙の高空間分解能天体撮影 -大型地上望遠鏡の視力を補正する「補償光学」とは?-
komei_mt
0
230
『三匹の子ぶた』から学ぶネットワークセキュリティの昔と今 / Network Security: Then and Now Through the Lens of The Three Little Pigs
nttcom
1
1.8k
Invisible to AI? Making TYPO3 Sites Quotable by AI Search Systems
wolfgangwagner
0
200
Sets in Go
ramalho
1
310
サイバー捜査員研修(前半)
nomizone
1
2k
メルカリのグローバルアプリで挑んだ AlloyDB 運用と課題解決の実践記
hatappi
0
250
【CEDEC2026】グラフィックスエンジニアのためのニューラルシェーディング入門
cygames
PRO
0
140
[しろおび夏祭り2026] チャットするAIから、作業するAIへ - 使われ方の変化と、その裏側で起きていること
kk0n
0
1.7k
PLATEAU で バーチャル花火大会
tatsuya1970
0
150
SO-101×VLAによる3色キューブのピック&プレース
abeja
0
200
JavaScript 研修 (2026)
recruitengineers
PRO
2
530
社内の7割が使うデータ基盤を、 データチーム2人で回すためにやったこと
koh_yoshi
4
1.3k
Featured
See All Featured
A brief & incomplete history of UX Design for the World Wide Web: 1989–2019
jct
2
460
Kristin Tynski - Automating Marketing Tasks With AI
techseoconnect
PRO
0
440
Public Speaking Without Barfing On Your Shoes - THAT 2023
reverentgeek
1
520
What Being in a Rock Band Can Teach Us About Real World SEO
427marketing
0
1.1k
Code Review Best Practice
trishagee
74
20k
<Decoding/> the Language of Devs - We Love SEO 2024
nikkihalliwell
1
280
Raft: Consensus for Rubyists
vanstee
141
7.6k
How To Speak Unicorn (iThemes Webinar)
marktimemedia
1
520
Documentation Writing (for coders)
carmenintech
77
5.4k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
37
6.5k
Imperfection Machines: The Place of Print at Facebook
scottboms
270
14k
Between Models and Reality
mayunak
4
390
Transcript
Selenium入門 (2023年版) 2023-09-15 社内勉強会 onozaty
2021年2月12日に発表したものを元にアップデート https://speakerdeck.com/onozaty/seleniumru-men
Seleniumとは • Webブラウザの操作を自動化するためのフレームワーク • 主にWebアプリケーションのE2Eテストに利用 • ブラウザ操作をプログラムで行うことができる • テスト以外の自動化ツールでも利用 •
単一のAPIで様々なブラウザを同じように操作可能 • 各ブラウザ×各OSの組み合わせで同じことを実行できる Seleniumの公式サイト https://www.selenium.dev/ ドキュメントの日本語訳 https://www.selenium.dev/documentation/ja/
Seleniumの昔と今
Seleniumの昔と今 - Selenium RC 初期(10年以上前)のSelenium 1は、Selenium RC(Remote Control)とい う仕組みを使っていた •
プロキシのような形で、テスト対象のサイトにJavaScriptを埋め込 んで操作するような仕組み • ブラウザのセキュリティに起因する制約あり • アーキテクチャ的に複雑→安定しない要因に ※ Selenium Core、Selenium RCといったキーワードが出てきたら、昔の情報なので要注意
Seleniumの昔と今 - Selenium WebDriver Selenium 2からWebDriverを使うように • WebDriverを使うことで、直接ブラウザを操作できるようになっ た •
Selenium RCの時のようなセキュリティ制限を受けなくなった • シンプルな構成となった→安定するように • 現在ではW3Cで標準化されており、各ブラウザ毎にWebDriverの 実装が用意されている • 主にブラウザベンダが提供している
Seleniumを構成する要素 • Selenium WebDriver • Selenium IDE • Selenium Grid
Selenium WebDriver https://www.selenium.dev/ja/documentation/webdriver/
Selenium WebDriver • WebDriverを使うことで、ブラウザ操作をプログラムで行うことが できる 画像引用元: https://www.selenium.dev/ja/documentation/overview/components/ 各プログラミング言語(Java、Python、C#、Ruby、 JavaScriptなど)のバインディングが提供されている。 PHPのようにSelenium公式にはなくて、他から提供さ
れているものもあり。 各ブラウザ(Chrome、Firefox、Edgeなど) に対応したDriverが提供されている。 Web APIとして要求を受け付けて、 ブラウザを操作する。
Selenium WebDriver - 利用方法 昔は各種ブラウザに対応したDriverを入手し、それをパスとして指定 する必要があったが、Selenium 4.6(2022年11月リリース)でSelenium ManagerというツールがSelenium本体に同梱され、Selenium Manager が自動で対応するブラウザのDriverをダウンロードしてくれるように
なった。 • https://www.selenium.dev/ja/documentation/selenium_manager/ • 手動でのダウンロードが不要に • ブラウザのバージョンが上がる毎に、対応するDriverのバージョンもあ がって、動かなくなるということが以前は良く発生していた • WebDriverManager(Java)など、同じようなことをやってくれるラ イブラリもいくつかあったが、それも当然いらなくなる
Selenium WebDriver - 利用方法 WebDriver driver = new ChromeDriver(); //
サイトを開く driver.get("https://selenium.dev"); // id=search-byの要素を選択し、abcといった文字を入力 driver.findElement(By.id("search-by")).click(); driver.findElement(By.id("search-by")).sendKeys("abc"); // id=search-buttonの要素を探してclick driver.findElement(By.id("search-button")).click(); WebDriverのインスタンスを生成し、そのインスタンスを通して操作 ※今後出てくるコードは全てJavaでのコード
Selenium WebDriverでのデモ https://github.com/onozaty/selenium-sandbox/tree/main/selenium-junit
WebDriverを使いやすくしたもの • Selenium WebDriver自体は、テスト用というより、ブラウザ操作の ためのもの • コードが冗長になりがち • テストランナーやアサーションは別途用意する必要がある
WebDriverを使いやすくしたもの • WebDriverを内部で使いながら、テストを書きやすくするためのラ イブラリがいくつもある • Selenide https://selenide.org/ • Java •
WebdriverIO https://webdriver.io/ • Node.js
Selenide WebDriver driver = new ChromeDriver(); driver.get("http://192.168.33.10/"); driver.findElement(By.cssSelector("a[href=¥"/login¥"]")).click(); driver.findElement(By.id("username")).sendKeys("admin"); driver.findElement(By.id("password")).sendKeys("password");
driver.findElement(By.id("login-submit")).click(); assertThat(driver.findElement(By.cssSelector("h2")).getText()) .isEqualTo("title"); Configuration.browser = WebDriverRunner.CHROME; open("http://192.168.33.10/"); $("a[href=¥"/login¥"]").click(); $("#username").sendKeys("admin"); $("#password").sendKeys("password"); $("#login-submit").click(); $("h2").shouldHave(exactText("title")); 上記をSelenideで書くと、下記のように簡潔になる
Selenium IDE https://www.selenium.dev/selenium-ide/
Selenium IDE • Selenium IDEはブラウザ操作の記録、実行が行えるブラウザの拡張 機能 • ChromeとFirefox版がある
Selenium IDE • 昔のSelenium IDEはFirefox版のみで、FirefoxがXUL/XPCOMベースの 拡張機能を廃止したことで、一度死んだ • 今のものは、まったく新しいものとして作られたもの • 当初は旧IDEからかなり機能が落ちるものだったが、コマンドも揃ってきて、
今現在では十分使えるものとなった • コマンド一覧 https://docs.seleniumhq.org/selenium-ide/docs/en/api/commands/
Selenium IDE - 操作方法 • Selenium IDEを起動し、ブラウザ操作をレコーディング • Assertもブラウザ上で右クリックから追加することができる •
for each や if文などの制御構文も書くことができる • コマンドとして直接入力することで複雑な動きも実現 • コマンド一覧 https://docs.seleniumhq.org/selenium-ide/docs/en/api/commands/ • 一連の操作を繰り返し実行できる • ファイル(*.side)としても保存できるので、再度開いて実行ということも可 能
Selenium IDEでのデモ
Selenium IDE - selenium-side-runner • 保存したファイル(*.side)は、selenium-side-runner というコマンド ラインツールでも実行可能 • Node.js
で書かれたツールになっている • https://www.selenium.dev/selenium-ide/docs/en/introduction/command- line-runner
Selenium IDE と WebDriver • Selenium IDEとSelenium WebDriverは全く異なる仕組みとなるが、 Selenium IDEの内容を、WebDriverでのコードとしてエクスポート
することができる • 最初のうちは、IDEからエクスポートした コードを元にすると、WebDriverのコードが 書きやすいかも
Selenium Grid https://www.selenium.dev/documentation/ja/grid/
Selenium Grid • Selenium Gridを使うことで、複数のリモートマシン上でSeleniumを 実行することができる • 異なるブラウザ、異なるOS上でのテストを一元管理しながら、複数のリ モートマシン上で実行できる •
並列で実行することで、テストの時間短縮を行える
Selenium Grid 画像引用元: https://www.selenium.dev/documentation/ja/grid/grid_4/components_of_a_grid/
Selenium以外のもの
Selenium以外のブラウザ自動化ツール • Puppeteer https://github.com/puppeteer/puppeteer • ChromeのDevTools Protocolを使って操作するNode.jsのライブラリ • DevTools Protocolを使うので、SeleniumのWebDriverより高機能なことが
できるメリットが以前はあったが、Seleniumも最近DevTools Protocolを使 えるようになっている • https://www.selenium.dev/ja/documentation/webdriver/bidirectional/chrome_devtools/ • DevToolsのRecorderで操作をレコーディングして、Puppeteerのスクリプト としてエクスポートできる
Selenium以外のブラウザ自動化ツール • Cypress https://www.cypress.io/ • TestCafe https://testcafe.io/ • どっちもテストに特化したフレームワーク •
オールインワン、準備が楽、学習コストが低い • プロキシやブラウザの拡張機能使って、ページ上でJavaScriptを実行するよ うな仕組み
Selenium以外のブラウザ自動化ツール • Playwright https://playwright.dev/ • テスト用のフレームワーク • SeleniumやPuppeteerと同様にブラウザ自体を操作する • Puppeteer
と似たAPI • GoogleにいてPuppeteer作った人が、MicrosoftにいってPlaywrightを作っている • ブラウザ操作のレコーディングによるコード生成機能 • Trace Viewer • テスト実行時の画面状態などをGUI上でトレースできる • Node.js(JavaScript/TypeScript)だけでなく、Java、Python、.NET(C#等) など にも対応
おわり ご清聴ありがとうございました