Motomichi Works Blog

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

windows7+eclipse+Selenium WebDriverその0004(Chromeを起動して操作する)

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

chromedriver.exeのバージョンが古いことによって真っ白くなる症状についての対応

Selenium IDEを使って、ChromeやIE上でテストスクリプトを実行する方法 | 品質向上ブログ

環境

はじめに

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

で実行したインストール手順で、Firefoxを起動して上手く動かすところまで行けました。

しかしながら、Chromeを起動するとURLが上手く設定されずページが真っ白だったのですが、どうやらchromedriver.exeのバージョンが合っていない事に起因する問題だったようです。

上記の記事を少し更新したので、現在は手順通り進めていただければ、FirefoxChromeもおそらく上手く起動できると思います。

ちなみに2.0のリンクから、chromedriver_win32.zipをダウンロードしたときは上手くいかず、2.16のリンクからchromedriver_win32.zipをダウンロードしたときは上手く動きました。

上手くいかなかったとき、Chromeは起動するものの、ページは真っ白で、URLバーにはdata:text/html;charset=utf-8,と表示されました。

ダウンロードする際によく見ずに、一番上にあるリンクをクリックしてハマってしまい恥ずかしい限りです。

chromedriver.exeをダウンロードするページ

新しいバージョンをダウンロードしてください。

http://chromedriver.storage.googleapis.com/index.html

HogeTest.javaの記述内容

Firefoxを起動する場合のコードもコメントとして残しています。

package com.example.tests;

import java.io.File;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.io.FileUtils;
import org.junit.*;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.chrome.ChromeDriver;



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

    // Chromeを起動する
    System.setProperty("webdriver.chrome.driver","./driver/chromedriver.exe");
    driver = new ChromeDriver();

    // IEを起動する
    // System.setProperty("webdriver.ie.driver", "./driver/IEDriverServer.exe"); 
    // driver = new InternetExplorerDriver();
  }

  @Test
  public void testSample(){
    String[][] params;
    // キャプチャを取得したいページ数
    params = new String[3][];//params[0],params[1],params[2],の3つの配列を用意する

    String[] param;
    //params[0]の設定
    param = new String[2];
    param[0] = "https://www.google.co.jp/";
    param[1] = generateFileName(param[0]);
    params[0] = param;

    //params[1]の設定
    param = new String[2];
    param[0] = "http://www.yahoo.co.jp/";
    param[1] = generateFileName(param[0]);
    params[1] = param;

    //params[2]の設定
    param = new String[2];
    param[0] = "http://blog.asial.co.jp/1180";
    param[1] = generateFileName(param[0]);
    params[2] = param;

    try {
      for (int i = 0; i < params.length; i++) {
      String[] option = params[i];
      capture(option[0], option[1]);
      }
    } catch (WebDriverException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }//end of @Test

  @After
  public void tearDown(){
    //ブラウザを閉じる
    driver.quit();
  }
  public void capture(String url, String filename) throws Exception{
    driver.get(url);
    FileUtils.copyFile(
      ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE),
      new File(filename)
    );
  }
  public static String generateFileName(String str1){
    String regex1 = "(http[s]*://[a-z]*.[a-z]*.[a-z]*.[a-z]*/)";
    Pattern p1 = Pattern.compile(regex1);
    Matcher m1 = p1.matcher(str1);
    String str2 = m1.replaceAll("");
    String regex2 = "(/)";
    Pattern p2 = Pattern.compile(regex2);
    Matcher m2 = p2.matcher(str2);
    String str3 = m2.replaceAll("__");
    return str3+".png";
  }
}

プロセスについて

ブラウザを閉じてもプロセスが残ってしまうので、タスクマネージャーからプロセスの終了をしてあげると良さそう。