mdasberg / ng-apimock

Node plugin that provides the ability to use scenario based api mocking: for local development for protractor testing
MIT License
99 stars 26 forks source link

Cannot seem to change scenarios in an e2e test #88

Closed farisT closed 3 years ago

farisT commented 4 years ago

Hi,

I am having trouble changing scenarios in a test. At the moment I have these e2e tests:

import { NoPermissionsPage } from './no-permission.po';
import { MyProductsPage } from './my-products.po';
import { browser, logging } from 'protractor';

declare const ngApimock: any;

describe('Products', () => {
  let myProductsPage: MyProductsPage;
  let noPermissionsPage: NoPermissionsPage;

  beforeEach(async () => {
    myProductsPage = new MyProductsPage();
    noPermissionsPage = new NoPermissionsPage();
  });

  describe('Products page', () => {

    describe('Error scenario', () => {
      it('should show notification with clickable link' , async () => {
        await ngApimock.selectScenario('getProductFamilies', 'internalServerError');
        const url = '/producten-plein';
        await myProductsPage.navigateTo();
        expect(await myProductsPage.getErrorNotification().isPresent()).toBe(true);
        expect(await myProductsPage.getErrorNotificationRefreshLink().getAttribute('href')).toMatch(`${url}$`);
        expect(await browser.getCurrentUrl()).toMatch(`${url}$`);
      });
    });

    describe('Success scenario', () => {

      afterEach(async () => {
        const logs = await browser.manage().logs().get(logging.Type.BROWSER);
        expect(logs).not.toContain(jasmine.objectContaining({
          level: logging.Level.SEVERE,
        } as logging.Entry));
      });

      it('should load', async () => {
        await myProductsPage.navigateTo();
        expect(await myProductsPage.getTitleText()).toEqual('Mijn Producten');
        expect((await myProductsPage.getActiveTiles()).length).toEqual(4);
        expect((await myProductsPage.getInActiveTiles()).length).toEqual(1);
      });

      it('should open new tab when clicked on upper tile button', async () => {
        await myProductsPage.navigateTo();
        expect((await browser.getAllWindowHandles()).length).toBe(1);
        const goToProductButton = await myProductsPage.getActiveTileButton(1);
        goToProductButton.click();
        const activeBrowsers = await browser.getAllWindowHandles();
        expect(activeBrowsers.length).toBe(2);
      });
    });
  });

In the Error Scenario describe I change the scenario of the api-mock to internalServerError however the scenario does not change and therefore the tests in that describe fail. What is interesting is that when I fdescribe the error scenario (aka only run those tests) it does successfully change the scenario and the tests pass. Not sure what is going wrong.

Node version: 12.13.1 Angular: 7.2.0 ng-apimock: 1.4.9 protractor: 5.4.9 (have also tried with 7.0.0)

mdasberg commented 4 years ago

apimock uses a cookie to make sure that parallel tests don't intervene with each other. Therefor a page needs to be opened before you select a scenario.

Here you navigate after the selection of the scenario.

This issue has been resolved in the new version of ngapimock.

farisT commented 3 years ago

@mdasberg Thank you for this. We have updated to the new ngapimock and that did the trick 👍