@@ -70,23 +70,70 @@ const defaultConfig = {
7070 * I.seeElement('=user'); // matches => [data-qa=user]
7171 * I.click('=sign-up'); // matches => [data-qa=sign-up]
7272 * ```
73+ *
74+ * Using `data-qa` OR `data-test` attribute with `=` prefix:
75+ *
76+ * ```js
77+ * // in codecept.conf.js
78+ * plugins: {
79+ * customLocator: {
80+ * enabled: true,
81+ * prefix: '=',
82+ * attribute: ['data-qa', 'data-test'],
83+ * strategy: 'xpath'
84+ * }
85+ * }
86+ * ```
87+ *
88+ * In a test:
89+ *
90+ * ```js
91+ * I.seeElement('=user'); // matches => //*[@data-qa=user or @data-test=user]
92+ * I.click('=sign-up'); // matches => //*[data-qa=sign-up or @data-test=sign-up]
93+ * ```
94+ *
95+ * ```js
96+ * // in codecept.conf.js
97+ * plugins: {
98+ * customLocator: {
99+ * enabled: true,
100+ * prefix: '=',
101+ * attribute: ['data-qa', 'data-test'],
102+ * strategy: 'css'
103+ * }
104+ * }
105+ * ```
106+ *
107+ * In a test:
108+ *
109+ * ```js
110+ * I.seeElement('=user'); // matches => [data-qa=user],[data-test=user]
111+ * I.click('=sign-up'); // matches => [data-qa=sign-up],[data-test=sign-up]
112+ * ```
73113 */
74114module . exports = ( config ) => {
75- config = Object . assign ( defaultConfig , config ) ;
115+ config = { ... defaultConfig , ... config } ;
76116
77117 Locator . addFilter ( ( value , locatorObj ) => {
78118 if ( typeof value !== 'string' ) return ;
79119 if ( ! value . startsWith ( config . prefix ) ) return ;
80120
121+ if ( ! [ 'String' , 'Array' ] . includes ( config . attribute . constructor . name ) ) return ;
122+
81123 const val = value . substr ( config . prefix . length ) ;
82124
83125 if ( config . strategy . toLowerCase ( ) === 'xpath' ) {
84- locatorObj . value = `.//*[@${ config . attribute } =${ xpathLocator . literal ( val ) } ]` ;
126+ locatorObj . value = `.//*[${
127+ [ ] . concat ( config . attribute )
128+ . map ( ( attr ) => `@${ attr } =${ xpathLocator . literal ( val ) } ` )
129+ . join ( ' or ' ) } ]`;
85130 locatorObj . type = 'xpath' ;
86131 }
87132
88133 if ( config . strategy . toLowerCase ( ) === 'css' ) {
89- locatorObj . value = `[${ config . attribute } =${ val } ]` ;
134+ locatorObj . value = [ ] . concat ( config . attribute )
135+ . map ( ( attr ) => `[${ attr } =${ val } ]` )
136+ . join ( ',' ) ;
90137 locatorObj . type = 'css' ;
91138 }
92139
0 commit comments