2
0

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 5 years have passed since last update.

Docker上でSelenium WebDriver が動かない

Posted at

概要

C#で回帰テストを書こうとしたら、詰んだ。

ローカルで詰んだ際は以下で回避した。
SeleniumがCannot start the driver service on http://localhost:${port} を吐いて死ぬ

が、今度コンテナ上で動かそうと思ったら同じエラーで再度詰んだ。しかも解決できぬというおまけ付き。

なお、多分プロキシさんのせいです。

前提

macOS High Sierra
Docker for Mac
dotnet core 2.2.202

実行したいソース

    var options = new ChromeOptions();
    options.AddArgument(UserAgent);
    options.AddArgument("--headless");

    using (var chrome = new ChromeDriver("ドライバーのパス", options))
    {
        chrome.Url = "対象";
                
        // なにがし
    }

やってみる

ひとつのコンテナで解決しようとしたが無理っぽかったので別にSeleniumサーバーたてることでなんとかなるような気がした

https://github.com/SeleniumHQ/docker-selenium
こちらよりDockerファイルを拝借して、imageを作成。

StandaloneChromeDebugで作成することにしました

docker build -t chromedebug:1.0 .

テストを実行するコンテナ用dockerfile


ROM mcr.microsoft.com/dotnet/core/sdk:2.2 AS build

WORKDIR # よしなに指定
COPY # ソースを入れる
RUN dotnet restore

とりあえずtestイメージとして放置

docker build -t test:1.0 .

でdocker-compose.ymlを書いてみる

version: '3.7'
services:

  selenium:
    image: chromedebug:1.0
    container_name: selenium
    restart: always
    ports:
      - "4444::4444"

  test:
    image: test:1.0
    container_name: test 
    restart: always
    command: "tail -f /dev/null"
    depends_on:
      - selenium

実行

docker-compose up -d
docker exec dotnet test

ダメでした

ソースも変えましょう
せっかくselenium立てたのに指定すんの忘れてました。以下のように編集します

    var options = new ChromeOptions();
    options.AddArgument(UserAgent);
    options.AddArgument("--headless");

    // docker-compose.yml で立てたseleniumサーバ
    var uri = new Uri("http://selenium:4444/wd/hub/");

    using (var driver = new RemoteWebDriver(uri, options))
    {
        chrome.Url = "対象";
                
        // なにがし
    }

できました

docker-compose up -d
docker exec dotnet test

これで問題なく実行できました

まとめ

  • 別にサーバ(コンテナ)たてれば動く
  • ぶっちゃけこんなことしなくてもプロキシさんさえいなければ普通に動く気もする
2
0
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
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?