segment-boneyard / nightmare

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

how to make several goto without losing the previous ones #1572

Closed ahernand53 closed 4 years ago

ahernand53 commented 4 years ago

I've been looking for the following with nightmarejs if possible, but I haven't found anything.

This is an example of what I want, some help

  1. I want to go on site_1 (temporary email service) and get an email.
  2. Then go to site_2 and fill that email, then submit a form.
  3. Then go to to site_1 and check if email arrived
  4. Analyze the site email and look for the verification code (otp) and send it to the site_2 the verification page
  5. go to site_2 Receive the otp code and place it at the entrance to finish the registration.

So far what I have been able to achieve has been up to point 3. But not knowing how to pass after the otp code

nightmareEmail
     .viewport(400, 600)
     .goto(PAGE_EMAIL_GENERATE)
     .wait('#mail')
     .evaluate(() => document.querySelector('#mail').value)
     .then(email => {
         registerUser(email)
     })
     .catch((error) => {
         // eslint-disable-next-line no-console
         console.error('Search failed:', error);
      });

registerUser(email) {
        let name = Faker.name.findName()
        let password = Faker.internet.password()
        // eslint-disable-next-line no-console
        console.log('Registrando')
        nightmareRegister
            .clearCache()
            .viewport(400,600)
            .goto(PAGE_FOR_REGISTER)
            .type('input[name=customerName', name)
            .type('input[name=email]', email)
            .type('input[name=password]', password)
            .type('input[name=passwordCheck]', password)
            .click('input#continue[type=submit]')
            // eslint-disable-next-line no-console
            .then(() => console.log('Done!'))
    }
rafaelperozin commented 4 years ago

I'm doing exactly this but I create a loop with map and run the promise again for each website.

const scanCompetitor = url => {
     return new Promise((resolve, reject) => {
         nightmare
             .goto(url)
             .evaluate(() => document.querySelector('[itemprop="price"]').getAttribute('content'))
             .end()
             .then(foundPrice => resolve(foundPrice))
             .catch(error => console.log('Search failed:', error));
     });
}

const urls = [url1, url2, url2];
urls.map(async url => {
     return scanCompetitor(url).then(async price => console.log(price));
});
ahernand53 commented 4 years ago

@rafaelperozin Thank you, the only drawback is that I would update the page and it is not what I want, so I don't know how I would do it to stay on that page to make the reactive update