单击后打开的非角度页面 [英] Non-angular page opened after a click

查看:23
本文介绍了单击后打开的非角度页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现以下测试场景:

I'm trying to implement the following test scenario:

  • 点击页面上的标志
  • 断言打开了一个新的浏览器窗口(Chrome 中的选项卡)并检查当前 URL

问题是在新浏览器窗口中打开的页面是一个非角度页面,而我正在执行点击的主页是一个角度页面.

The problem is that the page opened in a new browser window is a non-angular page while the main page I'm performing the click in is an angular page.

这是我的第一次尝试:

it("should show logo", function () {
    var logo = scope.page.logo;
    expect(logo.isDisplayed()).toEqual(true);

    // opens a new page on click
    logo.click().then(function () {
        browser.getAllWindowHandles().then(function (handles) {
            browser.switchTo().window(handles[1]).then(function () {
                expect(browser.getCurrentUrl()).toEqual('http://myurl.com/');
            });

            // switch back to the main window
            browser.switchTo().window(handles[0]);
        });
    });
});

失败:

错误:等待量角器与页面同步时出错:在窗口上找不到角度"

Error: Error while waiting for Protractor to sync with the page: "angular could not be found on the window"

这是可以理解的.

我的第二次尝试是使用 ignoreSynchronization 布尔标志:

My second attempt was to play around with ignoreSynchronization boolean flag:

browser.ignoreSynchronization = true;
logo.click().then(function () {
    browser.getAllWindowHandles().then(function (handles) {
        browser.switchTo().window(handles[1]).then(function () {
            expect(browser.getCurrentUrl()).toEqual('http://myurl.com/');
        });

        // switch back to the main window
        browser.switchTo().window(handles[0]);
    });

这实际上使这个特定的测试通过而没有任何错误,但它会影响在这个测试之后执行的每个测试,因为 browser 是一个全局对象并且在测试之间共享 - 量角器不再与 angular on 同步导致各种错误的页面.

This actually makes this particular test pass without any errors, but it affects every test executed after this one, because browser is a global object and is shared between tests - protractor no longer syncs with angular on a page which results into all sorts of errors.

我应该如何实施测试?

作为一种解决方法,我可以更改 restartBrowserBetweenTests 设置true 并更改 ignoreSynchronization 值没有任何问题 - 但它会显着减慢测试速度.

As a workaround, I can change the restartBrowserBetweenTests setting to true and change ignoreSynchronization value without any problems - but it slows down the tests dramatically.

推荐答案

验证完成后,您可以将 ignoreSynchronization 设置为 false.但是,请注意 ignoreSynchronization 是同步的,而其他所有内容(单击/获取/等)都是异步的,因此您需要小心.可能最安全的方法是在 beforeEach 中将其设置为 true,在 afterEach 中将其设置为 false,然后在该测试中测试单个徽标单击.

You can set ignoreSynchronization to be false once your verification is done. However, note that ignoreSynchronization is synchronous while everything else (click/get/etc) is asynchronous, so you need to be careful with that. Probably the safest way is to set it to true in beforeEach and false in afterEach, and just test that single logo click in that test.

这篇关于单击后打开的非角度页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆