From 4370aea93454abbb954951200c0804e919d86c30 Mon Sep 17 00:00:00 2001 From: kobenguyent Date: Sat, 16 Dec 2023 09:28:09 +0100 Subject: [PATCH] fix: seeattributesonelements throws error --- lib/helper/Playwright.js | 3 ++- lib/helper/Puppeteer.js | 3 ++- test/helper/webapi.js | 13 +++++++++++++ 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/lib/helper/Playwright.js b/lib/helper/Playwright.js index 714b20c14..772b2a3a2 100644 --- a/lib/helper/Playwright.js +++ b/lib/helper/Playwright.js @@ -2167,7 +2167,8 @@ class Playwright extends Helper { let chunked = chunkArray(attrs, values.length); chunked = chunked.filter((val) => { for (let i = 0; i < val.length; ++i) { - if (!val[i].includes(values[i])) return false; + // if the attribute doesn't exist, returns false as well + if (!val[i] || !val[i].includes(values[i])) return false; } return true; }); diff --git a/lib/helper/Puppeteer.js b/lib/helper/Puppeteer.js index 41f2fd44a..3c89bbc43 100644 --- a/lib/helper/Puppeteer.js +++ b/lib/helper/Puppeteer.js @@ -1822,7 +1822,8 @@ class Puppeteer extends Helper { for (let i = 0; i < val.length; ++i) { const _actual = Number.isNaN(val[i]) || (typeof values[i]) === 'string' ? val[i] : Number.parseInt(values[i], 10); const _expected = Number.isNaN(values[i]) || (typeof values[i]) === 'string' ? values[i] : Number.parseInt(values[i], 10); - if (!_actual.includes(_expected)) return false; + // if the attribute doesn't exist, returns false as well + if (!_actual || !_actual.includes(_expected)) return false; } return true; }); diff --git a/test/helper/webapi.js b/test/helper/webapi.js index 39f7cdfdd..168639bed 100644 --- a/test/helper/webapi.js +++ b/test/helper/webapi.js @@ -1374,6 +1374,19 @@ module.exports.tests = function () { e.message.should.include('all elements (a) to have attributes {"qa-id":"test","href":"/info"}'); } }); + + it('should return error when using non existing attribute', async function () { + if (isHelper('TestCafe') || isHelper('WebDriver')) this.skip(); + + try { + await I.amOnPage('https://github.com/codeceptjs/CodeceptJS/'); + await I.seeAttributesOnElements({ css: 'a[href="/team"]' }, { + disable: true, + }); + } catch (e) { + e.message.should.include('expected all elements ({css: a[href="/team"]}) to have attributes {"disable":true} "0" to equal "1"'); + } + }); }); describe('#seeCssPropertiesOnElements', () => {