• <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>
  • WebDriver 之測試失敗自動截圖(2)

    發表于:2012-08-24來源:test China作者:lihuazhang點擊數: 標簽:WebDriver
    import java.net.URL; import org.openqa.selenium.OutputType; import org.openqa.selenium.TakesScreenshot; import org.openqa.selenium.WebDriverException; import org.openqa.selenium.remote.CapabilityType;

      import java.net.URL;

      import org.openqa.selenium.OutputType;

      import org.openqa.selenium.TakesScreenshot;

      import org.openqa.selenium.WebDriverException;

      import org.openqa.selenium.remote.CapabilityType;

      import org.openqa.selenium.remote.DesiredCapabilities;

      import org.openqa.selenium.remote.DriverCommand;

      import org.openqa.selenium.remote.RemoteWebDriver;

      public class CustomRemoteWebDriver extends RemoteWebDriver implements

      TakesScreenshot {

      public CustomRemoteWebDriver(URL url, DesiredCapabilities dc) {

      super(url, dc);

      }

      @Override

      public X getScreenshotAs(OutputType target)

      throws WebDriverException {

      if ((Boolean) getCapabilities().getCapability(

      CapabilityType.TAKES_SCREENSHOT)) {

      return target

      .convertFromBase64Png(execute(DriverCommand.SCREENSHOT)

      .getValue().toString());

      }

      return null;

      }

      }

      然后,我們在加一個封裝類, 將截圖方法放進去。

      WebDriverWrapper.screenShot :

      /**

      * Function to take the screen shot and save it to the classpath dir.

      * Usually, you will find the png file under the project root.

      *

      * @param driver

      * Webdriver instance

      * @param desc

      * The description of the png

      */

      public static void screenShot(WebDriver driver, String desc) {

      Date currentTime = new Date();

      SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd-hh-mm-ss");

      String dateString = formatter.format(currentTime);

      File scrFile = ((TakesScreenshot) driver)

      .getScreenshotAs(OutputType.FILE);

      try {

      desc = desc.trim().equals("") ? "" : "-" + desc.trim();

      File screenshot = new File("screenshot" + File.separator

      + dateString + desc + ".png");

      FileUtils.copyFile(scrFile, screenshot);

      } catch (IOException e) {

      e.printStackTrace();

      }

      }

      下面,就是添加 Junit 的 TestRule:

      import org.junit.rules.TestRule;

      import org.junit.runner.Description;

      import org.junit.runners.model.Statement;

      import org.openqa.selenium.WebDriver;

      public class TakeScreenshotOnFailureRule implements TestRule {

      private final WebDriver driver;

      public TakeScreenshotOnFailureRule(WebDriver driver) {

      this.driver = driver;

      }

      @Override

      public Statement apply(final Statement base, Description description) {

      return new Statement() {

      @Override

      public void evaluate() throws Throwable {

      try {

      base.evaluate();

      }

      catch (Throwable throwable) {

      WebDriverWrapper.screenShot(driver, "assert-fail");

      throw throwable;

      }

      }

      };

      }

      }

      代碼很簡單,在拋出 evalate 方法的錯誤之前,截圖。

      然后就是使用這個 TestRule, 很簡單,只要在你的 測試用例里面加入:

      public class MyTest {

      ...

      @Rule

      public TestRule myScreenshot = new TakeScreenshotOnFailureRule(driver);

      ...

      @Test

      public void test1() {}

      @Test

      public void test2() {}

      ...

      }

      即可。關于 Junit 的 Rule 請自行 google!

      兩則的比較

      總得來說,兩種方法都很方便, 也很有效果, 基本都能截圖成功。

      不同之處在于,

      RemoteWebDriver 監聽器是在 RemoteWebDriver 拋出異常的時候截圖。

      TestRule 是在 assert 失敗的時候截圖。

      我在項目中最早是用第一種方法,后來改用第二種,主要是因為,在自定義的監聽器里, 它遇到所有的異常都會截圖,這個時候,如果你用了 condition wait 一個 Ajax 的元素, 那就會很悲劇,你會發現在你的目錄下面有無數的截圖。當初我沒有找到解決方法,期待有人提出。

    原文轉自:http://www.kjueaiud.com

    老湿亚洲永久精品ww47香蕉图片_日韩欧美中文字幕北美法律_国产AV永久无码天堂影院_久久婷婷综合色丁香五月

  • <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>