Ad

Protractor - How To Open X Amount Of Sites And SendKeys

Right now I am trying to create a script where I have x amount of sites through a json with a dict of URL, AMOUNT etc:

{
    "URL": [{
            "https://testing.com/en/p/-12332423/": "999"
        }, {
            "https://testing.com/en/p/-123456/": "123"
        },
        {
            "https://testing.com/en/p/-456436346/": "422"
        }
    ]
}

where we have Link: Amount.

What I am trying to do now is that I would like to open each site and then add those amount that is given so etc first open https://testing.com/en/p/-12332423/ with amount of 999

and once this is done then we go for the next site which is https://testing.com/en/p/-123456/ amount of 123 and so on.

For the moment I have only managed to do one page to work by having:

{
    "URL": "https://testing.com/en/p/-12332423/"
}

with the code of:

const userData = require('../globalContent.json');


describe('Add To Cart', function () {



    it('Open Product URL', (done) => {
        browser.driver
            .then(() => browser.waitForAngularEnabled(false))
            .then(() => browser.manage().window().maximize())
            .then(() => browser.get(userData.URL))
            .then(() => done());
    });


    //Hardcoded to write 999
    it('Enter 999 Items', function (done) {

        browser.driver
            .then(() => utils.presenceOf(element(by.id('amount'))))
            .then(() => element(by.id('amount')).clear())
            .then(() => utils.sendKeys(element(by.id('amount')), "999"))
            .then(() => done());

    });

and once that is done then I want to redirect to a specific site etc

it('Finished all sites', (done) => {
    browser.driver
        .then(() => browser.waitForAngularEnabled(false))
        .then(() => browser.manage().window().maximize())
        .then(() => browser.get("https://finished.com"))
        .then(() => done());
});

however my problem is that I am not quite sure if its effective to do a list with dict where I have link:amount or if there is any smoother way. (Will accept new suggestions) but however I would like to loop through each site and then do the Enter of amount.

Open first link from json list dict -> Add amount -> Open second link from json -> Add amount -> Open .... until all sites are done -> Redirect to new page https://finished.com

and my question is how am I able to do that with the information I have now?

Ad

Answer

I suggest combining your two steps: Open Product URL and Enter x items into a single step. Then you can iterate over the contents of your 'URL' array.

describe('Add To Cart', function () {
  const openUrl = function(url) {
    browser.driver
      .then(() => browser.waitForAngularEnabled(false))
      .then(() => browser.manage().window().maximize())
      .then(() => browser.get(url));
  }

  const enterAmount = function(amount) {
    browser.driver
     .then(() => utils.presenceOf(element(by.id('amount'))))
     .then(() => element(by.id('amount')).clear())
     .then(() => utils.sendKeys(element(by.id('amount')), amount))
     .then(() => done());
  }

  it('Open Product URL and enter x items', (done) => {
    for (const data of userData.URL) {
      const url = Object.keys(data)[0]
      const amount = data[url]

      openUrl(url).then(() => {
        enterAmount(amount);   
      }).then(() => done());
    }
  });

 //... finish the site
}

I also suggest that you fix your URL object by giving it proper keys like:

{
  "pageProperties": [
    {
        url: "https://testing.com/en/p/-12332423/",
        amount: "999"
    }, {
        url: "https://testing.com/en/p/-123456/",
        amount: "123"
    },
    {
        url: "https://testing.com/en/p/-456436346/",
        value: "422"
    }
   ]
}

Then you'll access its properties as:

for (const page of userData.pageProperties) { 
  openUrl(page.url).then(() => {
    enterAmount(page.value);   
  }).then(() => done());
}

// and of course access url by `page.url`
Ad
source: stackoverflow.com
Ad