package rjcs; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.List; import org.apache.commons.io.FileUtils; import org.openqa.selenium.TakesScreenshot; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.By; import org.openqa.selenium.WebElement; import org.testng.annotations.Test; import java.io.File; import java.io.IOException; import org.openqa.selenium.OutputType; import com.thoughtworks.selenium.SeleneseTestNgHelper; public class Navigate { public static void main (String [] args) { System.setProperty ( "webdriver.firefox.bin", "C: \\ Program Files (the x86) \\ \\ firefox.exe the Mozilla Firefox"); // Firefox installation path is provided to prevent the system can not find FirefoxDriver Driver = new new FirefoxDriver (); // initialize FireFox browser instance, and open the browser the try { driver.manage () window () Maximize ();.. // maximum window of the Thread.sleep ( 5000 ); driver.manage () window () Maximize ();.. //Maximize the window Thread.sleep ( 5000 ); driver.get ( "https://www.baidu.com"); // open a URL, a method Thread.sleep ( 5000 ); driver.findElement (By.id ( "kw" .)) the Clear (); driver.findElement (By.id ( . "kw")) sendKeys ( "China" ); driver.findElement (By.id ( "su" )) the Click ();. the Thread .sleep ( 5000 ); the SimpleDateFormat the dateFormat = new new the SimpleDateFormat ( "yyyyMMddHHmmss"); // transition time format Time = dateFormat.format String (Calendar.getInstance () getTime ().); // Get the current time File srcFile = ((TakesScreenshot) Driver) .getScreenshotAs (OutputType.FILE); // execute screen capture FileUtils.copyFile (srcFile , new new file ( "C: \\ \\ del the Users \\ Desktop", Time + ".png")); // use copyFile FileUtils tools class () method to save getScreenshotAs () returns the file; "screenshot" immediate theme folder saved the Thread.sleep ( 5000 ); } the catch (Exception E) { e.printStackTrace (); } the finally { driver.quit (); } } }
java + selenium-- simple screenshot + time + saved in a path
Guess you like
Origin www.cnblogs.com/xiaobaibailongma/p/12214999.html
Ranking