![](/img/trans.png)
[英]Protractor/Jasmine test browser.isElementPresent not working when looking for a class
[英]Continue loop after getting alert test from browser in protractor jasmine test
我在我的角度应用程序中用茉莉花赛跑者实现Protrator框架。 我单击具有预期结果的每个元素。 现在,我的应用程序中有一个公司列表。 每个公司有两三台机器是侧杆。 我实现了两个for循环,它们也通过公司和机器循环。 现在,在一台计算机列表中有一个错误,结果显示了来自本地主机的警报。 因此,循环中断并失败。 我想在那里实现一个条件,以便它可以检测到该条件并从循环继续。 但是我不知道如何实现它。
我的测试代码
for (let company = 0; company < await companyOption.count(); company++) {
await companyOption.get(company);
for (let machine = 0; machine < await machineList.count(); machine++) {
await click.onto(machineList.get(machine));
if (window.alert()) {
//here i want to implement if browser.switchto.alert() is appear then test should be continue to next loop
} else {
await expect(machineList.get(machine).isSelected());
}
.................... other code
}
打字稿中的警报代码
ngOnInit() {
this.route.paramMap.switchMap((params: ParamMap, index) => this.machineService.load(parseInt(params.get("machineId")))).subscribe(machine => {
this.machine = machine;
if (this.machine.type === null)
window.alert("No machine type assigned for " + this.machine.name + "\nAssign a type to view the details.");
});
}
尝试这个
for (let company = 0; company < await companyOption.count(); company++) {
await companyOption.get(company);
for (let machine = 0; machine < await machineList.count(); machine++) {
await click.onto(machineList.get(machine));
// solution! //
let errorMessage; // define a variable, which is currently undefined
try {
// try run this code, if alert is not present it'll throw an error
// if alert is present it skips is
await browser.switchTo().alert().accept();
} catch (e) {
// if there was no alert and an error was thrown than assign error to variable
// if there was alert this block will be skipped and errorMessage will stay undefined
errorMessage = e.message;
}
// this block will be executed if alert was present!
// otherwise do nothing
if (errorMessage !== undefined) {
// ... your code here ...
}
// end of solution //
}
我检查了我的代码,它仍然有效,如果仍然不起作用,请在代码的一部分中查找错误
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.