node.js - 是否可以使用 TestCafé 比较两个图像以验证确切的图像是否可用?
问题描述
在我们的项目中,图标很少。我们能否使用 TestCafe 测试这些图像/图标。
示例代码:
<a href="www.fb.com/" class="fa fa-facebook-square"> </a>
预期结果:
- 图 1 -> 本地存储
- 图片 2 -> 可在网站上找到。
我需要调用本地图像并将其与网站图像进行比较。
解决方案
我可以建议两种方法来比较网站上的图像。
一种方法是对带有图像的 DOM 元素进行截图,并使用第三方库将其与本地图像进行比较。例如,查看外观相同的库。
另一种方法是使用RequestLogger记录对图像的请求,并使用Buffer.compare()方法将响应正文与本地文件进行比较。请参阅下面说明此方法的示例:
import fs from 'fs';
import { RequestLogger } from 'testcafe';
const logger = RequestLogger('https://devexpress.github.io/testcafe/images/landing-page/banner-image.png', {
logResponseBody: true
});
fixture `Compare images`
.page`https://devexpress.github.io/testcafe/`;
test
.requestHooks(logger)
('Test', async t => {
await t.expect(logger.count(record => record.response.statusCode === 200)).eql(1);
const actualImageBuffer = logger.requests[0].response.body;
const expectedImageBuffer = fs.readFileSync('c:\\Tests\\images\\banner-image.png');
await t.expect(Buffer.compare(actualImageBuff, expectedImageBuff)).eql(0);
});
推荐阅读
- c++ - vptr调用虚函数
- r - Fedora 28 中的 libgfortran.so.4
- bootstrap-4 - Bootstrap4 堆叠卡
- c# - 在 GridView1_RowDataBound 中找不到控件
- c# - 如何加载递归实体而不在根级别加载子实体?
- r - 更改选定列的列名
- android - 即使添加了侦听器,Android FirebaseListAdapter 也无法正常工作
- resharper - 如何在 ReSharper 中刷新“单元测试会话”结果?(粘性失败测试)
- python - 如何在 Flask 中构造使用相同功能的多个路由和渲染模板
- android - Phonegap - 构建失败并出现异常