gpt4 book ai didi

javascript - Protractor :有错误但测试通过

转载 作者:行者123 更新时间:2023-11-29 10:10:27 25 4
gpt4 key购买 nike

我正在使用 Protractor 和其他库(如 chai、cucumber 和 gherkin)创建一个测试工具。我有三个文件:

  1. my_feature.feature - 用于指定小 cucumber 特征
  2. test_step.js - 此文件包含步骤定义
  3. index.html - 是我正在测试的网页

my_feature.feature

# features/my_feature.feature

Feature: Test cucumber automation
As a front-end developer
I want to automate e2e testing


Scenario: Altro test
Given I go on "file:///Users/Emanuele/Desktop/lavoro/test-automation/app/html/index.html"
Then The text of the element with selector "#test-button" should be "My button"

test_step.js

    'use strict';

var chai = require('chai'),
chaiAsPromised = require('chai-as-promised');

chai.use(chaiAsPromised);

var expect = chai.expect;

// Protractor won't wait for Angular
browser.ignoreSynchronization = true;

module.exports = function() {

this.World = require('../support/world').World;

// default timeout for all step definitions
this.setDefaultTimeout(20 * 1000);

/*
** Open a page based on an absolute url
** Ex. https://www.google.com
*/
this.Given(/^I go on "([^"]*)"$/, function(url, callback){
browser.get(url);

callback();
});

/*
** Check if the text of the element with a given selector is equal to myText.
*/
this.Then(/^The text of the element with selector "([^"]*)" should be "([^"]*)"$/, function(elementSelector, myText, callback){
var selectedElement = element(by.css(elementSelector));

//Resolve the promise
selectedElement.getText().then(function(text){
expect(text).to.equal(myText);
});


callback();
});
};

index.html

<html>
<head></head>
<body>

<button id="test-button">Test button</button>

</body>
</html>

现在,当我运行测试时,我得到了一个奇怪的结果,因为给定场景的两个步骤都通过了,但由于 expect(text).to.equal(myText ); 行。发生这种情况是因为,根据小 cucumber 功能,按钮内的文本应该是我的按钮而不是测试按钮

在这里您可以找到我的控制台中显示的结果: enter image description here

我想知道为什么即使有错误也通过了第二步?我认为测试应该失败,因为比较的字符串不同。我错了吗?我怎样才能避免这种行为?

编辑:

如果我使用 chai-as-promised 中的 eventually 解决 promise ,我会得到相同的结果。所有测试均已通过,但错误文本略有不同:AssertionError: expected 'Test button' to equal 'My Button'

提前谢谢你。

最佳答案

我找到了两个解决此问题的方法。

首先:

selectedElement.getText().then(function(text){
try{
expect(text).to.equal(myText);
} catch(error){
callback(new Error("Compare error"));
}
callback();
});

在这里我解决了 promise ,然后我检查元素的文本是否等于字符串 myText。我不喜欢这个解决方案,因为手动解决 promise ,我没有使用 chai-as-promised 的很多功能。

第二:

expect(selectedElement.getText()).to.eventually.equal(myText).notify(callback); 

我认为这是最好的解决方案并且基于 notify由 chai-as-promised 提供。这 post关于 cucumber-protractor 对这个问题很有帮助。

关于javascript - Protractor :有错误但测试通过,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34519996/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com