Motomichi Works Blog

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

windows7+eclipse+Selenium WebDriverその0008(JUnitのテスト結果をエクスポートする)

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

Seleniumのテスト結果レポートをもっとおしゃれに | 品質向上ブログ

環境

pom.xmlの記述内容

参考ページの<build>~中略~</build>をそのままコピペしたら上手く動かなかったので、とりあえず以下のような感じにした。

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.example.tests</groupId>
  <artifactId>hoge-sample</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <dependencies>
    <dependency>
      <groupId>org.seleniumhq.selenium</groupId>
      <artifactId>selenium-java</artifactId>
      <version>2.45.0</version>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
    </dependency>
    <!-- dependency for Adapter -->
    <dependency>
      <groupId>ru.yandex.qatools.allure</groupId>
      <artifactId>allure-junit-adaptor</artifactId>
      <version>1.4.4</version>
    </dependency>
    <!-- dependency for Adapter -->
  </dependencies>
  <build>
    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.18.1</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
  <!-- Setting for Report -->
  <reporting>
    <excludeDefaults>true</excludeDefaults>
    <plugins>
      <plugin>
        <groupId>ru.yandex.qatools.allure</groupId>
        <artifactId>allure-maven-plugin</artifactId>
        <version>2.0</version>
        <configuration>
          <reportVersion>1.4.4</reportVersion>
        </configuration>
      </plugin>
    </plugins>
  </reporting>
  <!-- Setting for Report -->
</project>

pom.xmlを編集したのでUpdate Project

プロジェクトhoge-sampleを右クリックして、Maven > Update Project...をクリックした。

HogeTest.javaの記述内容

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";
  }
}

テスト実行(Maven build)

  • プロジェクトhoge-sampleをクリック
  • メニューのRun > Run Configurationsをクリック
  • Goalsのところにtestと入力して、Runをクリックするとテストが実行される。

Goalsにtestと入力して実行すると、結果がC:\Users\ユーザー名\workspace\hoge-sample\target\surefire-reportsに保存される。
maven-surefire-pluginがtxtとxmlを出力してくれているっぽい。

Consoleに表示された情報

以下のような感じに表示された。

[INFO] Scanning for projects...
[INFO] 
[INFO] Using the builder org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder with a thread count of 1
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building hoge-sample 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ hoge-sample ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:2.5.1:compile (default-compile) @ hoge-sample ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ hoge-sample ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:2.5.1:testCompile (default-testCompile) @ hoge-sample ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-surefire-plugin:2.18.1:test (default-test) @ hoge-sample ---
[INFO] Surefire report directory: C:\Users\smotomichi\workspace\hoge-sample\target\surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running HogeTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 26.787 sec - in HogeTest

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 43.517 s
[INFO] Finished at: 2015-06-25T14:15:00+09:00
[INFO] Final Memory: 11M/230M
[INFO] ------------------------------------------------------------------------

Goalsをsiteにしてbuildしてみる

  • プロジェクトhoge-sampleをクリック
  • メニューのRun > Run Configurationsをクリック
  • Goalsのところにsiteと入力して、Runをクリックするとテストが実行される。

失敗した

以下のようにエラーが表示された。

[INFO] Scanning for projects...
[INFO] 
[INFO] Using the builder org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder with a thread count of 1
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building hoge-sample 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-site-plugin:3.3:site (default-site) @ hoge-sample ---
[INFO] configuring report plugin ru.yandex.qatools.allure:allure-maven-plugin:2.0
[WARNING] No project URL defined - decoration links will not be relativized!
[INFO] Rendering site with org.apache.maven.skins:maven-default-skin:jar:1.0 skin.
[INFO] Generating "Allure" report    --- allure-maven-plugin:2.0
[INFO] Report Version: 1.4.4
[INFO] Results Pattern: **/allure-results
[INFO] Found [0] results directories by pattern [**/allure-results]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 23.762 s
[INFO] Finished at: 2015-06-25T14:22:10+09:00
[INFO] Final Memory: 15M/182M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-site-plugin:3.3:site (default-site) on project hoge-sample: Error during page generation: Error rendering Maven report: Can't find any results directories by pattern [**/allure-results] -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

いろいろいじっていたらhtml形式のレポートっぽいのでたけどテストは実行されていない

参考ページのソースをまるごと自分のpom.smlに盛り込んで、少し編集して、以下のようにしてみたらhtml形式のレポートが出力されたけど、テストの実行結果が0件だった。

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.example.tests</groupId>
  <artifactId>hoge-sample</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <dependencies>
    <dependency>
      <groupId>org.seleniumhq.selenium</groupId>
      <artifactId>selenium-java</artifactId>
      <version>2.45.0</version>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
    </dependency>
    <!-- dependency for Adapter -->
    <dependency>
      <groupId>ru.yandex.qatools.allure</groupId>
      <artifactId>allure-junit-adaptor</artifactId>
      <version>1.4.4</version>
    </dependency>
    <!-- dependency for Adapter -->
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.0</version>
        <configuration>
          <source>1.7</source>
          <target>1.7</target>
        </configuration>
      </plugin>
      <!-- unit test run setting for Adapter -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.14</version>
        <configuration>
          <testFailureIgnore>false</testFailureIgnore>
          <argLine>
            -javaagent:${settings.localRepository}/org/aspectj/aspectjweaver/1.8.3/aspectjweaver-1.8.3.jar
          </argLine>
          <properties>
            <property>
              <name>listener</name>
              <value>ru.yandex.qatools.allure.junit.AllureRunListener</value>
            </property>
          </properties>
        </configuration>
        <dependencies>
          <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.3</version>
          </dependency>
        </dependencies>
      </plugin>
      <!-- unit test run setting for Adapter -->
    </plugins>
  </build>
  <!-- Setting for Report -->
  <reporting>
    <excludeDefaults>true</excludeDefaults>
    <plugins>
        <plugin>
            <groupId>ru.yandex.qatools.allure</groupId>
            <artifactId>allure-maven-plugin</artifactId>
            <version>2.2</version>
            <configuration>
              <resultsPattern>target/allure</resultsPattern>
              <reportPath>target/report</reportPath>
              <reportVersion>1.4.4</reportVersion>
            </configuration>
        </plugin>
    </plugins>
  </reporting>
  <!-- Setting for Report -->
</project>

C:\Users\smotomichi\workspace\hoge-sample\target\sitecssとかimagesとかなんか色々生成された。

具体的にやったこととしては以下のようなこと。

  • 環境変数JAVA_HOMEが無かったので、JAVA_HOMEを設定した。
  • pom.xmlを変更したので、Update Projectを実行した。
  • C:\Users\smotomichi\workspace\hoge-sample\targetにallureフォルダを作た。
  • プロジェクトを右クリックして、Refreshを実行した。

ひとまずこの辺で諦めようか

あんまり時間を使うのもなんだし、txt出せてるから最低限の目的は達成した。

${settings.localRepository}はデフォルト設定値が有効なら、たぶんC:\Users\smotomichi\.m2\repositoryのはず。

今度、JUnit · allure-framework/allure-core Wiki · GitHubとか見ながらもう少しだけいじってダメだったら諦めよう。

sahaginというのがあるらしいので、そっちを試してみるのはどうか。と少し思う。

一旦ここまで。