selenium-java,UI自动化截图方法
截图方法:
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; public class Aiding_Method { /* * 参数 * String screenPath:文件路径 * 例:D:\\图片\\新增-上传-返回.jpg * WebDriver driver:测试类定义的driver对象 */ public void takeScreenshot(String screenPath, WebDriver driver) { try { File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);//OutputType.FILE--截幕保存为图片 FileUtils.copyFile(scrFile, new File(screenPath));//把图片保存到指定路径 } catch (IOException e) { System.out.println("截图出现错误"); } } }
使用示例:
public class Test { private WebDriver driver; @Before public void setUp() throws Exception { element_operation = new Element_operation(); System.setProperty("webdriver.chrome.driver","D:\\Configuration\\chromedriver.exe");//谷歌浏览器路径 driver = new ChromeDriver(); driver.manage().window().maximize();//最大化浏览器 driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); driver.get("http://www.baidu.com"); } @After public void tearDown() throws Exception { try { Thread.sleep(1000); } catch (Exception e) { // TODO: handle exception } driver.quit(); } @Test public void test1() throws ParseException{ Aiding_Method aiding_Method = new Aiding_Method(); aiding_Method.takeScreenshot("C:\\Users\\Administrator\\Desktop\\1.jpg", driver); } }