1

javaを使用してセレンを実行中に障害が発生したときに撮影されるスクリーンショットに、失敗したメソッド名を追加しようとしています。私はネット上で複数のソリューションを試しましたが、それらはすべて、ルール クラスまたはメソッド名のメソッド名を返すことになります。スクリーンショットのファイル名が「shouldFail_date.png」を返すようにする方法がわかりません。

package test;

import org.apache.commons.io.FileUtils;
import org.junit.rules.TestWatcher;
import org.junit.runner.Description;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;

import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class ScreenShotRule extends TestWatcher {
    private WebDriver browser;

    public ScreenShotRule(WebDriver browser) {
        this.browser =  browser;
    }

    @Override
    protected void failed(Throwable e, Description description) {
        TakesScreenshot takesScreenshot = (TakesScreenshot) browser;

        File scrFile = takesScreenshot.getScreenshotAs(OutputType.FILE);
        File destFile = getDestinationFile();
        try {
            FileUtils.copyFile(scrFile, destFile);
        } catch (IOException ioe) {
            throw new RuntimeException(ioe);
        }
    }

    @Override
    protected void finished(Description description) {
        browser.close();
    }

    private File getDestinationFile() {
        Throwable t = new Throwable();
        String callerMethodName = t.getStackTrace()[1].getMethodName();
        DateFormat dateFormat = new SimpleDateFormat("dd_MMM_yyyy");
        String userDirectory = "screenshots/" + dateFormat.format(new Date()) + "/";
        new File(userDirectory).mkdirs();
        String absoluteFileName = userDirectory callerMethodName + dateFormat.format(new Date()) + ".png";

        return new File(absoluteFileName);
    }
}

package test;

import org.junit.*;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class ScreenShotTest {
    private WebDriver browser = new FirefoxDriver();

    @Rule
    public ScreenShotRule screenShootRule = new ScreenShotRule(browser);

    @Test
    public void shouldFail() {
        browser.get("http://www.google.com");
        By link = By.partialLinkText("I do not expect to find a link with this text");
        browser.findElement(link);
    }
}
4

1 に答える 1

0

効率的なセレン テスト フレームワークの 1 つであるISFWを使用できます。ISFWは、testng に基づいており、必要に応じて説明的なレポートを提供します。スナップショット 概要 詳細レポートの一部を次に示します。

于 2013-09-18T18:59:42.090 に答える