0

I have a nightwatch test where I am testing a list of urls for mobile app content RSS, CSS, and HTML 'feeds'. They are in arrays like:

var rssFeeds = [
'https://www.example.com/rss/feed1',
'https://www.example.com/rss/feed2',
]

In my test, I am using a for loop to iterate through each url in the array to test like:

var urlToTest = ''; //< -- this is always 'https://www.example.com/rss/feed2' in .element check below
    // rss Feeds
    for (var i = 0; i < rssFeeds.length; i++) {
        urlToTest = rssFeeds[i].toString()
        browser
            .url('https://validator.w3.org/feed/')
            .waitForElementVisible('body', 'W3C RSS Validator page loaded... https://validator.w3.org/feed/ \n')
            .setValue('#url', urlToTest)
            .click('a[class="submit"]')
            .element('css selector', 'img[title="Valid RSS"]', function (result) {
                if (result.value && result.value.ELEMENT) {
                    // Element is present, do the appropriate tests
                    console.log('valid RSS...')
                    browser.perform(function () {
                        browser.verify.ok(1 == 1, urlToTest + ' valid RSS...') //<-- HERE
                    })
                } else {
                    // Element is not present.
                    console.log('invalid RSS...')
                    browser.verify.ok(1 != 1, urlToTest + ' invalid RSS...') //<-- AND HERE
                }
            })
    }

In each .url() call, it is properly entering feed1, feed2, etc sequentially, however, in the .element() call it is always the last value in the array, so the reporting looks wrong. It always looks like:

✓ appContent

TC004-AppContent

✓ W3C RSS Validator page loaded... https://validator.w3.org/feed/

✓ Passed [ok]: https://www.example.com/rss/feed2 valid RSS...

✓ W3C RSS Validator page loaded... https://validator.w3.org/feed/

✓ Passed [ok]: https://www.example.com/rss/feed2 valid RSS..

So, since urlToTest is always the last array value, the report isn't very helpful right now by not differentiating what url is good/bad. I tried wrapping different parts of the code in perform() call to try to get it to run exactly when it needs too to no avail.

I also saw posts on .execute() but I am just not getting it...

Any ideas are greatly appreciated.

Kamil Naja
  • 6,267
  • 6
  • 33
  • 47
Beau D'Amore
  • 3,174
  • 5
  • 24
  • 56

1 Answers1

1

I had to move the .element() call into a function outside the loop as per a javascript issue described here

So, the code is now:

var urlToTest = '';
    // rss Feeds
    for (var i = 0; i < rssFeeds.length; i++) {
        urlToTest = rssFeeds[i].toString()
        browser
            .url('https://validator.w3.org/feed/')
            .waitForElementVisible('body', 'W3C RSS Validator page loaded... https://validator.w3.org/feed/')
            .setValue('#url', urlToTest)
            .click('a[class="submit"]')
            .waitForElementVisible('body', 'W3C results page loaded...')
        checkElement(browser, 'img[title="Valid RSS"]', urlToTest)
    }

and

// must call from outside loop in order to maintain proper syncronous execution
function checkElement(browser, elem, url) {
    browser.element('css selector', elem, function (result) {
        if (result.value && result.value.ELEMENT) {
            // Element is present, do the appropriate tests
            browser.verify.ok(1 == 1, url + ' valid feed...')
        } else {
            // Element is not present.
            browser.verify.ok(1 != 1, url + ' invalid feed...')
        }
    })
}
Beau D'Amore
  • 3,174
  • 5
  • 24
  • 56