NightwatchJS 不传递变量
NightwatchJS not passing variable
出于范围界定的目的,我已经将变量分开,我能够使用来自 url1 的相应 DOM 元素填写所有变量,但是当我尝试将它们传递给 url2 字段时,它们被设置到 undefined
就像从未设置变量一样。然而 console.log 有效......我也将一个函数传递给 .setValue('#address-serviceability-form', '', '', function(e){ // set the value here})
但我仍然未定义......这是变量范围问题吗?
describe('Ecosia.org Demo', function() {
var address ='';
var streetNumber = '';
var city = '';
var stateAndZip = '';
var state = ''
var zip = '';
before(browser => browser.url('url1.com'));
test('Extract random address', function (browser) {
browser
.getText('.rand_large li:first-child', function(result) {
address = result.value.split("\n");
streetNumber = address[0];
console.log('streetNumber', streetNumber) //this works and logs correctly
city = address[1].split(",")[0];
stateAndZip = address[1].split(",")[1];
state = stateAndZip.split(" ")[1];
zip = stateAndZip.split(" ")[2];
})
.url('url2.com')
.waitForElementVisible('form')
.setValue('#address-serviceability-form', streetNumber) // this is set to undefined
.pause(5000)
});
after(browser => browser.end());
});
在我看来,您对 NodeJS 回调的理解存在问题。
您应该可以使用 async/await 解决此问题。您的代码没有等待回调被解析,并且在用于设置值函数之前未分配值。你可以试试下面的方法。
test('Extract random address', async function(browser) {
await browser
.getText('.rand_large li:first-child', function(result) {
address = result.value.split("\n");
streetNumber = address[0];
console.log('streetNumber', streetNumber) //this works and logs correctly
city = address[1].split(",")[0];
stateAndZip = address[1].split(",")[1];
state = stateAndZip.split(" ")[1];
zip = stateAndZip.split(" ")[2];
})
browser.url('url2.com')
.waitForElementVisible('form')
.setValue('#address-serviceability-form', streetNumber) // this is set to undefined
.pause(5000)
});
出于范围界定的目的,我已经将变量分开,我能够使用来自 url1 的相应 DOM 元素填写所有变量,但是当我尝试将它们传递给 url2 字段时,它们被设置到 undefined
就像从未设置变量一样。然而 console.log 有效......我也将一个函数传递给 .setValue('#address-serviceability-form', '', '', function(e){ // set the value here})
但我仍然未定义......这是变量范围问题吗?
describe('Ecosia.org Demo', function() {
var address ='';
var streetNumber = '';
var city = '';
var stateAndZip = '';
var state = ''
var zip = '';
before(browser => browser.url('url1.com'));
test('Extract random address', function (browser) {
browser
.getText('.rand_large li:first-child', function(result) {
address = result.value.split("\n");
streetNumber = address[0];
console.log('streetNumber', streetNumber) //this works and logs correctly
city = address[1].split(",")[0];
stateAndZip = address[1].split(",")[1];
state = stateAndZip.split(" ")[1];
zip = stateAndZip.split(" ")[2];
})
.url('url2.com')
.waitForElementVisible('form')
.setValue('#address-serviceability-form', streetNumber) // this is set to undefined
.pause(5000)
});
after(browser => browser.end());
});
在我看来,您对 NodeJS 回调的理解存在问题。
您应该可以使用 async/await 解决此问题。您的代码没有等待回调被解析,并且在用于设置值函数之前未分配值。你可以试试下面的方法。
test('Extract random address', async function(browser) {
await browser
.getText('.rand_large li:first-child', function(result) {
address = result.value.split("\n");
streetNumber = address[0];
console.log('streetNumber', streetNumber) //this works and logs correctly
city = address[1].split(",")[0];
stateAndZip = address[1].split(",")[1];
state = stateAndZip.split(" ")[1];
zip = stateAndZip.split(" ")[2];
})
browser.url('url2.com')
.waitForElementVisible('form')
.setValue('#address-serviceability-form', streetNumber) // this is set to undefined
.pause(5000)
});