首页 > 解决方案 > 将一个角度应用程序导航到另一个角度应用程序时量角器超时错误

问题描述

我有两个角度应用程序

  1. 登录

  2. 对于业务逻辑

我尝试使用量角器开始对这些应用程序进行自动化测试。但面临登录后从第二个应用程序(第一个应用程序)获取元素详细信息的问题。

这些文件是

specs: [
        'login.js',// 1st application 
        'navigatetoRegisterReport.js',// page loaded by menu navigation from 1st application
        'requestCreation.js',// 2nd application page loaded by `browser.get(2nd app Url)`
        'navigateToDcOtherInvoice.js'// navigation to another screen in my 2nd application 
    ],

我已经通过以下方式完成了登录逻辑,并且运行良好

async first() {
        await browser.driver.get(browser.baseUrl).then(async () => {
            await browser.wait(ExpectedConditions.elementToBeClickable(element(by.linkText("Other User"))), 30000);
            await element(by.linkText("Other User")).click().then(() => {
                browser.wait(ExpectedConditions.elementToBeClickable(element(by.css('#username'))), 30000);
                element(by.css('#username')).sendKeys('XXXXX').then(() => {
                    browser.wait(ExpectedConditions.elementToBeClickable(element(by.id("password-field"))), 30000);
                    element(by.id("password-field")).sendKeys('XXXXX').then(() => {
                        browser.wait(ExpectedConditions.elementToBeClickable(element(by.id('login-submit'))), 30000);
                        element(by.id('login-submit')).click().then(async () => {
                            const dashboardImage = await element(by.css("app-dashboard .dashboard-image"));
                            browser.wait(ExpectedConditions.elementToBeClickable(dashboardImage), 30000);
                            dashboardImage.isDisplayed().then((value) => {
                                if (value == true) {
                                    console.log('dashborad image is displayed')
                                } else {
                                    console.log('dashborad image is not identified')
                                }
                            }).catch(error => console.error('caught error while login', error));;
                        }).catch(error => console.error('caught error 7', error));;
                    }).catch(error => console.error('caught error on password', error));;
                }).catch(error => console.error('caught error on user name', error));;
            }).catch(error => console.error('caught error tab click', error));;
        }).catch(error => console.error('caught error on load', error));
    }

但是在从第二个应用程序获取元素值时,我得到了错误

async requestCreation(date: string, report: string) {

        await browser.get('https://2ndapplication/xxx/xxxx').then(async () => {
            var selectDate = element(by.xpath("//input[@id='icon-right']"));
            var reportType = element(by.xpath("//input[@placeholder='Report Type']"));
            browser.wait(ExpectedConditions.elementToBeClickable(selectDate), 30000);
            selectDate.clear();
            selectDate.sendKeys(date)
            browser.wait(ExpectedConditions.elementToBeClickable(reportType), 30000);
            reportType.click();
            reportType.clear();
            reportType.sendKeys(report)
        }).catch(error => console.error('caught error on requestCreation', error));
    }

错误:

ScriptTimeoutError: script timeout
  (Session info: chrome=88.0.4324.190)
  (Driver info: chromedriver=88.0.4324.96 (68dba2d8a0b149a1d3afac56fa74648032bcf46b-refs/branch-heads/4324@{#1784}),platform=Windows NT 10.0.18363 x86_64)
    at Object.checkLegacyResponse (E:\updatedCode\backup\node_modules\selenium-webdriver\lib\error.js:546:15)
    at parseHttpResponse (E:\updatedCode\backup\node_modules\selenium-webdriver\lib\http.js:509:13)
    at doSend.then.response (E:\updatedCode\backup\node_modules\selenium-webdriver\lib\http.js:441:30)
    at process._tickCallback (internal/process/next_tick.js:68:7)
From: Task: Protractor.waitForAngular() **- Locator: By(xpath, //input[@id='icon-right'])

我可以在浏览器中看到该元素并且它是可见的。但是量角器抛出错误,因为它不存在。我知道如果我为WaitForAngularDisabled(false). 但是这两个应用程序都仅由 Angular 实现。所以我不想通过禁用角度来失去任何量角器功能。那么如何通过量角器测试两个角度应用呢?

版本:

标签: angularautomationjasmineprotractor

解决方案


正如我在评论中提到的,您的问题是因为您的 Angular 应用程序不断地在后台轮询某些东西,即使在视觉上它可能看起来页面已经准备好。你可以在量角器页面这里阅读更多关于它的信息。不幸的是,我只做测试自动化,从未尝试在应用程序端解决问题。但是,无论如何,我总是倾向于禁用量角器的等待角度,所以它从来没有真正困扰过我


我知道您已经在另一篇文章中看到了我的答案,我只是想在这里背诵一些以供大家参考

  1. 确定您的页面是否为 Angular:在浏览器中和“控制台”选项卡运行命令中打开开发控制台
getAllAngularTestabilities()

如果输出是getAllAngularTestabilities is not defined,那么您的页面不是有角度的,请转到步骤 #3 以禁用内置等待

  1. 如果您的页面是有角度的,那么庆祝还为时过早,因为 Protractor 可能仍然无法在本地工作。在控制台中运行这些命令以检查它是否存在
getAllAngularTestabilities()[0]._ngZone.hasPendingMacrotasks
getAllAngularTestabilities()[0]._ngZone.hasPendingMicrotasks

如果其中任何一个返回true(如果有待处理的微任务或宏任务),则转到最后一步。如果都是false,恭喜,您可以使用内置量角器的等待角。但是,如果您不喜欢它,我不喜欢它,请阅读最后一步以了解如何禁用它

  1. 运行上述命令。但!它返回一个promise,需要处理,最好使用await关键字
await browser.waitForAngularEnabled(false)

完整答案在这里


推荐阅读