【问题标题】:Protractor if else with expect(condition)量角器 if else with expect(condition)
【发布时间】:2019-06-15 13:53:16
【问题描述】:

在我的脚本中,我需要包含 if-else 条件以验证是否预期 = 实际。如果它们不匹配,则应转到 else 块并打印“StepFailed”。但是,始终执行 if 块和尽管预期≠实际,但 o/p 已“通过步骤”。以下是我的代码:

var expected = ['Select training program using Index', 'Selenium','A','UFT/QTP','Loadrunner'];
var els = element.all(by.xpath("//select[@id='dropdown1']/option"));
   for (var i = 0; i < expected.length; ++i) {
     if(expect(els.get(i).getText()).to.eventually.equals(expected[i])){
        console.log('' +'Steppassed'+ '');
     }else{
        console.log('' +'Stepfailed'+ '');
     }
});

下拉值有:'Select training program using Index', 'Selenium', 'Appium'、'UFT/QTP'、'Loadrunner'。如果您观察到我给出的是 'A' 而不是“Appium”,因此该步骤应该会失败,但没有发生。

请建议我如何解决这个问题。谢谢。

【问题讨论】:

    标签: typescript npm protractor cucumber


    【解决方案1】:

    首先,如果您正在检查条件值,则无需断言

    你可以做这样的事情 -

    var expected = ['Select training program using Index', 'Selenium', 
    'A','UFT/QTP','Loadrunner'];
    
    var els = element.all(by.xpath("//select[@id='dropdown1']/option"));
    
    for (var i = 0; i < expected.length; ++i) {
       els.get(i).getText().then((text) => {
           if(text === expected[i] ) {
               console.log('' +'Steppassed'+ '');
             } else {
                console.log('' +'Stepfailed'+ '');
               }
             });
           }
    

    您还可以使用 async/await 语法来更好地处理和提高 Promise 的可读性!

    【讨论】:

    • 很高兴知道:)
    • 兄弟,你能用我的内置报告器和钩子文件取悦我吗?我也需要更新它。我是黄瓜打字稿量角器的新手。
    • 你可以查看这个 repo 以获得指导 - github.com/igniteram/protractor-cucumber-typescript 如果你遇到任何问题,你总是在这里提出问题
    • 哈哈兄弟,过去 2 个月我一直在使用您的 repo 代码。我观察到的是,每次运行脚本时报告都会发生变化。如果我正在运行,我实际上想要 2 个报告2次以上,每个测试步骤都没有截图。有什么更新吗?
    • 好的,你能在那个 repo 中创建 github 问题,以便我可以将它们添加为当前框架的增强功能吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-02
    • 2020-07-20
    • 2019-08-08
    • 2023-03-29
    相关资源
    最近更新 更多