gpt4 book ai didi

angular - Protractor 元素链接对我不起作用

转载 作者:行者123 更新时间:2023-12-02 14:03:35 25 4
gpt4 key购买 nike

我正在使用 Protractor 针对 Angular 应用程序构建一些测试操作。我试图让我的定位器尽可能易于阅读和维护,并尝试使用“元素链接”来做到这一点。根据我在 SO 和 Protractor 文档中阅读的所有内容,我认为以下定位器策略应该有效:

xpPanelPayment 变量的定义只是为了便于阅读。

let xpPanelPayment = "//div [@class='panel-heading' and text()='Payment']/following-sibling::div [@class='panel-body']";   
this.pnlPayment = element(by.xpath(`${xpPanelPayment}`));
this.valTotalPayment = element(by.xpath(`${xpPanelPayment}`))
.element(by.xpath(`//strong [text()='Total Payment:']/../following-sibling::div/strong`));

我更喜欢的是:

this.valTotalPayment = this.pnlPayment
.element(by.xpath(`//strong [text()='Total Payment:']/../following-sibling::div/strong`));

但是当我尝试这样做时,我收到一个错误,似乎表明 this.pnlPayment 未定义。也许这是一个线索?

以下是使用这些定位器的方法:

const Receipt = require('./Receipt.js').Receipt;

exports.verifyTotalPayment = (payment) => {
it(`Receipt Validation - Verify total payment $${payment}`, () => {
console.log(`Receipt.pnlPayment.locator() = '${Receipt.pnlPayment.locator()}'`);
console.log(`Receipt.valTotalPayment.locator() =
'${Receipt.valTotalPayment.locator()}'`);
expect(Receipt.valTotalPayment.getText()).toEqual(`$${payment}`);
});
}

以下是运行日志的内容:

Receipt.pnlPayment.locator() = 'By(xpath, //div [@class='panel-heading' and text()='Payment']/following-sibling::div [@class='panel-body'])'
Receipt.valTotalPayment.locator() = 'By(xpath, //strong [text()='Total Payment:']/../following-sibling::div/strong)'
[09:31:07] W/element - more than one element found for locator By(xpath, //strong [text()='Total Payment:']/../following-sibling::div/strong) - the first result will be used

看来 valTotalPayment 的“父”部分被完全忽略。我的 valTotalPayment 规范哪里出了问题?如果我使用整个 xpath 字符串而不引用父对象,valTotalPayment 会找到正确的元素,但这会破坏我想要做的事情。

最佳答案

问题来自于 valTotalPayment 的 xpath。您希望从 pnlPayment 的后代中找到 valTotalPayment

您使用 //,但使用 ./ 来表示 valTotalPayment

//表示整个页面的任意元素节点

./ 表示前一个/父元素的任何后代元素节点

最后,在编写定位器时,CSS 选择器是首选,xpath 是第二个。

如果需要的话,你可以在元素链中混合CSS选择器和xpath: element(by.css()).element(by.xpath()).element(by.css())....

关于angular - Protractor 元素链接对我不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49280981/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com