求知 文章 文库 Lib 视频 iPerson 课程 认证 咨询 工具 讲座 Model Center   Code  
会员   
要资料
 
 

Selenium教程
Selenium概述
Selenium IDE
Selenium IDE下载
Selenium IDE 工具特点
Selenium IDE测试创建
Selenium IDE测试
Selenium IDE验证点
Selenium - IDE模式匹配
Selenium用户扩展
Selenium IDE- 不同的浏览器
Selenium 环境安装设置
Selenium RC
Selenium - Selenese命令
Selenium Webdriver
Selenium定位器
文本框的相互作用
单选按钮互动
复选框交互
下拉框交互
Synchronization 同步
拖放
键盘操作
鼠标操作
多选择操作
查找所有链接
Selenium测试设计技术
Selenium页面对象模型
使用Excel数据驱动
log4j日志
异常处理
多浏览器测试
捕捉屏幕截图
捕捉视频
Selenium TestNG
Selenium网格
 
 

捕捉屏幕截图
1006 次浏览
4次  

捕捉屏幕截图

截图捕获功能可以帮助我们在需要在运行时抓取截图,在特别是当故障发生。随着截图的帮助和日志信息,我们将能够更好地分析结果

截图是本地执行和Selenium 网格(远程)处决配置不同。让我们来看看他们每一个例子。

本地主机执行

在下面的例子中,我们将计算百分比之后的截图。请确保给一个有效的路径,用以保存屏幕截图。

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

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;

public class webdriverdemo
{
public static void main(String[] args) throws IOException
{
WebDriver driver = new FirefoxDriver();

//Puts a Implicit wait, Will wait for 10 seconds before throwing exception
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

//Launch website
driver.navigate().to("http://www.calculator.net/");

//Maximize the browser
driver.manage().window().maximize();

// Click on Math Calculators
driver.findElement(By.xpath(".//*[@id='menu']/div[3]/a")).click();

// Click on Percent Calculators
driver.findElement(By.xpath(".//*[@id='menu']/div[4]/div[3]/a")).click();

// Enter value 10 in the first number of the percent Calculator
driver.findElement(By.id("cpar1")).sendKeys("10");

// Enter value 50 in the second number of the percent Calculator
driver.findElement(By.id("cpar2")).sendKeys("50");

// Click Calculate Button
driver.findElement(By.xpath(".//*[@id='content']/table/tbody/tr/td[2]/input")).click();

// Get the Result Text based on its xpath
String result = driver.findElement(By.xpath(".//*[@id='content']/p[2]/span/font/b")).getText();

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

FileUtils.copyFile(screenshot, new File("D:screenshotsscreenshots1.jpg"));

//Print a Log In message to the screen
System.out.println(" The Result is " + result);

//Close the Browser.
driver.close();
}
}

输出

在执行这个脚本,截图保存在“D:screenshots”文件夹中名为'screenshots1.jpg“,如下图所示。

Selenium网格- 捕捉屏幕截图

当Selenium网格工作,我们应该确保从远程系统采取正确的截图。我们将充分利用增强的驱动程序。

例子

我们将连接到集线器Firefox的节点上执行该脚本。更多关于配置集线器和节点,请参阅Selenium网格章节。

package TestNG;

import org.openqa.selenium.remote.Augmenter;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.TakesScreenshot;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.*;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
import java.io.File;
import java.net.URL;
import java.net.MalformedURLException;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.remote.RemoteWebDriver;
import java.io.IOException;

public class TestNGClass
{
public WebDriver driver;
public String URL, Node;
protected ThreadLocal<RemoteWebDriver> threadDriver = null;

@Parameters("browser")
@BeforeTest
public void launchapp(String browser) throws MalformedURLException
{
String URL = "http://www.calculator.net";
if (browser.equalsIgnoreCase("firefox"))
{
System.out.println(" Executing on FireFox");
String Node = "http://10.112.66.52:5555/wd/hub";
DesiredCapabilities cap = DesiredCapabilities.firefox();
cap.setBrowserName("firefox");

driver = new RemoteWebDriver(new URL(Node), cap);
//Puts a Implicit wait, Will wait for 10 seconds before
throwing exception
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

//Launch website
driver.navigate().to(URL);
driver.manage().window().maximize();
}
else
{
throw new IllegalArgumentException("The Browser Type is
Undefined");
}
}


@Test
public void calculatepercent() throws IOException
{
driver.findElement(By.xpath(".//*[@id='menu']/div[3]/a")).click();
// Click on Math Calculators
driver.findElement(By.xpath(".//*[@id='menu']/div[4]/div[3]/a"))
.click(); // Click on Percent Calculators

// Make use of augmented Driver to capture Screenshots.
WebDriver augmentedDriver = new Augmenter().augment(driver);
File screenshot = ((TakesScreenshot)augmentedDriver).getScreenshotAs
(OutputType.FILE);
FileUtils.copyFile(screenshot, new File("D:screenshots
emotescreenshot1.jpg"));

// Please note - Screenshot would be saved on the system where the script is executed and NOT on remote machine.

driver.findElement(By.id("cpar1")).sendKeys("10");
// Enter value 10 in the first number of the percent Calculator
driver.findElement(By.id("cpar2")).sendKeys("50");
// Enter value 50 in the second number of the percent Calculator
driver.findElement(By.xpath(".//*[@id='content']/table/tbody/tr/td[2]/input")).click();
// Click Calculate Button
String result = driver.findElement(By.xpath(".//*[@id='content']/p[2]/span/font/b")).getText();
// Get the Result Text based on its xpath
System.out.println(" The Result is " + result); //Print a Log In message to the screen

if(result.equals("5"))
{
System.out.println(" The Result is Pass");
}
else
{
System.out.println(" The Result is Fail");
}
}

@AfterTest
public void closeBrowser()
{
driver.quit();
}
}

输出

当执行该脚本,截图被捕获并储存在指定的位置,如下所示。


您可以捐助,支持我们的公益事业。

1元 10元 50元





认证码: 验证码,看不清楚?请点击刷新验证码 必填



1006 次浏览
4次
 捐助