mercredi 28 novembre 2018

Unable to intercept request [Puppeteer JS]

In app both when testing manually and in puppeteer the request is being done and the data is being used,so i know the problem isnt there.I tryed multiple ways to accees and intercept the requests ot atlest try to replace the data that is coming from the api as start but didnt manage to do anything.I copied the code below from answer from github issues page that seems the fix the other person's problem.

import puppeteer from 'puppeteer';
describe('index', () => {
  let page;
  beforeAll(async () => {
    jest.setTimeout(30000);
    const browser = await puppeteer.launch({
      headless: false,
      slowMo: 250,
      args: ['--windows-size=1920,1080']
    });
    page = await browser.newPage();
    await page.goto('http://localhost:1234/');
  });
  afterAll(() => {
    browser.close();
  });
  it('should have title "Fonoapi app"', async () => {
    const title = await page.title();
    expect(title).toMatch('Fonoapi app');
  });
  describe('searching for device', () => {
    const device = {
      brand: 'lenovo',
      model: 'p70'
    };
    const constants = {
      url:
        'https://fonoapi.freshpixl.com/v1/getdevice?brand=lenovo&device=p70&token=d1191e0a0b1cb4cdc6b573126ca98b321b703f1ebe49fe7b',
      array: [
        {
          Brand: 'Lenovo',
          DeviceName: 'Lenovo P700i',
          alert_types: 'Vibration; MP3, WAV ringtones'
        }
      ]
    };

    beforeAll(async () => {
      await page.waitForSelector('.search-form');
      await page.type('input[name=brand]', device.brand);
      await page.type('input[name=model]', device.model);
      await page.$eval('.search-btn', btn => btn.click());
      await page.setRequestInterception(true);
      await page.on('request', interceptedRequest => {
        if (interceptedRequest.url() === constants.url) {
          interceptedRequest.respond({
            status: 200,
            contentType: 'application/javascript; charset=utf-8',
            body: JSON.parse(constants.array)
          });
        } else interceptedRequest.continue();
      });

      await page.waitForSelector('.device-card');
    });
    
  });
});

Aucun commentaire:

Enregistrer un commentaire