1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

curlとWebDriverだけでブラウザを操作する

Last updated at Posted at 2023-11-05

概要

curlを使ってchomedriver(以下、WebDriver)に命令を出し、ブラウザを動かす記事です。
なお、タイトルにはないですが、SessionIdを取り出す時はjqを使ってます。

動作環境

  • Macbook
    • Apple Sillicon M1
    • Sonoma 14.0
  • brew install
    • curl
    • jq
  • WebDriver
    • chromedirver:119

curlの基本

まずはhelpを見てみよう。

~ $ curl --help
Usage: curl [options...] <url>
 -d, --data <data>          HTTP POST data
 -f, --fail                 Fail fast with no output on HTTP errors
 -h, --help <category>      Get help for commands
 -i, --include              Include protocol response headers in the output
 -o, --output <file>        Write to file instead of stdout
 -O, --remote-name          Write output to a file named as the remote file
 -s, --silent               Silent mode
 -T, --upload-file <file>   Transfer local FILE to destination
 -u, --user <user:password> Server user and password
 -A, --user-agent <name>    Send User-Agent <name> to server
 -v, --verbose              Make the operation more talkative
 -V, --version              Show version number and quit

This is not the full help, this menu is stripped into categories.
Use "--help category" to get an overview of all categories.
For all options use the manual or "--help all".

どうやら全部のヘルプを見る場合は--help allとつけないといけないようです。
下記のコマンドを実行します。

curl --help all

さまざまなオプションが表示されますが、今回は個人的によく使う-X-H-dを使ってWebDriverを動かしていきたいと思います。

-Xとは

リクエストの種類を指定します。
リクエストの種類:GET,POST,PUT,DELETE

-Hとは

ヘッダを指定します。今回はContent-Type: application/jsonをヘッダとして送信します。

Content-Type: application/json

-dとは

リクエスト時に送信するデータです。今回はWebDriverの仕様に沿ってデータを渡します。

WebDriverのセットアップ

まずはWebDriverを実行するブラウザとWebDriverのバージョンを同じバージョンにする必要があります。
ブラウザからバージョンを確認しましょう。

SettingsAbout Chromeから参照できます。

VersionCheck.png

次にブラウザと同じバージョンのWebDriverをダウンロードします。

バージョン番号が114までのブラウザの場合は以下のリンクからダウンロードできます。
ダウンロードリンク

115以降の方は以下のリンクです。Stable版を選択しましょう。
ダウンロードリンク

ダウンロードするとexeが圧縮されたファイルに入っていますのでコマンドとして実行したい人は
環境変数のPATHにWebDriverのパスを入力しておきましょう。

PATHが通っているかの確認コマンドとして以下のコマンドを実行します。

chromedriver --version

実行結果

ChromeDriver 119.0.6045.105 (38c72552c5e15ba9b3117c0967a0fd105072d7c6-refs/branch-heads/6045@{#1103})

WebDriverの仕様

WebDriverは実行時、ローカルホストで9515のポートを占有します。
実行時に生成されたローカルホストへのURLがエンドポイントになります。

つまりはデフォルトのエンドポイントは下記の通りです。

http://localhost:9515

WebDriverの操作方法

WebDriverのエンドポイントURLにさまざまなリクエストを送ることで
WebDriverはリクエスト内容に応じてブラウザに命令します。

命令を実行するにあたり、WebDriverはsessionIdを発行します。
発行したsessionIdを用いることでどのブラウザへ命令できるかを認識します。

WebDriverを起動

今回はGoogle Chromeを動かすためchromedriverを実行します。
下記のコマンドを実行してください。

chromedriver

sessionIdを発行

起動ができたところでsessionIdを発行します。
下記のコマンドを実行します。

res=$(curl -X POST -H "Content-Type: application/json" -d '{"capabilities":{}}' http://localhost:9515/session)

実行に成功するとブラウザが立ち上がりますが、ここではいったん無視で大丈夫です。
次にjqを使ってres変数内にあるsessionIdを取得します。

sessionId=$(echo $res | jq -r '.value.sessionId')

特定のページを開く

sessionIdが取得できましたら、sessionIdを使ってChromeを操作していきましょう。
下記のコマンドを実行するとhttps://www.google.co.jp/をブラウザで開きます。

curl -X POST -H "Content-Type: application/json" -d '{"url":"https://www.google.co.jp/"}' "http://localhost:9515/session/$sessionId/url"

開いたページのタイトルを取得

Webページを開くことに成功しましたらcurlからブラウザまでの通信経路については問題がないと思うので
今度は逆パターン、つまりはブラウザの情報をWebDriverで取得しましょう。

下記のコマンドを実行します。

curl -X GET -H "Content-Type: application/json" "http://localhost:9515/session/$sessionId/title"

Webページのタイトルが返ってくれば、成功です。

ブラウザを閉じる

最後にブラウザを閉じましょう。

curl -X DELETE -H "Content-Type: application/json" "http://localhost:9515/session/$sessionId"

まとめ

今回はcurlを使ってWebDriverに命令を出し、ブラウザを操作しました。
他にもいろんな操作ができます。詳しくは以前のzenn記事に書いていますので
興味のある方は読んでいただけますと幸いです。

なお、実際にはSeleniumを利用してブラウザテストをやるものなのでアプリのテストでは
フレームワークなどを活用しましょう。

参考

以前のzenn記事について

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?