2017-09-12 17 views
6

मैं मोचा का उपयोग कर एंड एंड परिदृश्य को स्वचालित करने पर काम कर रहा हूं। मेरे पास एक यूआरएल एंडपॉइंट है जिसे परिणामस्वरूप प्रतिक्रिया में एक निश्चित मूल्य प्राप्त होने तक मतदान किया जाना है। इसे करने का कोई तरीका है ?जेएसओएन प्रतिक्रिया में कुछ मान निर्धारित होने तक एक यूआरएल मतदान करना: मोचा, एकीकरण परीक्षण

+0

इसे जांचें https://github.com/ilikebits/gallop –

+0

मैं एक GET अनुरोध के लिए प्राधिकरण कैसे प्रदान करूं और मुझे TypeError भी मिल सकता है: अपरिभाषित त्रुटि –

+0

की संपत्ति 'सब्सक्राइब' नहीं पढ़ सकता है क्या आप नोड के साथ सॉकेट प्रोग्रामिंग जानते हैं? – Salman

उत्तर

-1

यह मैं कैसे WebdriverIO और मोचा के साथ यह करने के लिए कर रहा था है

describe("wait for value in content of page",() => { 
    it("should be able to wait to value in url",() => { 

     var max_seconds_to_wait = 10; 
     var seconds_counter = 0; 
     var should_continue = true; 

     while (should_continue) { 

     browser.url('http://your.url.com'); 
     var response = JSON.parse(browser.getText("body")); 
     console.log(response) 

     if (response == 'something') { 
      should_continue = false;    
     } 
     browser.pause(1000); 
     seconds_counter++; 

     if (seconds_counter > max_seconds_to_wait) { 
      throw 'Waiting for json from url timeout error'; 
     } 
    } 
    }); 
}); 
+0

आपने कई चीजों को याद किया: 1) जांच प्रतिक्रिया की उम्मीद है मूल्य 2) max_seconds_to_wait समय पर त्रुटि फेंक 3) केवल इस परीक्षण के लिए डिफ़ॉल्ट टाइमआउट बदलें। आइडिया का उपयोग WebdriverIO बहुत अच्छा है! – galkin

+0

टिप्पणियों के लिए धन्यवाद, मैंने जवाब संपादित किया। मुझे लगता है कि ऐसा करने का एक बेहतर तरीका परीक्षण के अंदर नहीं, एक उपयोगिता फ़ंक्शन की तरह एक निर्यात करने योग्य फ़ंक्शन बनाना है, और फिर इस फ़ंक्शन को आवश्यक समय_to_wait पास करना आसान होगा। – satlosb

5

request और कॉलबैक दृष्टिकोण के साथ उदाहरण:

const request = require('request'); 

describe('example',() => { 
    it('polling', function (done) { 
     this.timeout(5000); 

     let attemptsLeft = 10; 
     const expectedValue = '42'; 
     const delayBetweenRequest = 100; 

     function check() { 
      request('http://www.google.com', (error, response, body) => { 
       if (body === expectedValue) return done(); 
       attemptsLeft -= 1; 
       if (!attemptsLeft) return done(new Error('All attempts used')); 
       setTimeout(check, delayBetweenRequest); 
      }); 
     } 

     check(); 
    }); 
}); 

got और async के साथ उदाहरण/इंतजार है दृष्टिकोण:

const utils = require('util'); 
const got = require('got'); 

const wait = utils.promisify(setTimeout); 

describe('example',() => { 
    it('polling', async function (done) { 
     this.timeout(5000); 
     const expectedValue = '42'; 
     const delayBetweenRequest = 100; 

     for (let attemptsLeft = 10; attemptsLeft; attemptsLeft -= 1) { 
      const resp = await got.get('http://www.google.com'); 
      if (resp.body === expectedValue) return done(); 
      await wait(delayBetweenRequest); 
     } 

     done(new Error('All attempts used')); 
    }); 
}); 
संबंधित मुद्दे