diff --git a/packages/tees-drivers/__test__/src/base/index.test.js b/packages/tees-drivers/__test__/src/base/index.test.js new file mode 100644 index 0000000..3cdaf38 --- /dev/null +++ b/packages/tees-drivers/__test__/src/base/index.test.js @@ -0,0 +1,56 @@ +const { + Query +} = require('../../../src/base/index'); + +describe('base/index unit test for getSelector', () => { + + let query; + beforeAll(() => { + query = new Query('', { label: 'data-auto-id' }); + }) + + test('The selector is @searchBoxForm @searchBox then return [data-auto-id="searchBoxForm"] [data-auto-id="searchBox"]', () => { + expect(query.getSelector(`@searchBoxForm @searchBox`)).toEqual(`[data-auto-id="searchBoxForm"] [data-auto-id="searchBox"]`); + }); + + test('The selector is @searchBoxForm input[data-auto-id-test="searchBox"] then return [data-auto-id="searchBoxForm"] input[data-auto-id-test="searchBox"]', () => { + expect(query.getSelector(`@searchBoxForm input[data-auto-id-test="searchBox"]`)).toEqual(`[data-auto-id="searchBoxForm"] input[data-auto-id-test="searchBox"]`); + }); + + test('If selector is a native css, it will not change"]', () => { + expect(query.getSelector(`[class="searchBoxForm"] input[class="searchBox"]`)).toEqual(`[class="searchBoxForm"] input[class="searchBox"]`); + }); + + test('The selector is @b_searchboxForm:1 then return [data-auto-id="b_searchboxForm"]:nth-child(1)', () => { + expect(query.getSelector(`@b_searchboxForm:1`)).toEqual(`[data-auto-id="b_searchboxForm"]:nth-child(1)`); + }); + + test('The selector is @b_searchboxForm:-1 then return [data-auto-id="b_searchboxForm"]:nth-last-child(1)', () => { + expect(query.getSelector(`@b_searchboxForm:-1`)).toEqual(`[data-auto-id="b_searchboxForm"]:nth-last-child(1)`); + }); + + //TODO: unsupported selector + test('The selector is div@b_searchboxForm then return [div[data-auto-id="b_searchboxForm"]', () => { + expect(query.getSelector(`div@b_searchboxForm`)).not.toEqual(`div[data-auto-id="b_searchboxForm"]`); + }); + + test('The selector is div@b_searchboxForm then return [div[data-auto-id="b_searchboxForm"]', () => { + expect(query.getSelector(`@b_searchboxForm div@b_searchbox`)).not.toEqual(`[data-auto-id="b_searchboxForm"] div[data-auto-id="b_searchbox"]`); + }); + +}); + + +describe('base/index unit test for getSelector', () => { + let query; + beforeAll(() => { + query = new Query('', {}); + }) + + test('If selector is a native css, it will not change', () => { + [`[class="searchBox"]`, `[class*="searchBox"] [class="searchBoxForm"]`].forEach((selector) => { + expect(query.getSelector(selector)).toEqual(selector); + }) + }); + +}); diff --git a/packages/tees-drivers/src/base/index.js b/packages/tees-drivers/src/base/index.js index c1e0c36..c3b0a40 100644 --- a/packages/tees-drivers/src/base/index.js +++ b/packages/tees-drivers/src/base/index.js @@ -16,20 +16,15 @@ class Query { getSelector(selector) { const labelSign = /^@\s*/; - const isUsingCssSelector = !this._label || labelSign.test(selector); - if (isUsingCssSelector) { - return selector - .replace(labelSign, '') - .split(' ') - .map((_selector) => { - const [labelSelector, index] = _selector.split(':'); - const childSelector = index ? - `:nth-${/-/.test(index) ? 'last-' : ''}child(${index.replace('-', '')})` : ''; - return `[${this._label}="${labelSelector}"]${childSelector}`; - }) - .join(' '); - } - return selector; + return selector.split(' ').map(_selector => { + const [labelSelector, index] = _selector.replace(/^@\s*/, '').split(':'); + if (labelSign.test(_selector)) { + return `[${this._label}="${labelSelector}"]${index ? + `:nth-${/-/.test(index) ? 'last-' : ''}child(${index.replace('-', '')})` : ''}` + } else { + return _selector; + } + }).join(' ') } async waitFor(param, options, ...args) {