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

Could not select scenario in Protractor #61

Open pezholio opened 5 years ago

pezholio commented 5 years ago

Hi,

I'm trying to use ng-apimock with Protractor in an Ionic project. After a few false starts, I think I'm almost there, but I'm getting the error Could not select scenario [get-single-case] thrown when trying to use my mocks.

Here's my protractor.conf.js:

// Protractor configuration file, see link for more information
// https://github.com/angular/protractor/blob/master/lib/config.ts

const { SpecReporter } = require('jasmine-spec-reporter');

exports.config = {
  allScriptsTimeout: 11000,
  specs: [
    './src/**/*.e2e-spec.ts'
  ],
  capabilities: {
    'browserName': 'chrome'
  },
  directConnect: true,
  baseUrl: 'http://localhost:4200/',
  framework: 'jasmine',
  jasmineNodeOpts: {
    showColors: true,
    defaultTimeoutInterval: 30000,
    print: function() {}
  },
  onPrepare() {
    require('ts-node').register({
      project: 'e2e/tsconfig.e2e.json'
    });

    var ngApimock = require('ng-apimock')();

    ngApimock.run({
      "src": "e2e/mocks",
    });

    jasmine.getEnv().addReporter(new SpecReporter({ spec: { displayStacktrace: true } }));
    browser.ngApimock = require('../.tmp/mocks/protractor.mock.js');
  },
  ngApimockOpts: {
    angularVersion: 6,
    hybrid: false
  }
};

And here's a mock:

{
  "expression": "/cases/fdaacd04-7125-117a-ac6d-45a68b1d5de1",
  "method": "GET",
  "name": "case",
  "responses": {
    "get-single-case": {
      "default": true,
      "status": 200,
      "data": {
      ...
      }
    }
  }
}

(I've left the actual data out for brevity)

And here's my tests:

import { CasePage } from './case.po';
import { browser, ExpectedConditions, $ } from 'protractor';

describe('Case information', () => {
  let page: CasePage;

  beforeEach(() => {
    browser['ngApimock'].selectScenario('case', 'get-single-case');
    page = new CasePage();
    page.get();
  });

  describe('Listing cases', () => {
    it('should list cases', () => {
      var cases = page.getListofCases();
      expect(cases.count()).toEqual(20);
    });
  });

  describe('Show case detail', () => {
    it('should show case information', () => {
      page.clickFirstCase().then(function() {
        expect(page.getHeaderText()).toMatch(/Visit details/);
      });
    })
  });
});

Any help would be appreciated!

mdasberg commented 5 years ago

@pezholio could you try and chain your selectScenario promise. It is an async call so it could be that the scenario has not been set yet befor you doe a page.get()

pezholio commented 5 years ago

I've tried this in my page object:

browser['ngApimock'].selectScenario('case', 'get-single-case').then(function() {
  this.navigateTo('/cases/fdaacd04-7125-117a-ac6d-45a68b1d5de1');
});

And I still get the same Could not select scenario [get-single-case] error

pezholio commented 5 years ago

Here's some more detailed output:

Jasmine started

  Case information

    Show case detail
      ✓ should show case information

Executed 1 of 1 spec SUCCESS in 0.014 sec.
[11:47:58] E/launcher - Could not select scenario [get-single-case]
[11:47:58] E/launcher - Error: Could not select scenario [get-single-case]
    at process.on (/Users/stuart/Documents/FamilyStoryUI/fsIonic/node_modules/protractor/built/launcher.js:177:52)
    at process.emit (events.js:180:13)
    at process.emit (/Users/stuart/Documents/FamilyStoryUI/fsIonic/node_modules/protractor/node_modules/source-map-support/source-map-support.js:439:21)
    at process.emit (/Users/stuart/Documents/FamilyStoryUI/fsIonic/node_modules/source-map-support/source-map-support.js:461:21)
    at process._fatalException (internal/bootstrap/node.js:391:27)
[11:47:58] E/launcher - Process exited with error code 199

Weirdly, the test seems to pass, but I don't see any output in Chrome

marcioferlan commented 5 years ago

@pezholio can you please share the setup for running ng-apimock on your Ionic 3 project? Like the dependencies you installed, the setup for tslint, protractor and jasmine.. as well as a simple test case in which you managed to select the scenario properly? I'm trying it for a couple hours now with no success yet. Thanks in advance!

pezholio commented 5 years ago

Thanks, but I ended up going down a different route for this, using a live test API, and creating/deleting everything I needed as part of the setup/teardown routine.

julissamackey commented 5 years ago

Hi @marcioferlan @mdasberg I am also receiving this error using Protractor

Could not select scenario [success] thrown

Here is my setup: My Protractor Config

const { SpecReporter } = require('jasmine-spec-reporter');

exports.config = {
    allScriptsTimeout: 30000,
    specs: [
        './source/client/test/ui-tests/spec-files/*/*.ui-spec.ts'
    ],
    capabilities: {
        browserName: 'chrome',
        chromeOptions: {
            // args: ["--headless", "--disable-gpsu"] 
        }
    },
    directConnect: true,
    jasmineNodeOpts: {
        showColors: true,
        defaultTimeoutInterval: 60000,
        print: function () { }
    },
    onPrepare() {

        require('ts-node').register({
            project: './source/client/test/ui-tests/tsconfig.e2e.json'
        });

        jasmine.getEnv().addReporter(new SpecReporter({ spec: { displayStacktrace: true } }));

        var ngApimock = require('ng-apimock')();
        ngApimock.run({
            "src": "./source/client/test/ui-tests/mock-api-responses",
          });
        browser.ngApimock = require('./.tmp/mocks/protractor.mock.js');

    },
    ngApimockOpts: {
        angularVersion: 7,
        hybrid: false
    },
    specReporter: {
        maxLogLines: 5,  // limit number of lines logged per test
        suppressErrorSummary: false,  // do not print error summary
        suppressFailed: false,  // do not print information about failed tests
        suppressPassed: false,  // do not print information about passed tests
        suppressSkipped: true,  // do not print information about skipped tests
        showSpecTiming: false // print the time elapsed for each spec
    }
};

My mock

{
    "expression": "/billing/customers/search",
    "method": "POST",
    "body": {"LastName":"kop"},
    "name": "partialNameMatch",
    "responses": {
      "success": {
        "default": true, 
        "status": 200, 
        "headers": {},
        "data": {"Customers": [--omitted for brevity --]}, 
        "delay": 2000
      }
    }
  }

Where I am selecting the scenario [ Page Object ]

navigateToSubscriberSearch(){
        return browser['ngApimock'].selectScenario('partialNameMatch', 'success').then(()=>{
            browser.get('/subscribers/search');
        });
    }

Any thoughts?