2017-10-02 1 views
0

je le code suivant:chaîne Continuing Promise sur browser.wait() Délai d'attente

.then(function() { 
    return buttonClick(); 
}) 
// BEGIN 
.then(function() { 
    return browser.wait(EC.elementToBeClickable(anotherButton), timeOut) 
     .then(function() { 
      anotherButton.click(); 
     }) 
}) 
.then(function() { 
    return browser.wait(EC.elementToBeClickable(yetAnotherButton), timeOut) 
     .then(function() { 
      yetAnotherButton.click(); 
     }) 
}) 
// END 
.then(function() { 
    browser.wait(do some things); 
}) 

que je dois faire le code entre BEGIN et END execute seulement quand return browser.wait(EC.elementToBeClickable(anotherButton), timeOut) réussit. Si ce qui précède ne réussit pas, je voudrais juste reprendre la chaîne au browser.wait(do some things);

J'ai essayé plusieurs façons d'y parvenir et j'ai trouvé de la chance en faisant quelque chose de similaire à this mais je n'ai pas encore obtenu les résultats que je cherche.

Des suggestions sur comment je peux accomplir cela?

Merci

Répondre

0

EDIT: Selon Protractor FAQ vous pouvez exécuter une seconde fonction dans un cas d'erreur.

.then(function() { 
    return buttonClick(); 
}) 
//wrap your BEGIN-END-part into a new function like this: 
// then(function(){BEGIN-END}, function(err){failcase-execution}) 
.then(function(){ 
    // BEGIN 
    .then(function() { 
     return browser.wait(EC.elementToBeClickable(anotherButton), timeOut) 
      .then(function() { 
       anotherButton.click(); 
      }) 
    }) 
    .then(function() { 
     return browser.wait(EC.elementToBeClickable(yetAnotherButton), timeout) 
      .then(function() { 
       yetAnotherButton.click(); 
      }) 
    }) 
}, function(err){ 
    //Here is your execution in error-case. 
    browser.wait(do some things); 
}) 
.then(function() { 
    browser.wait(do some things); 
}) 

Mais pour moi, ça sonne, comme si vous essayez de tester deux portées différentes. Y at-il une raison, ces deux tests doivent être exécutés dans le même cas de test (it-block)?

Quelque chose comme:

describe ("test", function(){ 
    it("executes the first part incl. Begin-End", function(){ 
     //eventually some steps to come here first. 
     .then(function() { 
      return buttonClick(); 
     }) 
     // BEGIN 
     .then(function() { 
      return browser.wait(EC.elementToBeClickable(anotherButton), timeOut) 
       .then(function() { 
        anotherButton.click(); 
       }) 
     }) 
     .then(function() { 
      return browser.wait(EC.elementToBeClickable(yetAnotherButton), timeOut) 
       .then(function() { 
        yetAnotherButton.click(); 
       }) 
     }) 
    }); 
    it("executes the second part, which resumes anyway", function(){ 
     //if necessary, repeat some of the steps from before test 
     .then(function() { 
      return buttonClick(); 
     }) 
     .then(function() { 
      browser.wait(do some things); 
     }) 
    }); 
}); 

Ou si vous essayez de comprendre juste de sortir, si vous pouvez cliquer sur le bouton, vous pouvez exécuter les clics entre BEGIN/END dans une fonction séparée, qui retourne juste vrai ou faux (semblable à ce que vous avez lié):

//existing part 
.then(function() { 
    return buttonClick(); 
}) 
.then(
    bool clicksWorked = this.optionalExecution(); 
) 
.then(function() { 
    browser.wait(do some things); 
    if(!clicksWorked){console.log("Some Buttons couldn't be clicked")} 
}) 

//as separate function, which just returns either true or false 
this.optionalExecution = function() { 
    try { 
     // BEGIN 
     browser.wait(EC.elementToBeClickable(anotherButton), timeOut) 
      .then(function() { 
       anotherButton.click(); 
      }) 
     browser.wait(EC.elementToBeClickable(yetAnotherButton), timeOut) 
      .then(function() { 
       yetAnotherButton.click(); 
      }) 
     return true 
    } 
    catch (Exception e) { 
     return false 
    } 
} 

comme une remarque de côté: Pouvez-vous ajouter un peu plus de contexte à votre .then() -list que ce n'est pas clair pour moi, si tous vos .then() se réfèrent à un entrée en condition ou comment ils sont intégrés dans le scénario de test. Dans votre exemple actuel, il ne semble pas nécessaire de placer vos actions dans .then() -blocks. Protractor exécuterait ligne par ligne de manière synchrone, également sans les .then(). Voir aussi here

+0

Il vous suffit de relire la FAQ de Protractor et de trouver une meilleure façon de procéder. Ajouté par Edit à ma réponse: https://github.com/angular/protractor/blob/master/docs/faq.md#how-can-i-catch-errors-such-as-elementnotfound –