testing - 使用 TestCafe 评估颜色变化
问题描述
为了独立于静态文本,我想检查选择器的 RGB 值。我发现
const opacity = await selector.getStyleProperty('opacity');
await t.expect(opacity).eql(1, {timeout: 5000})
将改变的元素是:
<div class="cl-asset-wfstate" style="background-color: rgb(244, 159, 79);"></div>
<div class="cl-asset-wfstate" style="background-color: rgb(92, 195, 55);"></div>
所以我尝试了
const bgcolour = await Selector('div.cl-asset-wfstate').getStyleProperty('background-color');
await t.expect(bgcolour).eql('rgb(92, 195, 55)', {timeout: 5000})
但这不起作用。断言永远不会得到解决。有什么建议吗?
解决方案
如果我理解正确的话,你有一个div
元素,它在测试执行期间会改变它的颜色。如果是这样,您不需要执行此行:
const bgcolour = await Selector('div.cl-asset-wfstate').getStyleProperty('background-color')
因为在这种情况下,bgcolour
变量将包含一个不会改变的值。
我修改了您的示例以演示如何实现您的场景:
HTML:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<div class="cl-asset-wfstate" style="background-color: rgb(0, 0, 0);">test</div>
<script>
setTimeout(function () {
document.querySelector('div').style.backgroundColor = 'rgb(92, 195, 55)';
}, 3000);
</script>
</body>
</html>
测试代码:
import { Selector } from 'testcafe';
fixture `fixture`
.page `../pages/index.html`;
test('1', async t => {
const selector = Selector('div.cl-asset-wfstate');
const opacity = await selector.getStyleProperty('opacity');
await t.expect(opacity).eql('1', {timeout: 5000});
await t.expect(selector.getStyleProperty('background-color')).eql('rgb(92, 195, 55)', {timeout: 5000})
});
另请参阅:选择器对象
推荐阅读
- python - 自定义 Django 表单 - 根据请求修改已清理的数据
- php - 允许将 WooCommerce 中特定用户角色的相同父类别的产品添加到购物车
- ruby-on-rails - 这是做什么的:`const { $ } = require("@rails/ujs");` 在我的 Rails 应用程序中?它似乎导致错误
- openlayers - 将给定点移动 X,Y 米 Openlayers
- python - AttributeError:“MySQLConverter”对象没有属性“_tuple_to_mysql”
- php - PHP 对象的属性可以是数组吗?如果是这样,我如何从对象中添加它?
- azure - Azure Purview 无法对 Azure 数据资源管理器数据库进行分类
- sql-server - 弹性作业代理立即在逻辑应用中的作业超时
- javascript - 引导模式不会出现在 laravel 项目中
- python - Discord.py - 对 .event on_message 应用冷却时间