Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

java截图 #168

Open
v5tech opened this issue Oct 21, 2017 · 0 comments
Open

java截图 #168

v5tech opened this issue Oct 21, 2017 · 0 comments

Comments

@v5tech
Copy link
Owner

v5tech commented Oct 21, 2017

<dependency>
	<groupId>org.apache.commons</groupId>
	<artifactId>commons-lang3</artifactId>
	<version>3.6</version>
</dependency>
<dependency>
	<groupId>com.google.guava</groupId>
	<artifactId>guava</artifactId>
	<version>23.0</version>
</dependency>
<dependency>
	<groupId>org.seleniumhq.selenium</groupId>
	<artifactId>selenium-java</artifactId>
	<version>3.6.0</version>
</dependency>
<dependency>
	<groupId>com.codeborne</groupId>
	<artifactId>phantomjsdriver</artifactId>
	<version>1.2.1</version>
</dependency>
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.phantomjs.PhantomJSDriver;

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

public class TakesScreenshotUtils {

    public static void main(String[] args) throws IOException {
        takesScreenshot1();
        takesScreenshot2();
    }

    public static void takesScreenshot1() throws IOException {
        System.setProperty("webdriver.chrome.driver", "D:\\chromedriver.exe");

        System.setProperty("phantomjs.binary.path", "D:\\phantomjs.exe");

        WebDriver driver = new PhantomJSDriver();

        driver.manage().window().maximize();

        driver.get("http://www.jianshu.com");

        //指定了OutputType.FILE做为参数传递给getScreenshotAs()方法,其含义是将截取的屏幕以文件形式返回。
        File srcFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);

        //利用FileUtils工具类的copyFile()方法保存getScreenshotAs()返回的文件对象。
        FileUtils.copyFile(srcFile, new File("screenshot.png"));

        //关闭浏览器
        driver.quit();
    }

    public static void takesScreenshot2() {
        Runtime rt = Runtime.getRuntime();
        Process process;
        try {
            process = rt.exec("D:\\phantomjs.exe" + " D:\\responsive-screenshot.js " + "http://www.baidu.com");
            try {
                process.waitFor();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant