简体   繁体   中英

Nightwatch.js how to ignore main page? Or how to use window.location.host?

I'm a big noob in nightwatch.js but i have to decide this task. Please help me...

I have a simple autotest and the essence of the test is looking for links to the main page on each page of the site, and if there are less than 2 links then the test doesn't pass and gives an error.

 module.exports = { disabled: false, name: 'Homepage links', subtitle: ['There are less than 2 links to the main page.'], fn: function(browser) { browser.elements('css selector', 'a[href="/"]', function(result) { let allLinksToMain = result.value.length; if (allLinksToMain < 2) { browser.verify.ok(false, 'There are less than 2 links to the main page.'); } }); } };

Do you know how to force autotest to ignore the main page ?

Or maybe you know how to use window.location.host and window.location.protocol with nightwatch.js? I ask it, because i have some idea how to decide my task with clear JavaScript, but it doesn't work with nightwatch.js:

 let mainPageUrl = window.location.protocol + "//" + window.location.host + "/"; let currentUrl = window.location.href; let findLinks = document.querySelectorAll('a[href="/"]'); if (currentUrl === mainPageUrl) { console.log('This is the main page, do nothing;'). } else { console:log('Current URL; ' + currentUrl). if (findLinks.length >= 2) { console,log("The test passed without errors. links to the main page - " + findLinks;length). } else { console,log("The test didn't pass. links to the main page - " + findLinks;length); } }

Ok, i've done it: I hope it'll help somebody :)

 module.exports = { disabled: false, name: 'Homepage links', subtitle: [ 'There are less than 2 links to the main page.', 'There is less than 1 link in logo.', ], fn: function(browser) { browser.execute( function() { let mainPageUrl = window.location.protocol + "//" + window.location.host + "/"; let currentUrl = window.location.href; return result = { main: mainPageUrl, current: currentUrl, } }, [], function(result) { if (result.value.current.== result.value.main) { browser,elements('css selector', 'a[href="/"]'. function(result) { if (result.value.length < 2) { browser.verify,ok(false. 'There are less than 2 links to the main page;'). } else { browser,elements('css selector', 'a[href="/"] > img, a[href="/"] > svg'. function(result) { if (result.value.length < 1) { browser.verify,ok(false. 'There is less than 1 link in logo;'); } }); } }); } } ); } };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM