Skip to content

Commit

Permalink
Merge pull request #19 from revjet-qa/feature/semicolons
Browse files Browse the repository at this point in the history
Enabled semicolons and strict linting rules
  • Loading branch information
alexkrechik authored Dec 5, 2017
2 parents 35aea05 + 50a0793 commit bd1f7e1
Show file tree
Hide file tree
Showing 9 changed files with 121 additions and 123 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@
"quotes": [2, "single", "avoid-escape"],
"require-jsdoc": 1,
"semi-spacing": [2, {"before": false, "after": true}],
"semi": [0, "always"],
"semi": [2, "always"],
"sort-vars": 0,
"keyword-spacing": 2,
"space-before-blocks": 2,
Expand Down
14 changes: 7 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
'use strict'
'use strict';

const fs = require('fs')
const files = fs.readdirSync('./src/steps')
const fs = require('fs');
const files = fs.readdirSync('./src/steps');

files.forEach((f) => {
const path = `./src/steps/${f}`
const fileSteps = require(path)
const path = `./src/steps/${f}`;
const fileSteps = require(path);

if (typeof fileSteps === 'function') {
fileSteps.apply(this)
fileSteps.apply(this);
}
})
});
88 changes: 44 additions & 44 deletions src/helpers/objects.processor.js
Original file line number Diff line number Diff line change
@@ -1,106 +1,106 @@
/* eslint no-param-reassign: 0*/
/* eslint no-undef: 0 */

const { _r } = require('./utils')
const { _r } = require('./utils');

const dynamicId = '$dynamicId$'
const regDynamicId = '\\$dynamicId\\$'
const dynamicId = '$dynamicId$';
const regDynamicId = '\\$dynamicId\\$';

// Can come from applysteps ??
const pageObject = '([a-zA-Z0-9_-]+ from [a-zA-Z0-9_-]+ page)'
const pageObjectsParts = '^([a-zA-Z0-9_-]+) from ([a-zA-Z0-9_-]+) page$'
const pageObject = '([a-zA-Z0-9_-]+ from [a-zA-Z0-9_-]+ page)';
const pageObjectsParts = '^([a-zA-Z0-9_-]+) from ([a-zA-Z0-9_-]+) page$';

const dictionaryObject = '([a-zA-Z0-9_-]+ from [a-zA-Z0-9_-]+ dictionary|"[^"]*")'
const dictionaryObjectsParts = '^(?:([a-zA-Z0-9_-]+) from ([a-zA-Z0-9_-]+) dictionary|"([^"]*)")$'
const dictionaryObject = '([a-zA-Z0-9_-]+ from [a-zA-Z0-9_-]+ dictionary|"[^"]*")';
const dictionaryObjectsParts = '^(?:([a-zA-Z0-9_-]+) from ([a-zA-Z0-9_-]+) dictionary|"([^"]*)")$';

// Todo do we need this in csp-qa
function injectInto(locator, injection) {
const lastInjectionSymbol = injection.slice(-1)
const lastLocatorSumbol = locator.slice(-1)
const lastInjectionSymbol = injection.slice(-1);
const lastLocatorSumbol = locator.slice(-1);

if (lastInjectionSymbol !== ']') {
// Add ']' to the end of injection only if missing (for backward compatibility)
injection += ']'
injection += ']';
}
if (lastLocatorSumbol === ')') {
// If our locator ends with round brackets
return injectInto(locator.replace(/\)$/, ''), injection) + ')'
return injectInto(locator.replace(/\)$/, ''), injection) + ')';
}
if (lastLocatorSumbol === ']') {
if (locator.match(/\[[0-9]+\]$/)) {
// Locator ends with brackets, which contain some xpath num
const nums = locator.match(/\[[0-9]+\]$/)[0]
const body = locator.replace(/\[[0-9]+\]$/, '')
const nums = locator.match(/\[[0-9]+\]$/)[0];
const body = locator.replace(/\[[0-9]+\]$/, '');

return injectInto(body, injection) + nums
return injectInto(body, injection) + nums;
}
return locator.substring(0, locator.length - 1) + ' and ' + injection
return locator.substring(0, locator.length - 1) + ' and ' + injection;
}
return locator + '[' + injection
return locator + '[' + injection;
}

function pageObjectGetter(str) {
const match = _r(pageObjectsParts).exec(str)
const match = _r(pageObjectsParts).exec(str);

if (!match) {
throw new Error(`Was unable to find Page Object for "${str}"`)
throw new Error(`Was unable to find Page Object for "${str}"`);
}
if (match[1]) {
const page = match[2]
const object = match[1]
const page = match[2];
const object = match[1];

if (!pages[page]) {
throw new Error(`"${page}" page is missing`)
throw new Error(`"${page}" page is missing`);
}
if (!pages[page][object]) {
throw new Error(`"${object}" page object is missing for the "${page}" page`)
throw new Error(`"${object}" page object is missing for the "${page}" page`);
}
return pages[page][object]
return pages[page][object];
}
throw new Error(`Unknown Page Object type for "${str}"`)
throw new Error(`Unknown Page Object type for "${str}"`);
}

function getPageObject(str) {
const pageObjectGetterFunc = objectsProcessor.pageObjectGetter || pageObjectGetter
const value = pageObjectGetterFunc(str)
const idValue = value.replace(_r(regDynamicId, 'g'), id.getId())
const pageObjectGetterFunc = objectsProcessor.pageObjectGetter || pageObjectGetter;
const value = pageObjectGetterFunc(str);
const idValue = value.replace(_r(regDynamicId, 'g'), id.getId());
const injection = 'not(ancestor-or-self::*[contains(@style,"visibility: hidden;") ' +
'or contains(@style,"display: none") or contains(@class,"x-hide-offsets")])'
const injectedvalue = injectInto(idValue, injection)
'or contains(@style,"display: none") or contains(@class,"x-hide-offsets")])';
const injectedValue = injectInto(idValue, injection);

return injectedvalue
return injectedValue;
}

function dicionaryGetter(str) {
const match = _r(dictionaryObjectsParts).exec(str)
const match = _r(dictionaryObjectsParts).exec(str);

if (!match) {
throw new Error(`Was unable to find Dictionary Object type for "${str}"`)
throw new Error(`Was unable to find Dictionary Object type for "${str}"`);
}
if (match[1]) {
const dictionary = match[2]
const object = match[1]
const dictionary = match[2];
const object = match[1];

if (!pages[dictionary]) {
throw new Error(`"${dictionary}" page is missing`)
throw new Error(`"${dictionary}" page is missing`);
}
if (!pages[dictionary][object]) {
throw new Error(`"${object}" page object is missing for the "${dictionary}" page`)
throw new Error(`"${object}" page object is missing for the "${dictionary}" page`);
}
return pages[dictionary][object]
return pages[dictionary][object];
}
if (match[3] !== undefined) {
return match[3]
return match[3];
}
throw new Error(`Unknown Dictionary Object type for "${str}"`)
throw new Error(`Unknown Dictionary Object type for "${str}"`);
}

function getDictionaryObject(str) {
const dicionaryGetterFunc = objectsProcessor.dicionaryGetter || dicionaryGetter
const value = dicionaryGetterFunc(str)
const idValue = value.replace(_r(regDynamicId, 'g'), id.getId())
const dicionaryGetterFunc = objectsProcessor.dicionaryGetter || dicionaryGetter;
const value = dicionaryGetterFunc(str);
const idValue = value.replace(_r(regDynamicId, 'g'), id.getId());

return idValue
return idValue;
}

module.exports = {
Expand All @@ -110,4 +110,4 @@ module.exports = {
injectInto,
getPageObject,
getDictionaryObject
}
};
10 changes: 5 additions & 5 deletions src/helpers/utils.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
const escapeRegExp = function (str) {
return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, '\$&')
}
return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, '\$&');
};

const _r = function (str, flags) {
return new RegExp(escapeRegExp(str), flags)
}
return new RegExp(escapeRegExp(str), flags);
};

module.exports = {
escapeRegExp,
_r
}
};
17 changes: 8 additions & 9 deletions src/steps/given.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
/* eslint no-undef: 0 */
/* eslint new-cap: 0 */

const { defineSupportCode } = require('cucumber')
const { dictionaryObject, getDictionaryObject } = require('../helpers/objects.processor')
const { _r } = require('../helpers/utils')
const { defineSupportCode } = require('cucumber');
const { dictionaryObject, getDictionaryObject } = require('../helpers/objects.processor');
const { _r } = require('../helpers/utils');

module.exports = function () {
defineSupportCode(({ Given }) => {
Expand All @@ -13,10 +12,10 @@ module.exports = function () {
* The URL to navigate to
* @type {String} or {DictionaryObject}
*/
const url = getDictionaryObject.call(this, urlDictionary)
const url = getDictionaryObject.call(this, urlDictionary);

browser.url(url)
})
browser.url(url);
});

})
}
});
};
17 changes: 8 additions & 9 deletions src/steps/then.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
/* eslint no-undef: 0 */
/* eslint new-cap: 0 */

const { defineSupportCode } = require('cucumber')
const { pageObject, getPageObject } = require('../helpers/objects.processor')
const { _r } = require('../helpers/utils')
const { defineSupportCode } = require('cucumber');
const { pageObject, getPageObject } = require('../helpers/objects.processor');
const { _r } = require('../helpers/utils');

module.exports = function () {
defineSupportCode(({ Then }) => {
Expand All @@ -13,10 +12,10 @@ module.exports = function () {
* The element should be present
* @type {PageObject}
*/
const locator = getPageObject(object)
const locator = getPageObject(object);

browser.$(locator).waitForExist()
})
browser.$(locator).waitForExist();
});

})
}
});
};
6 changes: 3 additions & 3 deletions test/features/page_objects/main.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
let e = {}
let e = {};

e.txtHeader = '//*[@id="header"]'
e.txtHeader = '//*[@id="header"]';

module.exports = e
module.exports = e;
68 changes: 34 additions & 34 deletions test/mocha/objects.processor.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { expect } = require('chai')
const { expect } = require('chai');

const {
injectInto,
Expand All @@ -7,10 +7,10 @@ const {
getPageObject,
getDictionaryObject,
dynamicId
} = require('../../src/helpers/objects.processor')
const { _r } = require('../../src/helpers/utils')
} = require('../../src/helpers/objects.processor');
const { _r } = require('../../src/helpers/utils');

const realId = 12345
const realId = 12345;

global.pages = {
main: {
Expand All @@ -19,28 +19,28 @@ global.pages = {
book: {
word: `dictionaryObject${dynamicId}`
}
}
};

global.id = {
getId: () => realId
}
};

global.objectsProcessor = { }
global.objectsProcessor = {};

describe('injectInto', () => {
const data = [
{ locator: '//a', injection: '@class="b"]', result: '//a[@class="b"]' },
{ locator: '//a', injection: '@class="b"', result: '//a[@class="b"]' },
{ locator: '//a[@id=1]', injection: '@class="b"', result: '//a[@id=1 and @class="b"]' },
{ locator: '(//a)[1]', injection: '@class="b"]', result: '(//a[@class="b"])[1]' }
]
];

data.forEach((d) => {
it(`should get "${d.result}" string from "${d.locator}" and "${d.injection}" injection`, () => {
expect(injectInto(d.locator, d.injection)).to.be.equals(d.result)
})
})
})
expect(injectInto(d.locator, d.injection)).to.be.equals(d.result);
});
});
});

describe('getPageObject', () => {
const data = [
Expand All @@ -50,23 +50,23 @@ describe('getPageObject', () => {
result: `//div[@id='${realId}' and not(ancestor-or-self::*[contains(@style,"visibility: hidden;") ` +
'or contains(@style,"display: none") or contains(@class,"x-hide-offsets")])]'
}
]
];

data.forEach((d) => {
const { step, regExp, result } = d
const { step, regExp, result } = d;

it(`should correctly get pageObject from the <<${step}>> step`, (next) => {
const match = step.match(regExp)
const match = step.match(regExp);

expect(match).to.be.an('array')
const strPageObject = match[1]
const pageObjectVal = getPageObject(strPageObject)
expect(match).to.be.an('array');
const strPageObject = match[1];
const pageObjectVal = getPageObject(strPageObject);

expect(pageObjectVal).to.be.equals(result)
next()
})
})
})
expect(pageObjectVal).to.be.equals(result);
next();
});
});
});

describe('getDictionaryObject', () => {
const data = [
Expand All @@ -80,20 +80,20 @@ describe('getDictionaryObject', () => {
regExp: _r(`When I write ${dictionaryObject}`),
result: `dictionaryObject${realId}`
}
]
];

data.forEach((d) => {
const { step, regExp, result } = d
const { step, regExp, result } = d;

it(`should correctly get dictionaryObject from the <<${step}>> step`, (next) => {
const match = step.match(regExp)
const match = step.match(regExp);

expect(match).to.be.an('array')
const strDictionaryObject = match[1]
const dictionaryObjectVal = getDictionaryObject(strDictionaryObject)
expect(match).to.be.an('array');
const strDictionaryObject = match[1];
const dictionaryObjectVal = getDictionaryObject(strDictionaryObject);

expect(dictionaryObjectVal).to.be.equals(result)
next()
})
})
})
expect(dictionaryObjectVal).to.be.equals(result);
next();
});
});
});
Loading

0 comments on commit bd1f7e1

Please sign in to comment.