Javascript 在量角器jasmine测试中从浏览器获取警报测试后继续循环

Javascript 在量角器jasmine测试中从浏览器获取警报测试后继续循环,javascript,angular,jasmine,protractor,Javascript,Angular,Jasmine,Protractor,我在angular应用程序中使用jasmine runner实现Protor框架。我单击每个元素并获得预期结果。现在我的应用程序中有一个公司列表。每个公司都有两三台机器是侧杠。我在公司和机器上实现了两个for循环。现在在一个机器列表中出现了一个错误,结果是显示来自本地主机的警报。由于这个原因,循环中断并失败。我想在那里实现一个条件,以便它能够检测到该条件并从循环中继续。但我无法得到它,我如何才能实现它 我的测试代码 for (let company = 0; company < a

我在angular应用程序中使用jasmine runner实现Protor框架。我单击每个元素并获得预期结果。现在我的应用程序中有一个公司列表。每个公司都有两三台机器是侧杠。我在公司和机器上实现了两个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
      }
试试这个

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 //
}
for(让company=0;company
我检查了我的代码,它正在工作,如果仍然不工作,请查找代码中您部分的错误

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 //
}