Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

202
Visualizações
Not able to resolve local variable while running test using protractor

I am new to protractor and trying to learn the concepts of asynchronous call in protractor. I am not able to resolve the status of boolean variable which I have set to true in case of any test case fails and execution goes to catch for promise Below is the seudo-code snippet. E.g:

var test = function (){
var deferred = protractor.promise.defer();
  var abort = false;
for (j = 0; j < testCount; j++) {
  switch (element.toString()) {

    case 'input': 
      handler.execute().then(function(result){
      deferred.fulfill(result);           
       ..
      }).catch(function(err){
        abort = true;
         if(abort){ // goes into if loop
          console.log('abort status'+ abort); // prints as true
         }
         deferred.reject(err);   
      })
     .....
  }

       if(abort){ //control do not go into if loop outside switch caseeven though abort is true
          console.log('abort status'+ abort); 
        break;
         }
}
return deferred.promise;
}

This may be silly question but I am not able to get resolve this. Thanks again hopefully that's clear enough to understand my problem.

UPDATES

Updated the code with promise I have used and added break statement in 2nd if statement to exit the for loop if test case fails.

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

As the handler.execute() method is running in a background thread, your second if is running before you are setting the value for abort.

You should move the code in that second if into the catch clause.

Edit

Add a waiting loop between the switch and the if(abort) statements, that waits for the background operation to finish.

over 4 years ago · Santiago Trujillo Relatório

0

Use a recursive function instead of for loop.

var abort = false;

it('should....', function() {
    sampleRecursive(0, testCount, abort)
});

function sampleRecursive(j, testCount, abort) {
    switch (element.toString()) {
        case 'input': 
        handler.execute().then(function(result){
            deferred.fulfill(result);           
            ...
        }).catch(function(err){
            abort = true;
            if(abort){ // goes into if loop
                console.log('abort status'+ abort); // prints as true
            }
            deferred.reject(err);   
        })
        ...
    }
    if (j < testCount && abort == false) {
        sampleRecursive(j + 1, testCount, abort)
    }
}

You can update the above function to fit well on your test.
But recursive functions are easier to use than for loop if you are expecting a break at the middle.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda