有多種方法可以做到這一點。
創建一個與ScreenCapture不同的類文件,並在此類文件內創建兩個方法。
一種方法用於何時成功運行特定的測試用例,另一種方法用於在執行測試腳本期間測試用例失敗的情況。
我給你提供了一個類文件。現在
package com.dummy;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
public class ScreenCapture {
public static void passScreenCapture() throws IOException
{
Date d = new Date();
System.out.println(d.toString());
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss"); // Your each screenshot will be taken as this format "Year-Month-Date-Hours-Minutes-Seconds"
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("D:\\RND\\"+sdf.format(d)+".png")); //your screenshot path and convert date string to SimpleDateFormat because windows can't capture screenshot with(:)
}
public static void failScreenCapture() throws IOException
{
Date d = new Date();
System.out.println(d.toString());
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HHmmss");
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
// Now you can do whatever you need to do with it, for example copy somewhere
FileUtils.copyFile(scrFile, new File("D:\\RND\\"+sdf.format(d)+".png"));
}
}
您的抓屏類文件是用兩種不同的方法一起準備。 你需要調用這個方法,你想調用的地方。
你可以直接調用這個方法到任何類如下。
ScreenCapture.passScreenCapture(); //classname.methodname
ScreenCapture.failScreenCapture();
OR
的另一種方法如下。
創建一個類文件,如下所示。
package com.dummy;
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.testng.annotations.Test;
public class ScreenShots {
public void captureScreen() throws IOException
{
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
// Now you can do whatever you need to do with it, for example copy somewhere
FileUtils.copyFile(scrFile, new File("D:\\RND\\Modulename.png"));
}
}
調用此方法的任何類,你可以調用這個方法,這樣
public void captureScreen() throws Exception
{
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
// Now you can do whatever you need to do with it, for example copy somewhere
FileUtils.copyFile(scrFile, new File("D:\\RND\\Modulepage.png"));
System.out.println("Module Page Screen is taken successfully.");
}
那怎麼服用單截圖知道嗎? – acikojevic
任何想法@acikojevic? –
你只是在尋找如何截圖? http://stackoverflow.com/questions/3422262/take-a-screenshot-with-selenium-webdriver – mrfreester