下面是我的nightwatch.js测试脚本的摘录,我想将其创建到自定义命令中。
测试代码为;
"Page 1 Transission SEO Crawl paths are correct": function (browser) {
// Transmission Solr Query Result Array
var transmissionArray = ryanlinkstransmissionpage.Transmission.filter(val => typeof val === "string");
transmissionArray.sort();
// Transmission Facet Navigation
browser.elements('xpath', '//a[contains(@href,"gearbox")]', function (transmissionFacetEntry) {
var transmissionPageElement = [];
transmissionFacetEntry.value.map(function(element) {
browser.elementIdAttribute(element.ELEMENT, 'innerText', function(transmissionFacetEntryResult) {
transmissionPageElement.push(transmissionFacetEntryResult.value);
if (transmissionFacetEntry.value.length === transmissionPageElement.length) {
if (JSON.stringify(transmissionArray) === JSON.stringify(transmissionPageElement)) {
console.log("they are the same", transmissionArray, transmissionPageElement);
return true;
} else {
console.log("they are not the same", transmissionArray, transmissionPageElement);
return false;
}
}
});
});
});
},
当我运行包含此脚本的测试脚本时,测试成功并通过。
当我将其“转换”为自定义命令时,它看起来如下;
exports.command = function(ryanlinksproductionyearpage) {
var yearArray = ryanlinksproductionyearpage.ProductionYear.filter(val => typeof val === "string");
yearArray.sort();
this
.elements('xpath', '//a[contains(@href,"year")]', function (yearFacetEntry) {
var yearPageElement = [];
yearFacetEntry.value.map(function(element) {
this.elementIdAttribute(element.ELEMENT, 'innerText', function(yearFacetEntryResult) {
yearPageElement.push(yearFacetEntryResult.value);
if (yearFacetEntry.value.length === yearPageElement.length) {
if (JSON.stringify(yearArray) === JSON.stringify(yearPageElement)) {
console.log("they are the same", yearArray, yearPageElement);
return true;
}
else {
console.log("they are not the same", yearArray, yearPageElement);
return false;
}
}
});
});
});
return this;
};
(请注意, ryanlinksproductionyearpage 已从测试脚本的另一部分中引用)。
我的测试脚本现在看起来像;
"Page 1 Year Plate SEO Crawl paths are correct": function (browser) {
browser.cars_for_sale.make_level.ryan_links.yearPlateArrayComparison(ryanlinksproductionyearpage);
},
但是,我现在收到以下错误;
✖ TypeError: this.elementIdAttribute is not a function
我在自定义命令脚本中缺少明显/基本的东西吗?还是这是 elementIdAttribute 问题?
任何帮助将不胜感激。
谢谢