使用 WatiN 验证二进制内容

发布于 2024-10-07 07:32:49 字数 236 浏览 1 评论 0原文

我正在测试一个提供一些二进制(即非 HTML)内容的网站。有些链接直接提供自定义图像,其他链接则提供自定义 PDF。

我正在 WatiN 和 NUnit 中构建测试用例。想知道是否有办法,使用 WatiN 让它加载页面,然后获取该页面的 byte[] 内容。

目前,我只是启动一个 WebClient 来获取内容,而不是通过 WatiN,但这在我的测试套件中是更多未经测试的代码。

还有其他人这样做并有建议吗?

I'm testing a site that serves up some binary (i.e. non-HTML) content. Some links directly deliver custom images, other links custom PDF's.

I'm building test cases in WatiN and NUnit. Wondering if there's a way, using WatiN to get it load a page and then get the byte[] contents of that page.

Currently, I'm just launching a WebClient to grab the content, rather than through WatiN but that's more untested code in my test suite.

Anyone else doing this and have suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦过后 2024-10-14 07:32:49

跳过 Watin 并使用 HttpWebRequest 直接从 C# 获取内容(在您的测试或测试库中)可能会更容易。

K Scott Allen 有一篇关于使用 HttpWebrequest 获取二进制数据的博客文章:http://odetocode.com/Blogs/scott/archive/2004/10/05/webrequest-and-binary-data.aspx

如果您事先不知道链接目标(例如动态生成的 URL),然后使用 Watin 获取链接,然后使用 HttpWebRequest 获取内容。

引用自 K Scott Allen 的博客文章 应该可以得到你想要的 Byte[]

byte[] result;
byte[] buffer = new byte[4096];

WebRequest wr = WebRequest.Create(someUrl);

using(WebResponse response = wr.GetResponse())
{
   using(Stream responseStream = response.GetResponseStream())
   {
      using(MemoryStream memoryStream = new MemoryStream())
      {
         int count = 0;
         do
         {
            count = responseStream.Read(buffer, 0, buffer.Length);
            memoryStream.Write(buffer, 0, count);
         } while(count != 0);
         result = memoryStream.ToArray();
      }
   }
}

It might be easier to skip Watin and use HttpWebRequest to grab the content straight from C# (in your test, or test library).

K Scott Allen has a blog post on getting Binary data using HttpWebrequest here: http://odetocode.com/Blogs/scott/archive/2004/10/05/webrequest-and-binary-data.aspx

If you don't know the link target in advance (dynamically generated URLs for example), then use Watin to get the link, then HttpWebRequest to get the content.

Quote from K Scott Allen's blog post above, should get you the Byte[] you're after

byte[] result;
byte[] buffer = new byte[4096];

WebRequest wr = WebRequest.Create(someUrl);

using(WebResponse response = wr.GetResponse())
{
   using(Stream responseStream = response.GetResponseStream())
   {
      using(MemoryStream memoryStream = new MemoryStream())
      {
         int count = 0;
         do
         {
            count = responseStream.Read(buffer, 0, buffer.Length);
            memoryStream.Write(buffer, 0, count);
         } while(count != 0);
         result = memoryStream.ToArray();
      }
   }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文