7

障害が発生するたびにスクリーンショットをキャプチャし、次のコードを記述しようとしていますが、これは機能しません。

public class TestFile {

    WebDriver driver = new FirefoxDriver();

    @Test   
    public void Testone(){
        driver.get("http://www.google.com/");           
    }

    @AfterMethod(alwaysRun=true)
    public void catchExceptions(ITestResult result){
        System.out.println("result"+result);
        String methodName = result.getName();
        System.out.println(methodName);

        if(!result.isSuccess()){         
            try { 
                File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
                FileUtils.copyFile(scrFile,new File("C:\\screenshot2.png" ));
            } catch (IOException e1) {
                e1.printStackTrace();
            }       
        }
    }
}

これはで失敗しています

ファイルscrFile=((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);


スタックトレース:

[TestNG] Running:
C:\Documents and Settings\537310\Local Settings\Temp\testng-eclipse-1576306112\testng-customsuite.xml

result[TestResult name=Testone status=FAILURE method=TestFile.Testone()[pri:0, instance:com.example.tests.TestFile@1b34126] output={null}]
FAILED CONFIGURATION: @AfterMethod catchExceptions([TestResult name=Testone status=FAILURE method=TestFile.Testone()[pri:0, instance:com.example.tests.TestFile@1b34126] output={null}])
net.sf.cglib.core.CodeGenerationException: java.lang.IllegalAccessException-->Class org.openqa.selenium.remote.Augmenter$CompoundHandler can not access a member of class org.openqa.selenium.firefox.FirefoxDriver with modifiers "protected"
at net.sf.cglib.core.ReflectUtils.newInstance(ReflectUtils.java:235)

輸入品のリスト:

package com.example.tests;
import org.testng.annotations.Test;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import org.apache.commons.io.FileUtils;
import org.junit.Before;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.Augmenter;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.ITestResult;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;  
4

3 に答える 3

2

共有したスタックトレースはスタックトレースではありませんが、testngログだと思います。
あなたが提供した例は実際に機能します。@AfterMethodでは、テストが失敗した場合にのみスクリーンショットが撮られるため、テストを失敗させました。if(!result.isSuccess())
次に、例を再度実行すると、次のようになります。java.io.FileNotFoundException
:C: \ screenshot2.png(アクセスが拒否されました)
次に、画像の場所をDに変更しました。権限が正しく、エンドツーエンドで機能し、スクリーンショットを見ることができます。

乾杯

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.ITestResult;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.Test;

public class TestFile {

    WebDriver driver = new FirefoxDriver();

    @Test
    public void Testone() {

        driver.get("http://www.google.com/");
        assert false;

    }

    @AfterMethod(alwaysRun = true)
    public void catchExceptions(ITestResult result) {
        System.out.println("result" + result);
        String methodName = result.getName();
        System.out.println(methodName);

        if (!result.isSuccess()) {

            try {

                File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
                FileUtils.copyFile(scrFile, new File("C:\\screenshot2.png"));
            } catch (IOException e1) {
                e1.printStackTrace();
            }

        }
    }
}
于 2012-12-06T08:24:42.403 に答える
1

こんにちはsinisa229mihajlovski、

スクリプトは正しく機能しています。ただし、スクリプトにわずかな変更があります。「assertfalse」という行にコメントしないと、エラーが発生します。

于 2012-12-06T12:36:52.547 に答える
0

これを試して :

 WebDriver augmentedDriver = new Augmenter().augment(driver);
    File screenshot = ((TakesScreenshot)augmentedDriver).
                        getScreenshotAs(OutputType.FILE);

代わりに

File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);

于 2012-12-05T09:25:46.187 に答える