Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vk add math steps #68

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions example/features/sample.feature
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,7 @@ Feature: Cucumber test
Scenario: I want to test all the form method
Then user prints the message 'other testssttst' to console
# Given user navigates to '${vars.base_url}'

@full_test
Scenario: I want to test math execute
Given user execute math(1+1)
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"cucumber-html-reporter": "^5.0.0",
"fs-plus": "^3.1.1",
"jsonpath": "^1.0.1",
"mathjs": "^6.2.2",
"protractor": "^5.4.2",
"protractor-cucumber": "^0.1.8",
"protractor-cucumber-framework": "^1.0.2",
Expand Down
8 changes: 8 additions & 0 deletions src/step_defs/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ const {
} = helpers;
const path = require('path');

const { compile } = require('../support/helpers/math');

/**
* Sleeps the execution for a specific time in seconds
*/
Expand Down Expand Up @@ -89,3 +91,9 @@ Then(/^user saves a screenshot '(.*)'$/, (screenshotPath) => {
vars.addVariable('img_num', imgNum += 1);
}
});

Given(/^user execute math\((.*)\)/, (exp) => {
const result = compile(exp);
logger.info(result);
return result;
});
7 changes: 7 additions & 0 deletions src/support/helpers/math.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const math = require('mathjs');

const compile = (expression = '') => math.evaluate(expression);

module.exports = {
compile,
};
26 changes: 26 additions & 0 deletions test/support/helper/math.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const { compile } = require('../../../src/support/helpers/math');

describe('Helper Math - exp', () => {
it('should return correctly result from given expression', () => {
const expression = '1+2*4';
const result = compile(expression);
expect(result).toEqual(9);
});

it('should return correctly result from given expression with variables', () => {
const item = 2;
const expression = `1+2*4/${item}`;
const result = compile(expression);
expect(result).toEqual(5);
});

it('should throw error in undefined variables or symbols', () => {
const item = 'a';
const expression = `1+2*4/${item}`;
try {
compile(expression);
} catch (error) {
expect(error.message).toEqual('Undefined symbol a');
}
});
});