Motomichi Works Blog

モトミチワークスブログです。その日学習したことについて書いている日記みたいなものです。

windows7+eclipse+Selenium WebDriverその0001(環境構築して、Firefoxでwebページを開いて閉じるとこまで)

参考にさせて頂いたページ

Selenium WebDriverのインストール~動かしてみる - hifive

selenium2での記述サンプル --ignore-certificate-errorへの対応 - Qiita

Javaをインストールする

jre-8u45-windows-x64.exeをダウンロードしてきてインストールした。

パスが通っているか一応確認する

コマンドプロンプト

java -version

を実行したら

java version "1.8.0_45"
Java(TM) SE Runtime Environment (build 1.8.0_45-b15)
Java HotSpot(TM) 64-Bit Server VM (build 25.45-b02, mixed mode)

と表示された。

eclipseのダウンロードと解凍

eclipse-java-luna-SR2-win32-x86_64.zipをダウンロードして解凍した。

その中のeclipse/eclipse.exeを実行すると起動できると思う。

C:\Users\ユーザー名\workspaceを作成する。

eclipseの作業フォルダとして使用されます。

selenium-java-2.45.0.zipのダウンロードと解凍

ここからはほとんど参考ページのSelenium WebDriverのインストール~動かしてみる - hifiveにならって進めていきます。

http://docs.seleniumhq.org/download/からselenium-java-2.45.0.zipをダウンロードして、解凍した。

SeleniumSampleプロジェクトを作成する

メニューからFile > New > Java Projectを選択する。

Project nameのところに今回はSeleniumSampleと入力してfinishをクリックする。

eclipseの左カラムに開いている、Package Explorerの中にSeleniumSampleが作成される。

libディレクトリを作成する

C:\Users\ユーザー名\workspaceにlibディレクトリを作成する。

libディレクトリにjarファイルを入れる

さきほどselenium-java-2.45.0.zipを解凍してできた中の

  • selenium-java-2.45.0.jar
  • libsフォルダの中にある全てのjarファイル

上記をC:\Users\ユーザー名\workspace\libにコピーする。

libディレクトリにコピーしたjarを全て、クラスパスに追加する

  • eclipseのPackage ExplorerからSeleniumSampleを右クリック
  • Propertiesをクリック
  • 左カラムのJava Build Pathをクリック
  • Librariesタブをクリック
  • Add External JARs...をクリック
  • C:\Users\ユーザー名\workspace\SeleniumSample\libのjarファイルを全て選択して開くをクリック
  • OKをクリックすると、Setting build pathes...と表示されて、少し待つと設定が完了する。

パッケージの作成

  • eclipseFile > New > Packageを選択する
  • Source folderにはSeleniumSample/srcと表示されていると思うのでそのまま
  • Nameには任意の文字列を入れるのだけど、今回はcom.example.testsと入力する
  • finishをクリック

ディレクトリが作成されて

C:\Users\ユーザー名\workspace\SeleniumSample\src\com\example\tests

みたいな感じの構造が出来上がっている

Google Chromeのインストール

みなさん既にインストールされているかもしれませんが一応

Firefoxのインストール

みなさん既にインストールされているかもしれませんが一応

driverディレクトリの作成

C:\Users\ユーザー名\workspace\SeleniumSampleにdriverディレクトリを作成した。

chromedriver.exeの入手と配置

64bit専用のchromedriverが無いそうなので、32ビット版のchromedriverchromedriver_win32_2.1.zipをダウンロード

上記で打ち消し線を入れたものは古いバージョンで、chromeが起動してもブランクのページのまま閉じてしまうので、http://chromedriver.storage.googleapis.com/index.htmlから最新版をダウンロードすると良さそう。

2015年06月10日現在では2.16が最新のようなので、2.16のリンクから、chromedriver_win32.zipを改めてダウンロードした。

解凍するとchromedriver.exeが入っているので、C:\Users\ユーザー名\workspace\SeleniumSample\driver\chromedriver.exeみたいな感じで配置する

テスト実行用のjavaファイルを作成する

  • eclipseのPackage Explorerから、srcの中のcom.example.testsを右クリックして、New > Classをクリック
  • Source folderはSeleniumSample/srcになっているのでそのまま
  • Packageもcom.example.testsになっているのでそのまま
  • Nameは今回はHogeTestと入力する
  • Finishをクリックする
  • HogeTest.javaが作成されて、eclipseで編集できる。

    package com.example.tests;

    public class HogeTest {

    }

HogeTest.javaの編集(Firefox用)

以下のような感じで編集する。

package com.example.tests;

import org.junit.BeforeClass;
import org.junit.Before;
import org.junit.Test;
import org.junit.After;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class HogeTest {
    private WebDriver driver;
    @Before
    public void setUp(){
      //Firefoxを起動
      driver = new FirefoxDriver();
    }

    @Test
    public void testSample(){
      driver.get("http://www.yahoo.co.jp/");
    }

    @After
    public void tearDown(){
      //ブラウザを閉じる
      driver.quit();
    }
}

HogeTest.javaの編集(Chrome用)

package com.example.tests;

import org.junit.BeforeClass;
import org.junit.Before;
import org.junit.Test;
import org.junit.After;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class HogeTest {
    private WebDriver driver;
    @Before
    public void setUp(){
      System.setProperty("webdriver.chrome.driver","./driver/chromedriver.exe");
      driver = new ChromeDriver();
    }

    @Test
    public void testSample(){
      driver.get("http://www.yahoo.co.jp/");
    }

    @After
    public void tearDown(){
      driver.quit();
    }
}

chromeで表示される--ignore-certificate-errorを消す

--ignore-certificate-errorの表示を消すには、

selenium2での記述サンプル --ignore-certificate-errorへの対応 - Qiita

Firefoxを開いて閉じてみる

  • eclipseのPackage Exprolerの中のsrc > com.example.tests > HogeTest.javaを右クリックする。
  • Run As > 1 JUnit Testをクリック
  • しばらく待つとブラウザが落ちたときと同じFirefoxは動作を停止しましたが表示される。
  • プログラムを終了しますをクリック
  • しばらく待つと自動でFirefoxが起動して、自動でYahooのURLへアクセスし、自動で閉じられる

Chrome用のソースコードを記述して、上記の手順を実行するとChromeが開きます。

終わりに

HogeTest.javaにおいて、使用しないものまでimportしていますが、自身の覚書きとして残っています。

Firefoxは開いたり閉じたりまで、ようやくできた感じです。

IEはまた後で。

Chromeはchromedriver.exeのバージョンを新しくして動くようになりました。

今回はここまで。