量角器 - 如何打开 x 数量的站点和 sendKeys
Protractor - How to open x amount of sites and sendKeys
现在我正在尝试创建一个脚本,其中我通过 json 和 URL、AMOUNT 等的字典创建了 x 个站点:
{
"URL": [{
"https://testing.com/en/p/-12332423/": "999"
}, {
"https://testing.com/en/p/-123456/": "123"
},
{
"https://testing.com/en/p/-456436346/": "422"
}
]
}
我们有Link:金额。
我现在想做的是,我想打开每个站点,然后添加给定的数量,因此首先打开 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 变量
我建议将 Open Product URL
和 Enter x items
这两个步骤合并为一个步骤。
然后您可以遍历 '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`
现在我正在尝试创建一个脚本,其中我通过 json 和 URL、AMOUNT 等的字典创建了 x 个站点:
{
"URL": [{
"https://testing.com/en/p/-12332423/": "999"
}, {
"https://testing.com/en/p/-123456/": "123"
},
{
"https://testing.com/en/p/-456436346/": "422"
}
]
}
我们有Link:金额。
我现在想做的是,我想打开每个站点,然后添加给定的数量,因此首先打开 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 变量
我建议将 Open Product URL
和 Enter x items
这两个步骤合并为一个步骤。
然后您可以遍历 '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`