segment-boneyard / nightmare

A high-level browser automation library.
https://open.segment.com
19.54k stars 1.08k forks source link

reload with an interval #1576

Open zebiiii opened 4 years ago

zebiiii commented 4 years ago

I want to reload my page every seconds if a condition is true. With my code, nightmare refreshes only once and quit the interval to continue the code.

    .evaluate((keywords, color) => {

            setInterval(function() {
            if (document.querySelector('#container')) {

              var ElementCount = document.querySelector('#container').childElementCount

              for(var i=0; i < ElementCount; i++) {      
                var element_product = document.querySelectorAll('#container > article > div > h1 > a')[i].innerText.toLowerCase()
                var element_color = document.querySelectorAll('#container > article > div > p > a')[i].innerText.toLowerCase()
                var regex_product =  new RegExp(keywords)
                var regex_color = new RegExp(color)

                if (element_product.match(regex_product) && element_color.match(regex_color)) {
                  document.location.href = document.querySelectorAll('#container > article > div > h1 > a')[i].href
                  clearInterval()
                }
                if (!element_product.match(regex_product) && !element_color.match(regex_color) && i===(ElementCount-1)) {
                  window.location.reload()
                }
              }  
            }
            }, 1000)
    }, keywords, color)

thx, matt