question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Loop inside a loop

See original GitHub issue

I have this this code, first part is running ok but when it´s time to pass the i value to second loop, it doesn´t work. It looks like that:

function () {
                 return document.querySelectorAll('.menuElementsAgregator>li:nth-of-type(' + i + ')>.tsr-nav-second-level .has-sub .clickableTabWithLink').length
                                },

doesn´t accept(' + i + ') because if for eg. i manually write 2 in place of i rest of the code will run

 .execute(
            function() {
                return document.querySelectorAll('.menuElementsAgregator>li').length
            },
            function(result) {
                total_links = result.value;
                console.log("Number of main links:" + total_links);
                for (var i = 2; i <= total_links; i++) {
                  (function (i) {
                      browser.waitForElementPresent('.menuElementsAgregator', 3000)
                             .click('.menuElementsAgregator>li:nth-child(' + i + ')>a')
                             .waitForElementVisible('.menuElementsAgregator>li:nth-child(' + i + ')', 2000)
                        .execute(
                                function () {
                                    return document.querySelectorAll('.menuElementsAgregator>li:nth-of-type(' + i + ')>.tsr-nav-second-level .has-sub .clickableTabWithLink').length
                                },
                                    function(result) {
                                    total_links2 = result.value;
                                    console.log("Number of links:" + total_links2);
                                for (var j = 2; j <= total_links2 + 1; j++) {
                                    browser.waitUntilElementIsClickable('.menuElementsAgregator>li:nth-child(' + i + ')')
                                           .click('.menuElementsAgregator>li:nth-child(' + i + ')')
                                           .waitForElementPresent('.menuElementsAgregator>li:nth-of-type(' + i + ')>.tsr-nav-second-level>li:nth-of-type(' + j + ').has-sub', 5000)
                                           .click(' .menuElementsAgregator>li:nth-of-type(' + i + ')>.tsr-nav-second-level>li:nth-of-type(' + j + ').has-sub  .clickableTabWithLink:first-child')
                                           .pause(1000)
                                           .waitForElementVisible('.games-list', 5000);
                                            // .end();
                                    }
                                }
                            )
                  })(i);
              }
          })

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:9 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
mrjamesrileycommented, Jun 17, 2016

Ok, so the answer here is that you cannot refer to variables within the execute first function as you would normally expect to. What you can do, is make use of the optional second argument to the execute function, which allows you to pass in data. So in your case, you can pass in the value of i doing something like the following:

.execute(
  function (i) {
    return document.querySelectorAll('.menuElementsAgregator>li:nth-of-type(' + i + ')>.tsr-nav-second-level .has-sub .clickableTabWithLink').length
  }, [i],
  function(result) {

Note the second argument which is an array of data that will be passed into the execute function, referred to in order by the function arguments. You may want to rename i within the initial function, to avoid future confusion - but that should work for you.

See: http://nightwatchjs.org/api#execute

0reactions
beatfactorcommented, Jun 17, 2016

In the future please post requests for assistance on the Mailing List or StackOverflow.

Read more comments on GitHub >

github_iconTop Results From Across the Web

11.2. Nested Loops
A nested loop is a loop within a loop, an inner loop within the body of an outer one. How this works is...
Read more >
4.4. Nested For Loops — AP CSAwesome
A nested loop has one loop inside of another. These are typically used for working with two dimensions such as printing stars in...
Read more >
Nested Loops in Python - PYnative
A nested loop is a loop inside the body of the outer loop. The inner or outer loop can be any type, such...
Read more >
Nested Loop in Java (With Examples) - Programiz
In this tutorial, we will learn about nested loops in Java with the help of examples. If a loop exists inside the body...
Read more >
6.6: Nested Loops - Processing Tutorial - YouTube
This video looks at nested loops, i.e. a loop inside a loop.Support this channel on Patreon: https://patreon.com/codingtrainContact: ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found