在 Selenium 2 中截取测试屏幕截图的最佳方法是什么?

Best way to take screenshots of tests in Selenium 2?(在 Selenium 2 中截取测试屏幕截图的最佳方法是什么?)

本文介绍了在 Selenium 2 中截取测试屏幕截图的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一种方法来截取我的功能测试的屏幕截图.现在我正在使用带有 C# 绑定的 Selenium 2.我非常想在测试结束时截屏以确保显示所需的页面.你们知道我可以将哪些特定工具合并到我的 C# 代码中来触发屏幕截图吗?我找不到内置的 Selenium 2 解决方案(不仔细看).

I need a way to take screenshots of my functional tests. Right now I'm using Selenium 2 with C# bindings. I pretty much want to take a screenshot at the end of the test to make sure the desired page is displayed. Are there any particular tools you guys know of that I can incorporate into my C# code that will trigger a screenshot? I couldn't find a built-in Selenium 2 solution (without looking it over).

推荐答案

要在 Selenium 2 中进行屏幕截图,您需要执行以下操作

To do screenshots in Selenium 2 you need to do the following

driver = new FireFoxDriver(); // Should work in other Browser Drivers
driver.Navigate().GoToUrl("http://www.theautomatedtester.co.uk");
Screenshot ss = ((ITakesScreenshot) driver).GetScreenshot();

//Use it as you want now
string screenshot = ss.AsBase64EncodedString;
byte[] screenshotAsByteArray = ss.AsByteArray;
ss.SaveAsFile("filename", ImageFormat.Png); //use any of the built in image formating
ss.ToString();//same as string screenshot = ss.AsBase64EncodedString;

该代码应该可以工作,因为我在 IronPython Repl 中快速对其进行了测试.请参阅下面的 IronPython 代码

That code should work, as I quickly tested it in IronPython Repl. See the IronPython code below

import clr
clr.AddReference("WebDriver.Common.dll")
clr.AddReference("WebDriver.Firefox.dll")
from OpenQA.Selenium import *
from OpenQA.Selenium.Firefox import *
driver = FirefoxDriver()
driver.Navigate().GoToUrl("http://www.theautomatedtester.co.uk")
s = driver.GetScreenshot()
s.AsBaseEncodedString
# HUGE string appears in the REPL

这篇关于在 Selenium 2 中截取测试屏幕截图的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:在 Selenium 2 中截取测试屏幕截图的最佳方法是什么?

基础教程推荐