LoginSignup
4

More than 5 years have passed since last update.

SeleniumとVisualStudioでテスト環境構築(Mac)

Last updated at Posted at 2018-01-18

MacとWindowsで共通に使えるテスト環境を作ろうとして、少しハマったのでメモしておきます。
動作確認ブラウザはChrome、Firefox、Safariです。

環境

・macOS High Sierra
・VisualStudio for Mac Community 7.3.3
・Selenium 3.8.0
・xUnit 2.3.1
・.NetCore 2.1.4

構築手順

.NetCore1.1ではNugetでSelenium系のインストールがエラーになるので、.NetCore2.0をインストールしました。
また、SafariDriverはSelenium3系から対応らしいので、3系をインストールします。

1).NetCore2.1.4のSDKをイントール

2)Visual Studio for MacのCommunity版をインストール

screen001.png

3)Visual Studioを開き、以下を参考にプロジェクト作成

Selenium with C# and xUnit → .NetCore2.0で作成

※ xUnitは拡張機能でインストール → VisualStudioforMacでxUnitを実行する方法

4)上記手順と合わせて、以下もNugetからインストール

・Selenium.Support
・Selenium.WebDriver
・Selenium.WebDriver.ChromeDriver
・Selenium.Firefox.WebDriver
・xUnit
・xUnit.runner.visualstudio

※ Nugetは依存関係の上で右クリックでウィンドウ開きます
screen002.png

5)WebDriverのセットアップ

FirefoxとChromeのWebDriverは、以下を参考に/usr/local/bin配下におきます。
SafariWebDriverは /usr/bin配下に元々あります。
【selenium】Firefox/Chrome/SafariのWebDriverインストール

6)Safariのリモートオートメーション許可

以下を参考にsafariの設定をしておきます
Safari 10 の WebDriver ネイティブサポート

7)動作確認

先ほどのSelenium with C# and xUnitのコードを参考に各ブラウザ実行してみます。
各ブラウザごとに、以下のように変えれば動作確認できます。

// Chromeの場合
driver = new ChromeDriver();
// Safariの場合
driver = new SafariDriver();
// FireFoxの場合
driver = new FirefoxDriver();

これでWindowsとMacで共有できるテストプロジェクトが作れるかと思います。
Windowsの場合は、Selenium.WebDriver.MicrosoftDriverを入れればEdgeも動作できるかと。

注意点

Visual Studioの背面にブラウザ起動するため、フルサイズ表示してると起動しないように見えてハマった。
あと、WebDriverが正しく設定できてない場合、テスト実行時に以下のエラーがでるので、VisualStudioを再起動しましょう。

[エラー内容]
System.NullReferenceException : Object reference not set to an instance of an object.

参考記事

Selenium AND .NET Core 2.0 Preview 2 – it works!
Selenium WebDriver + .NET Core 2.0- What Everyone Ought to Know

そういえば、いつの間にかChrome版のSelenium IDEあったのね。
Chromeウェブストア - Selenium IDE

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
4