Polly.js + selenium-webdriver:如何存根/拦截XHR请求

时间:2018-09-07 12:02:15

标签: javascript selenium-webdriver xmlhttprequest stub stubbing

我正在为React应用运行硒测试,并且试图对axios(XHR)请求进行存根处理。我认为我已经接近了,但似乎缺少了一些东西。我正在运行polly l-正在监听localhost:3000-然后我的测试如下:

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()
  })

})

因此,这里的想法是应该通过“ / dashboard”而不要对其进行任何截取,而api请求(/api/users/me/api/users/login)应该被polly存根。发生的情况是,降落在/ dashboard上时,我收到了404错误,而波利似乎根本没有通过它。我在这里完全弄错了吗?

Cannot GET /dashboard

0 个答案:

没有答案