Polly.js + selenium-webdriver: How to stub/intercept XHR requests

I'm running selenium tests for a react app, and I'm trying to stub my axios (XHR) requests. Ithink I'm close but seems like there's something missing. I'm running polly l - listening on localhost:3000 - and then my test looks like this:

import { Polly } from '@pollyjs/core'
import XHRAdapter from '@pollyjs/adapter-xhr'
import LocalStoragePersister from '@pollyjs/persister-local-storage';
import webdriver from 'selenium-webdriver'
const { By, Key, until } = webdriver

Polly.register(XHRAdapter)
Polly.register(LocalStoragePersister);

describe('Loggin in', () => {

  const polly = new Polly('Sign In', {
    adapters: ['xhr'],
    persister: 'local-storage'
  });
  const { server } = polly;

  polly.configure({
    persisterOptions: {
      'local-storage': {
        key: '__pollyjs__'
      }
    }
  });

  server.get("http://localhost:3000/dashboard").passthrough()

  server.get('http://localhost:3000/api/users/me').intercept((req, res) => {
    res.status(200);
    res.json({});
  });
  server.put('http://localhost:3000/api/users/login').intercept((req, res) => {
    res.status(200);
    res.json({});
  });


  it('renders correctly', async() => {
    var chromeCapabilities = webdriver.Capabilities.chrome();
    var chromeOptions = {
      //'args': ['--headless']
      'args': ['--auto-open-devtools-for-tabs']
    };
    chromeCapabilities.set('chromeOptions', chromeOptions);
    const driver = new webdriver.Builder().withCapabilities(chromeCapabilities).build();

    jasmine.Ajax.stubRequest('/dashboard').andReturn({
      "status": 200
    });

    await driver.get('http://localhost:3000/dashboard')

    await driver.getCurrentUrl().then((url) => {
      expect(url).toEqual("http://localhost:3000/dashboard")
    })

    await polly.stop();
    driver.quit()
  })

})

So the idea here is that "/dashboard" should be passed through and not be intercepted at all, while the api requests (/api/users/meand /api/users/login) should be stubbed by polly. What happens is that when landing on /dashboard I get a 404 back and polly seem to not pass it through at all. Have I got it completely wrong here?

Cannot GET /dashboard

Answers:

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.