在 Selenium 2 中截取测试屏幕截图的最佳方法? [英] Best way to take screenshots of tests in Selenium 2?

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

问题描述

我需要一种方法来截取我的功能测试的屏幕截图.现在我正在使用 Selenium 2 和 C# 绑定.我非常想在测试结束时截取屏幕截图,以确保显示所需的页面.有没有你们知道的特定工具可以合并到我的 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 中截取测试屏幕截图的最佳方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆