英文:
Check autofilled value with Playwright
问题
我正在使用Playwright进行测试,遇到了一个问题。
在网页上,我有一个包含4个字段的表单。当其中2个字段填写时,第三个字段会显示计算出的值。Expect.soft在新出现的值上返回了字符串:“”。
作为示例,我有以下代码片段:
await page.getByLabel('Buy up price').fill(buyUp); // 填写Buy up价格
await page.getByLabel('Percent of sale price').fill(percent); // 填写销售价格百分比
if (percent != '') {
let salePrice = ((+buyUp) * (+percent)/100 + (+buyUp)).toFixed(8);
const predictPrice = page.locator('//*[@id="fvf_salePrice"]');
await expect.soft(predictPrice).toHaveText(salePrice, {timeout: 5000});
} else {
await page.getByLabel('Sale price', { exact: true }).fill(salePrice); // 填写销售价格
}
在调试模式下,我看到选择器 //*[@id="fvf_salePrice"]
上存在值,但是expect.soft部分总是失败,它返回了以下内容:
期望字符串:salePrice
接收到的字符串:“”。
我看到Playwright捕获了显示值的占位符,但无法获取正确的值。实际上,我已经尝试了所有类型的选择器(Xpath、CSS等),使用了while循环等待数据更新,但没有帮助。测试是在Chromium上执行的。
这是因为数据动态更改,还是代码中有什么问题?
英文:
I am testing with Playwright and faced one problem.
On web I've a form with 4 fields. When 2 of them are filled, in third one appears calculated value. Expect.soft returns on this new appeared value received string: ""
As example I've this part of code:
await page.getByLabel('Buy up price').fill(buyUp); // fill Buy up price
await page.getByLabel('Percent of sale price').fill(percent); // fill Percent of sale price
if (percent != '') {
let salePrice = ((+buyUp) * (+percent)/100 + (+buyUp)).toFixed(8);
const predictPrice = page.locator('//*[@id="fvf_salePrice"]');
await expect.soft(predictPrice).toHaveText(salePrice, {timeout: 5000});
} else {
await page.getByLabel('Sale price', { exact: true }).fill(salePrice); // fill Sale price
}
In debug mode I see value on selector //*[@id="fvf_salePrice"] exists, but expect.soft part allways fails, it returns:
expected string: salePrice
received string: "".
I see that playwright catch placeholder, where value displyed, but can't get right value. Actually I've used all types of selectors (Xpath, CSS, etc.), used while loop to wait until data renew, but nothing helped. Testing is performed on chromium
Is it because of dinamically changing data, or something wrong in code?
答案1
得分: 0
谢谢大家,我找到了解决方案。
由于这是一个输入字段,必须将 toHaveText() 替换为 toHaveValue()。
因此,await expect.soft(predictPrice).toHaveText(salePrice, {timeout: 5000});
必须替换为 expect.soft(predictPrice).toHaveValue(salePrice, {timeout: 5000});
。
英文:
Thanks to all, I've found a solution.
As it was an input field, toHaveText() must be replaced with toHaveValue().
So await expect.soft(predictPrice).toHaveText(salePrice, {timeout: 5000}); must be replaced with expect.soft(predictPrice).toHaveValue(salePrice, {timeout: 5000});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论