selenium-webdriver截图功能

2012-08-03  王少平 

#ruby脚本:
require "selenium-webdriver"
wd = Selenium::WebDriver.for :ie
wd.manage.timeouts.implicit_wait = 30
wd.navigate.to "http://www.baidu.com"
sleep 5
wd.save_screenshot "c:/test_screenshot#{Time.now.strftime("%s")}.png"
wd.quit

#java:
import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;

import com.google.common.io.Files;

public class FirstExampe {
public static void main(String[] args) {
WebDriver driver = new InternetExplorerDriver();

driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
driver.get("http://www.baidu.com");

try {
Thread.sleep(3000);
} catch (InterruptedException e1) {
e1.printStackTrace();
}

WebElement element = driver.findElement(By.id("kw"));
element.sendKeys("hello Selenium!");
element.submit();

try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}

File tmpFile = ((TakesScreenshot) driver)
.getScreenshotAs(OutputType.FILE);

try {
Files.copy(tmpFile, new File("c:/test1.png"));
System.out.println("截图成功");
} catch (IOException e) {
System.out.println("截图失败");
e.printStackTrace();
}

driver.quit();
}
}
518°/5162 人阅读/2 条评论 发表评论

熊志男  2012-08-04

这是什么脚本呢?


王少平  2012-08-09

熊志男: 这是什么脚本呢?
ruby脚本


登录 后发表评论