Skip to content

Commit

Permalink
Add support for defining multiple start urls via urlPaths option (#6)
Browse files Browse the repository at this point in the history
* #cnsqa-508 support more urls

* #cnsqa-508 update of tests, RandomScenario.runScenario and ScenarioHandler._getRandomScenarioStartUrl

* #cnsqa-508 removed dependency from package.json

* #cnsqa-508 MR changes
  • Loading branch information
lukasPan authored Mar 22, 2021
1 parent 692e632 commit a7f5f0b
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 16 deletions.
20 changes: 19 additions & 1 deletion src/scriptwriter/scenarios/ScenariosHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export default class ScenariosHandler {
failing: [],
};
this._initTime = null;
this._urlPathsIndex = 0;
}

/**
Expand Down Expand Up @@ -52,7 +53,10 @@ export default class ScenariosHandler {
}

if (this._isAllowedToStartRandomScenario()) {
return { type: 'random' };
return {
type: 'random',
scenario: { startUrl: this._getRandomScenarioStartUrl() },
};
}

return {};
Expand Down Expand Up @@ -110,4 +114,18 @@ export default class ScenariosHandler {

return scenario;
}

/**
* Loads a start url for scenario from specified url paths
* @returns {string} start url
*/
_getRandomScenarioStartUrl() {
const { urlPaths } = this._config;

if (this._urlPathsIndex >= urlPaths.length) {
this._urlPathsIndex = 0;
}

return this._config.url + urlPaths[this._urlPathsIndex++];
}
}
9 changes: 8 additions & 1 deletion src/scriptwriter/scenarios/__tests__/ScenariosHandlerSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,17 @@ describe('ScenariosHandler', () => {
defined: ['definedScenario'],
failing: ['failingScenario'],
};
scenariosHandler._config = {
url: 'http://test.url',
urlPaths: ['/start'],
};

expect(scenariosHandler.getScenario()).toEqual({ type: 'failing', scenario: 'failingScenario' });
expect(scenariosHandler.getScenario()).toEqual({ type: 'defined', scenario: 'definedScenario' });
expect(scenariosHandler.getScenario()).toEqual({ type: 'random' });
expect(scenariosHandler.getScenario()).toEqual({
type: 'random',
scenario: { startUrl: 'http://test.url/start' },
});
expect(scenariosHandler.getScenario()).toEqual({});
expect(scenariosHandler._isAllowedToStartRandomScenario).toHaveBeenCalledTimes(2);
});
Expand Down
5 changes: 5 additions & 0 deletions src/shared/config/template.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ module.exports = {
description: 'Starting url for all random scenarios',
type: 'string',
},
urlPaths: {
value: ['/'],
description: 'List of start url paths for random scenarios',
type: 'string[]',
},
numberOfAllowedActionsToReproduceErrorFromPreviousRun: {
value: 20,
description:
Expand Down
3 changes: 1 addition & 2 deletions src/tester/actions/BackAction.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import AbstractAction from './AbstractAction';

const BACK_CHANCE = 80;

const BACK_CHANCE = 30;
/**
* Back action, which will click on random
* or specific (if actionConfig is passed) page element
Expand Down
9 changes: 6 additions & 3 deletions src/tester/scenarios/RandomScenarios.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,28 @@ export default class RandomScenarios {
* performing actions on random page elements
* and trying to produce some page errors.
* @param {Browser} instance
* @param {Object} scenario
* @returns {Promise<Object>} results
*/
async runScenario(instance) {
async runScenario(instance, scenario) {
let results = {};

report('scenario:start', {
type: this.type,
scenario,
});

let response = await instance.page.goto(this._config.url);
let response = await instance.page.goto(scenario.startUrl);

if (response.status() >= 400) {
results.executionError = `Cannot load url ${this._config.url} [status: ${response.status()}]`;
results.executionError = `Cannot load url ${scenario.startUrl} [status: ${response.status()}]`;
} else {
results = await this._performActions(instance);
}

report('scenario:end', {
type: this.type,
scenario,
results,
});

Expand Down
2 changes: 1 addition & 1 deletion src/tester/scenarios/ScenariosHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,6 @@ export default class ScenariosHandler {
return this._failingScenarios.runScenario(instance, scenario);
}

return this._randomScenarios.runScenario(instance);
return this._randomScenarios.runScenario(instance, scenario);
}
}
22 changes: 16 additions & 6 deletions src/tester/scenarios/__tests__/RandomScenariosSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,21 @@ describe('RandomScenarios', () => {
let instance = { page };
scenarios._config = {
url: 'url',
urlPaths: ['/start'],
};
scenarios._performActions = jest.fn().mockReturnValue('results');

let results = await scenarios.runScenario(instance);
let results = await scenarios.runScenario(instance, { startUrl: `url/start` });

expect(messanger.report).toHaveBeenCalledWith('scenario:start', { type: 'random' });
expect(page.goto).toHaveBeenCalledWith('url');
expect(messanger.report).toHaveBeenCalledWith('scenario:start', {
type: 'random',
scenario: { startUrl: `url/start` },
});
expect(page.goto).toHaveBeenCalledWith(`url/start`);
expect(scenarios._performActions).toHaveBeenCalledWith(instance);
expect(messanger.report).toHaveBeenCalledWith('scenario:end', {
type: 'random',
scenario: { startUrl: `url/start` },
results,
});
expect(results).toEqual('results');
Expand All @@ -53,14 +58,19 @@ describe('RandomScenarios', () => {
let instance = { page };
scenarios._config = {
url: 'url',
urlPaths: ['/start'],
};

let results = await scenarios.runScenario(instance);
let results = await scenarios.runScenario(instance, { startUrl: `url/start` });

expect(messanger.report).toHaveBeenCalledWith('scenario:start', { type: 'random' });
expect(page.goto).toHaveBeenCalledWith('url');
expect(messanger.report).toHaveBeenCalledWith('scenario:start', {
type: 'random',
scenario: { startUrl: `url/start` },
});
expect(page.goto).toHaveBeenCalledWith(`url/start`);
expect(messanger.report).toHaveBeenCalledWith('scenario:end', {
type: 'random',
scenario: { startUrl: `url/start` },
results,
});
expect(results).toEqual({
Expand Down
5 changes: 3 additions & 2 deletions src/tester/scenarios/__tests__/ScenariosHandlerSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,14 @@ describe('ScenariosHandler', () => {

it('can run random scenario', () => {
let instance = 'instance';
let scenario = { startUrl: ['/start'] };
scenariosHandler._randomScenarios = {
runScenario: jest.fn(),
};

scenariosHandler.runScenario(instance, 'random');
scenariosHandler.runScenario(instance, 'random', scenario);

expect(scenariosHandler._randomScenarios.runScenario).toHaveBeenCalledWith(instance);
expect(scenariosHandler._randomScenarios.runScenario).toHaveBeenCalledWith('instance', scenario);
});

it('can run defined scenario', () => {
Expand Down

0 comments on commit a7f5f0b

Please sign in to comment.