Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/473.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 量角器-如何打开x数量的站点和SendKey_Javascript_Selenium_For Loop_Protractor - Fatal编程技术网

Javascript 量角器-如何打开x数量的站点和SendKey

Javascript 量角器-如何打开x数量的站点和SendKey,javascript,selenium,for-loop,protractor,Javascript,Selenium,For Loop,Protractor,现在,我正在尝试创建一个脚本,通过一个json,我有x个站点,其中包含URL、数量等: { "URL": [{ "https://testing.com/en/p/-12332423/": "999" }, { "https://testing.com/en/p/-123456/": "123" }, { "https://testing.com/en/p/-45643

现在,我正在尝试创建一个脚本,通过一个json,我有x个站点,其中包含URL、数量等:

{
    "URL": [{
            "https://testing.com/en/p/-12332423/": "999"
        }, {
            "https://testing.com/en/p/-123456/": "123"
        },
        {
            "https://testing.com/en/p/-456436346/": "422"
        }
    ]
}
我们有链接的地方:数量

我现在想做的是,我想打开每个站点,然后添加那些给定的金额,以便etc首先打开
https://testing.com/en/p/-12332423/
金额为
999

完成后,我们将进入下一个站点,即
https://testing.com/en/p/-123456/
123的金额等等

目前,我只通过以下方式完成了一页的工作:

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

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());

    });
一旦这样做了,我想重定向到一个特定的网站等

it('Finished all sites', (done) => {
    browser.driver
        .then(() => browser.waitForAngularEnabled(false))
        .then(() => browser.manage().window().maximize())
        .then(() => browser.get("https://finished.com"))
        .then(() => done());
});
然而,我的问题是,我不太确定在我有link:amount的地方用dict做一个列表是否有效,或者是否有更平滑的方法。(将接受新的建议),但我想通过每个网站循环,然后做金额输入

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

我的问题是如何利用我现在掌握的信息做到这一点?

尝试使用for循环(在it块之外)来迭代Url json变量

尝试使用for循环(在it块之外)要迭代Url json变量,我建议将两个步骤结合起来:
打开产品Url
输入x个项目
。 然后可以迭代“URL”数组的内容

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
}
我还建议您通过为
URL
对象提供适当的键来修复它,如:

{
  "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"
    }
   ]
}
然后,您将通过以下方式访问其属性:

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

// and of course access url by `page.url`

我建议将您的两个步骤结合起来:
打开产品URL
输入x个项目
。 然后可以迭代“URL”数组的内容

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
}
我还建议您通过为
URL
对象提供适当的键来修复它,如:

{
  "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"
    }
   ]
}
然后,您将通过以下方式访问其属性:

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

// and of course access url by `page.url`

这对我帮助很大!我确实有另一个问题,可能是独家新闻,但如果我们无法发送任何键到
const enteramum=function(amount),是否可以这样做{
然后我们跳过,类似于if-else语句,我们说,如果有一个sendkeys值,那么我们继续脚本,否则我们只跳到下一个测试用例?@wein这可以通过附加一个
作为后缀来选择amount参数。因此它将是
常量enteramum=函数(amount?{…}
。然后在
Then
方法中对
amount
执行空检查,如果
amount
为空,则继续发送密钥,否则不执行任何操作(最好执行跳过的日志)。这对我帮助很大!我确实有另一个问题可能是独家新闻,但如果我们无法向
const enteramum=function(amount)发送任何键,是否可以这样做{
然后我们跳过,类似于if-else语句,我们说,如果有一个sendkeys值,那么我们继续脚本,否则我们只跳到下一个测试用例?@wein这可以通过附加一个
作为后缀来选择amount参数。因此它将是
常量enteramum=函数(amount?{…}
。然后在
Then
方法中对
amount
执行空检查,如果
amount
为空,则继续发送密钥,否则不执行任何操作(最好执行跳过的日志)。