【问题标题】:Why Testcafe ignoring the first condition of If Else If statement为什么Testcafe忽略If Else If语句的第一个条件
【发布时间】:2019-03-05 09:43:17
【问题描述】:

我的应用程序中有以下安全问题

  1. 您的第一个电话号码是什么?
  2. 你最喜欢什么颜色?
  3. 你最喜欢的运动队是谁?

下面是部分代码

export default class webPage {
constructor () {
this.securityQuestion = Selector('#challengeQuestionLabelId');
this.sportQuestion    = this.securityQuestion.withText('sports');
this.colorQuestion    = this.securityQuestion.withText('color');
this.phoneQuestion    = this.securityQuestion.withText('phone');

}
}

条件语句

async answerSecurityQuestion() { 
                 var myAnswer; 

                 if ( await this.webPage.colorQuestion.exists ) { 
                     myAnswer = "color1"; 
                 } else if ( await this.webPage.phoneQuestion.exists ) { 
                     myAnswer = "phone1"; 
                 } else {
                     myAnswer = "sports1"; 
                 }

问题在于第一个 if 语句。即使问题包含“颜色”,代码也不会首先识别问题。

我已经切换了语句的顺序,发现问题不在于选择器,而在于第一个 if 语句。

有人知道如何解决这个问题吗?

【问题讨论】:

  • 我创建了一个具有类似场景的测试,但我尝试复制此行为没有成功。请您澄清一下关键字“this”所指的内容吗? “问题在于第一个 if 语句”是什么意思?执行第一条语句时是否有任何错误信息?

标签: javascript testing automated-tests assertion testcafe


【解决方案1】:

添加下面的等待语句解决了这个问题。

await t.wait(3000);

【讨论】:

    【解决方案2】:

    我认为你需要几个括号:

    async answerSecurityQuestion() { 
    
                 var myAnswer; 
    
                 if ( (await this.webPage.colorQuestion).exists ) { 
                     myAnswer = "color1"; 
                 } else if ( (await this.webPage.phoneQuestion).exists ) { 
                     myAnswer = "phone1"; 
                 } else {
                     myAnswer = "sports1"; 
                 }
    
    }
    

    也就是说,你在等待括号中的内容,对吧?

    【讨论】:

    • 感谢您的回复。按照建议应用括号后它停止工作。所以,我认为我们等待整个函数/条件。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多