zoukankan      html  css  js  c++  java
  • 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);
        }
    }
  • 相关阅读:
    clearfix 清除浮动的问题
    python第四十五课——继承性之多继承
    Linux基础第六课——grep|awk|sort|uniq
    Linux基础第五课——用户管理
    Linux基础第四课——文件操作
    Linux第三课——目录操作
    Linux基础第二课——系统架构
    Linux基础第一课——基础知识了解
    01 http协议概念及工作流程
    18- php Redis扩展编译
  • 原文地址:https://www.cnblogs.com/yanzhe/p/7516232.html
Copyright © 2011-2022 走看看