2016-11-29 8 views
1

私は、支払いをランダムに(番号)選択する機能を作成しました。最初の選択(番号)が見つからない場合(キャッチしようとする)、別のものを選択したいと思います。私はどのようなプロパティを読み取ることができません数8を送信する際オリジナルが見つからなかった場合、どうすれば別の要素をクリックできますか?

を 'クリック' し、それが信用カートビザを選択する必要があります...

しかし、私はいつも受け取る:

が失敗しました。間違っている?要素が実際にあるとidも正しいです:

CheckoutPage3.prototype.selectPayment = function (number) { 
    if (number == 1) { 
     try { 
      this.elementPaymentPaypal.click().then(function() { 
       return 1; 
      }, function (err) { 
       console.log("payment not found, select new"); 
       this.elementPaymentCreditCard.click(); 
       this.elementCreditVISA.click(); 
       number = 2; 
      }); 
     } 
     catch (err) { 
      console.log('error occured'); 
     } 
    } 
    else if (number == 2) { 
     this.elementPaymentCreditCard.click(); 
     this.elementCreditVISA.click(); 
     number = 2; 
    } else if (number == 3) { 
     this.elementPaymentCreditCard.click(); 
     this.elementCreditMasterCard.click(); 
     number = 3; 
    } 
    else if (number == 4) { 
     try { 
      this.elementPaymentCreditCard.click(); 
      this.elementCreditAmericanExpress.click().then(function() { 
       number = 4; 
      }, function (err) { 
       console.log("payment not found, select new"); 
       this.elementCreditVISA.click(); 
       number = 2; 
      }); 
     } 
     catch (err) { 
      console.log('error occured'); 
     } 
    } 
    else { 
     try { 
      this.elementPrePayment.click().then(function() { 
       number = 5; 
      }, function (err) { 
       console.log("payment not found, select new"); 
       this.elementPaymentCreditCard.click(); 
       this.elementCreditVISA.click(); 
       number = 2; 
      }); 
     } 
     catch (err) { 
      console.log('error occured'); 
     } 
    } 
}; 

答えて

2

これはあなたのページオブジェクトthisで、common problem in JavaScriptであることは常にあなたはそれがあることを期待するものではありません。例えば、ここに約束解決機能に:

this.elementPaymentPaypal.click().then(function() { 
    return 1; 
}, function (err) { 
    console.log("payment not found, select new"); 
    this.elementPaymentCreditCard.click(); // < HERE 
    this.elementCreditVISA.click(); // < and HERE 
    number = 2; 
}); 

this

はもう、「現在の」ページオブジェクトのインスタンスを参照しません。その後、

CheckoutPage3.prototype.selectPayment = function (number) { 
    var self = this; 

    if (number == 1) { 
     try { 
      // ... 

そしてthisの代わりにselfを使用します:

問題にアプローチするための一般的な方法は、親スコープにthisへの参照を保存することです

this.elementPaymentPaypal.click().then(function() { 
    return 1; 
}, function (err) { 
    console.log("payment not found, select new"); 
    self.elementPaymentCreditCard.click(); 
    self.elementCreditVISA.click(); 
    number = 2; 
}); 
関連する問題