2016-05-31 2 views
1

설명 : "시작됨", "진행 중", "성공", "오류"중 하나로 변경되는 웹 페이지에서 특정 레이블을 읽길 원합니다. 레이블 값이 "success"또는 "error"로 변경되면 더 이상 변경되지 않습니다.자바 스크립트의 재귀 호출에서

문제 : 각도기에서 javascript를 사용하여 레이블 값을 읽을 때 레이블의 텍스트 값이 호출 함수에 반환되지 않습니다. 대신 'undefined'를 반환합니다. 아래는 제 코드입니다. 제발 좀 봐주세요.

CheckColor_Test.js

var commonFunctions = require('../pages/CommonFunctions.js'); 
describe("Run Test", function() { 
    it("should stop once the status reached Success or Error", function() { 
     var processStatus = commonFunctions.refreshTillProcessFinish(); 
     expect(processStatus).toContain('Success','Error'); 
    }); 
}); 

각도기에서 실행 CommonFunctions.js

Var CommonFunctions = function(){ 
var label = element(by.id('Status')); 
var refreshStatusBtn = element(by.css('[ng-click="getJob()"]')); 
    this.getStatusValue = function() { 
     return label.then(function (headers) { 
      return headers.getText(); 
     }); 
    }; 
    this.refreshTillRefreshFinish = function() { 
     var refreshStatusMonitor = function (currentStatus) { 
      return currentStatus.then(function (Status) { 
       if (Status == 'Success' || Status.includes("Error")) { 
        console.log(Status); 
        return Status; 
       } else { 
        refreshStatusBtn.click(); 
        console.log(Status); 
        browser.sleep(2000); 
        refreshStatusMonitor (currentStatus); 
       } 
      }); 
     }; 
     return refreshStatusMonitor (this.getStatusValue); 
    }; 
} 
module.exports = new CommonFunctions(); 

: Webstorm에서 내가 설정 한 각도기, 따라서 나는 그것을 사용하여 실행하는 데 사용됩니다.

예상 결과 : 시험은 성공적으로 얻고

실제 결과를 전달해야합니다 시험은 아래의 오류와 함께 실패합니다.

"C:\Program Files (x86)\JetBrains\WebStorm 2016.1.1\bin\runnerw.exe" "C:\Program Files\nodejs\node.exe" node_modules\protractor\built\cli.js D:\Somesh_HDD\WebstormProjects\ProjectUBET\conf.js 
[22:19:59] I/direct - Using ChromeDriver directly... 
[22:19:59] I/launcher - Running 1 instances of WebDriver 
Spec started 
Started 
InProgress 
Success 

    Run Test 
    ? should stop once the status reached Success or Error 
     - Expected undefined to contain 'Success', 'Error'. 

************************************************** 
*     Failures     * 
************************************************** 

1) Run Test should stop once the status reached Success or Error 
    - Expected undefined to contain 'Success', 'Error'. 

Executed 1 of 1 spec (1 FAILED) in 33 secs. 
[22:20:36] I/launcher - 0 instance(s) of WebDriver still running 
[22:20:36] I/launcher - chrome #01 failed 1 test(s) 
[22:20:36] I/launcher - overall: 1 failed spec(s) 
[22:20:36] E/launcher - Process exited with error code 1 

Process finished with exit code 1 
+0

바이올린을 추가하십시오 – YakovL

+0

'currentStatus.then' func의 else 절에 return 문이 필요합니까? – James

+0

@ YakovL 노드 예제가 jsFiddle에서 실행되지 않는다고 생각합니다. – michaPau

답변

1

다음 반환 값 : 후자는 refreshStatusMonitor의 재귀 호출 중 하나에 반환

사실
return Status; 

:

return currentStatus.then(...); 

이 문에 의해 반환되는 값이 아닙니다 어디서나 캡처되지 않습니다. 이 약속을 포함하는 비동기 코드이기 때문에

, currentStatus의 반환 값은,뿐만 아니라 약속을해야하는 것 버블 다음도 할 약속 대기하도록 할 필요가 테스트에 refreshStatusMonitor, refreshTillRefreshFinish를 통해 무엇이든 기대하기 전에 성취되었다.

JavaScript 환경을 완전히 차단하므로 browser.sleep(...)을 사용하지 않는 것이 좋습니다. 대신 setTimeout(...)을 사용할 수 있습니다. 여기

그 아이디어를 기반으로 일부 검증되지 않은 코드 :

this.refreshTillRefreshFinish = function() { 
    // create a promise 
    var deferred = protractor.promise.defer(); 
    var refreshStatusMonitor = function (currentStatus) { 
     currentStatus.then(function refresh(Status) { 
      if (Status == 'Success' || Status.includes("Error")) { 
       // Signal the completion via the promise. 
       // This triggers the `then` callback in your revised test 
       deferred.fulfill(Status); 
      } else { 
       refreshStatusBtn.click(); 
       console.log(Status); 
       // Use setTimeout so JavaScript is not blocked here: 
       setTimeout(function() { 
        refreshStatusMonitor(currentStatus); 
       }, 2000); 
      } 
     }); 
    }; 
    refreshStatusMonitor(this.getStatusValue); 
    // Don't wait for the result to happen while blocking everything, 
    // instead return a custom-made promise immediately 
    return deferred.promise; 
}; 

다음 또한 약속 다루고있는 고려해야한다 귀하의 시험 :

it("should stop once the status reached Success or Error", function() { 
    var processStatus = commonFunctions.refreshTillProcessFinish().then(function() { 
     expect(processStatus).toContain('Success','Error'); 
     done(); 
    }); 
}, 20000); // set timeout to 20 seconds 

주 재스민이있다 기본 제한 시간은 2 초이므로 마지막에 추가 인수를 제공해야합니다.

주의 : 이러한 비동기 테스트는 단위 테스트 배치를 실행하는 데 적합하지 않습니다.

+0

버디, 나는 당신이 제안한 길을 따라 갔지만 행운은 없었습니다 ... ( –

+0

좀 더 자세한 정보를 제공해 줄 수 있습니까? 디버깅 코드를 추가하고 문제가있는 곳을 말 할 수 있습니까? – trincot

+0

저에게 다시 와서 오류를 줄 수 있습니까? 코드의 어느 부분에 문제가 있습니까? – trincot

0

스크립트에서 새로 고침 버튼을 재귀 적으로 클릭 할 수 있습니까?

나는 재귀 적 방법으로 약속을 도입함으로써 기존 스크립트를 거의 변경하지 않았습니다. 시도해보십시오.

var CommonFunctions = function(){ 
    var label = element(by.id('Status')); 
    var refreshStatusBtn = element(by.css('[ng-click="getJob()"]')); 
    this.refreshTillRefreshFinish = function() { 
     var defer = protractor.promise().defer(); 
     var refreshStatusMonitor = function() { 
      label.getText().then(function (Status) { 
       if (Status == 'Success' || Status.includes("Error")) { 
        defer.fulfill(Status); 
       } else { 
        refreshStatusBtn.click(); 
        browser.sleep(2000); 
        refreshStatusMonitor(); 
       } 
      }); 

      return defer.promise; 
     }; 
     return refreshStatusMonitor(); 
    }; 
} 
module.exports = new CommonFunctions(); 
+0

죄송합니다. 문제는 해결책이 해결되지 않습니다. –

관련 문제