2017-12-07 3 views
1

私はURLを要求するリクエストパッケージを使用し、繰延約束使用しています:分度器テストでアプリケーションのURLのステータスコードを確認するにはどうすればよいですか?

getMonitoredPageName = function() { 
    var deferredExecutor = protractor.promise.defer(); 
    var defer = protractor.promise.defer(); 

    request('http://google.com', 
    function (error, response, body) { 
     if (error || response.statusCode ==400) { 
      defer.reject(response.statusCode); 
     } else { 
      defer.fulfill(response.statusCode); 
     } 
    }); 

    return defer.promise;   
} 

を、私は、テストからの機能の上に呼び出しています:

it('should log the page name of every page view in the wizard', function() { 

    // We opened the first page of the wizard and we expect it to have been logged 
    expect(heartBeatNotification.getMonitoredPageName()).toBeTruthy(true); 
    //expect(heartBeatNotification.getMonitoredPageName()).toBe(400); 
    //browser.controlFlow().execute(heartBeatNotification.getMonitoredPageName); 

}) 

問題は、テストケースは関係なく常に成功していないが何であるかURL与えている。アプリケーションが実行されているかどうかを確認するテストケースを記述する必要があります。

答えて

0

ゼロ以外のステータスコードは "真実"なので、テストケースを調整する必要があります。応答が明示的に200であるかどうかを確認してください。それでもまだ成功しているので、何か2xxを確認してください。

expect(heartBeatNotification.getMonitoredPageName()).toEqual(200); 

それとも、またマイナーなノートを

などの200以上300未満
expect(heartBeatNotification.getMonitoredPageName()).toBeGreaterThanOrEqual(200); 
expect(heartBeatNotification.getMonitoredPageName()).toBeLessThan(300); 

いることを確認し、あなたの最初のifのブロックの上にロジックを確認したい場合があります。

if (error || response.statusCode ==400) { 
     defer.reject(response.statusCode); 
    } 

エラーが発生した場合は、response.statusCode(正ではない)があるではないかもしれません。エラー自体を拒否する方が良いかもしれません。

0

私はこのようにしてテストを行うことができました。それはあなたがやろうとしていたものより少し単純ですが、それはあなたがそれを理解するのに役立つはずです。注意すべき点は、ブロック設定に問題があることです。 responseundefinedの場合、テストはタイムアウトし、約束は返されません。とにかく、ここで私はあなたのテストを働かせることができました。ここで

it('should return status code 200', async() => { 
    const url = 'http://google.com'; 
    await heartBeatNotification.getMonitoredPageName(url).then(statusCode => { 
     expect(statusCode).toEqual(200); 
    },() => { 
     fail('app not started');    
    }); 
}); 

は関数である。

getMonitoredPageName = (url) => { 
    const defer = protractor.promise.defer(); 

    request(url, (err, response, body) => {    
     if(err) { 
      defer.reject(); 
     } else { 
      defer.fulfill(response.statusCode); 
     } 
    }); 

    return defer.promise; 
} 
+0

それは公共のサイトのために働いています。私のサイトはイントラネット内にあり、応答は定義されていません –

+0

また、URLをhttp://googleasdasdasd.comとして送信すると、その機能は長時間応答を待っています。 –

+0

ifブロックにいくつか変更を加えました。それを試して、それが動作しているかどうか確認してください。今より良いはずです。 – tehbeardedone

関連する問題