【发布时间】:2018-04-23 21:06:47
【问题描述】:
我刚刚在 VS Code 中使用 Protractor 编写了我的第一个 e2e 测试。这是一个困扰我好几天的奇怪问题。我不时收到此错误。我注意到有时当我关闭 VS Code 并重新打开它时。问题将消失。但是,它会在一段时间后恢复。
test.spec.ts
fit('should have an quote price', async () => {
await page.fillApplicantAge(0, '20');
const tobaccoUsageSelection = page.getRadioButton('smoker', 'no');
await page.clickElement(tobaccoUsageSelection);
const planTypeSelection = page.getNamedRadioButton('planType', 'Single');
await page.clickElement(planTypeSelection);
const coverageTypeSelection = page.getNamedRadioButton('coverageType', 'Gold');
await page.clickElement(coverageTypeSelection);
await page.fillApplicantEffectiveDate(0, 'today');
await page.fillApplicantExpiryDate(0, 'next month');
await page.setSelectBoxValueInsideChildComponent('destination', 'New York');
await page.fillTextBox('sumInsured', '5500');
const priceElement = await page.getElementByCss('h3 .amount');
expect(+(await priceElement.getText()).replace(',', '')).toBeGreaterThan(0);
});
在 test.po.ts 文件中:
async setSelectBoxValueInsideChildComponent(formFieldName: string, value: string) {
const formField = element(by.css(`[formControlName="${formFieldName}"] select`))
.element(by.css(`option[value="${value}"]`));
await formField.click();
}
getElementByCss(selector: string) {
return element(by.css(selector));
}
所以这个测试文件应该可以成功运行。它实际上已经成功运行了几次。但有时,“destination”行会抛出错误,有时“h3 .amount”行会无缘无故地抛出错误。
失败:使用定位器找不到元素:By(css selector, option[value="xxx"])
屏幕只是闪烁并很快退出。所以我无法检查发生了什么。
我很困惑。到目前为止,我关闭了 VS Code 以解决该问题。但这真的让我很烦恼,因为一旦我修改了某些内容,错误可能会再次出现。
请帮忙!提前致谢!
顺便说一句,我们如何在 VS Code 中调试 e2e 测试?我尝试了一些来自互联网的建议,但没有任何效果...... :(
【问题讨论】:
-
请显示函数:
setSelectBoxValueInsideChildComponent()和getElementByCss() -
刚刚将这两个功能添加到我的问题中。谢谢!
-
C,这两个函数没有明显的问题,我猜唯一可能的原因是你需要在这两个函数之前添加一些等待。您可以尝试出于调试目的来证明我的猜测。添加
await browser.sleep(10*1000) -
谢谢勇!我添加了 await browser.sleep(60*1000) 进行调试。事实证明,当 API 尝试获取国家/地区时出现错误。 “请求的资源错误中不存在‘Access-Control-Allow-Origin’标头”。我很困惑。我可以完美地通过 chrome 运行我的应用程序。国家总是表现出来。在 e2e Protractor 中进行测试时,为什么会抛出此错误?有谁知道?谢谢
-
通过 Chrome 运行时使用哪个主机?
标签: visual-studio-code protractor e2e-testing