注意:当前,我正在使用Selenium Webdriver(
junit
),并且我不知道如何编写添加代码屏幕截图的方法。#1 楼
截取@After
测试拆卸方法中的屏幕截图,该方法在每次测试后运行。这样,您将始终获得通过和未通过测试的屏幕截图。public class TestSample
{
static WebDriver driver;
@Before
public void setUp() {
//start new webdriver session, for eg using firefox
driver = new FirefoxDriver();
}
@Test
public void aTest() {
driver.get("http://www.google.com/");
//more test logic - test might pass or fail at this point
}
@After
public void tearDown() {
// take the screenshot at the end of every test
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
// now save the screenshto to a file some place
FileUtils.copyFile(scrFile, new File("c:\tmp\screenshot.png"));
//quit WebDriver session
driver.quit();
}
}
#2 楼
我写了一个博客,介绍如何使用Webdriver API拍摄屏幕快照,这是一个基本代码,但是它将帮助您拍摄屏幕截图。#3 楼
如何在MAC O / S中的Selenium Webdriver 2中拍摄屏幕快照需要导入->
package generic;
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;
@Test
public void browserValidate() throws IOException, InterruptedException {
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
Thread.sleep(4000);
FileUtils.copyFile(scrFile, new File("/Users/mhossain/Desktop/Mixed/screenshot.png"));
}
#4 楼
MultiScreenShot我在Github中找到了以下有关Multiscreenshot的有用链接
评论
您能否进一步解释一下该工具是什么以及如何解决此需求?谢谢!
– Paul Muir
2014-12-16 13:45
#5 楼
该命令应为:captureEntireScreenhot
(可在命令中使用),目标应该是要保存屏幕快照的文件夹的地址。必须手动编写。要求的示例:
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com/");
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("c:\saving location\filename.png"))`;
希望这可以解决您的问题。
评论
嗨,Atlaf,您能写一个小的代码段吗?它像您的答案看起来那样简单吗?我的意思是,也许是。但是一些代码会很有用。另外,“必须手动编写”是什么意思?
–corsiKa♦
2014年11月5日,0:23
亲爱的corsika答案对您有所帮助。
– Altaf
2014年11月5日,下午3:47
Altaf很有趣,它没有使用您在答案中提到的captureEntireScreenshot。还知道您可以编辑答案,而不是发布新答案。我建议您选择一个将代码编辑到其中,然后删除另一个。
–corsiKa♦
2014年11月5日4:30
#6 楼
我不确定如何手动执行操作,但是有一个测试框架(thucydides)可以满足您的需求。该工具实际上将Selenium Webdriver与其他工具集成在一起,使您的生活更轻松。
您可以非常快速地设置测试Maven项目。由于它是开源的,因此如果您不想弄乱其他工具,可以检查它们如何捕获屏幕截图。但是,我必须说,使用这样的工具可以使工作更加轻松...
评论
Martin,如果您明确声明Thucydides扩展了Webdriver,那将有帮助。从未听说过该特定工具的人可能会认为您的回答与问题无关。
–凯特·保罗(Kate Paulk)
13年11月13日在18:20
#7 楼
由于到目前为止尚未提及:有Shutterbug。Selenium Shutterbug是一个用Java编写的实用程序库,用于使用Selenium WebDriver制作屏幕截图,并进一步自定义,比较和处理它们。 Java AWT的帮助。
整个页面的屏幕截图:
Shutterbug.shootPage(driver, ScrollStrategy.WHOLE_PAGE)
.save("/path/to/screenshots/");
获取单个元素的图像:
Shutterbug.shootElement( driver, element )
.getImage()
您还可以添加突出显示,标题,文本等:
Shutterbug.shootPage(driver)
.blur(searchBox)
.highlight(searchBtn)
.monochrome(googleLogo)
.highlightWithText(googleLogo, Color.blue, 3, "Monochromed logo", Color.blue, new Font("SansSerif", Font.BOLD, 20))
.highlightWithText(searchBox, "Blurred secret words")
.withTitle("Google home page - " + new Date())
.withName("home_page")
.withThumbnail(0.7)
.save("/path/to/screenshots/");
评论
您明确指出这是您的帖子(而不仅仅是随机垃圾邮件),这是一个积极的信号,值得赞赏。我要指出的一点是,我们倾向于不鼓励仅链接的答案。您能否通过一些代码示例和少量解释使这个答案独立存在,保留指向您博客的链接以供进一步阅读?谢谢!
–corsiKa♦
2014-10-24 14:48
感谢您的评论。我已经编辑了代码。请让我知道是否可以使其更加清晰和精确。
–udit choudhary
2014-10-25 9:34
好多了。这是一个独立的答案,然后链接到博客以获取更多信息。作为一个侧面说明,我修复了格式设置(代码块前面必须有4个空格,请查看源代码(编辑)以了解我的操作方式),以便使它看起来更简洁。但这很好,谢谢大家!
–corsiKa♦
14-10-26在2:50