How to use Karma/Jasmine to test web application that opens a link in a new window/tab?

Discussão em 'Angular' iniciado por meeque, Fevereiro 24, 2025.

  1. meeque

    meeque Guest

    I have a small webapp that opens a link in a new window. The document in the new window then interacts with the original webapp through the DOM.

    The webapp itself is written in Angular, but the link is plain HTML (not even coming from an Angular template). The target document is not Angular, just plain HTML/CSS/JS:

    <a target="_blank" rel="opener" href="/assets/probes/xss.html">Click here!</a>


    Tests are set up according to Angular's component testing guide. Here's a snippet of the setup code:

    beforeEach(async () => {
    TestBed.configureTestingModule(xssDemoConfig);
    await TestBed.compileComponents();
    fixture = TestBed.createComponent(XssDemoComponent);
    fixture.detectChanges();

    // ...
    });


    This setup works like a charm for hundreds of other tests. The new test code is simple:

    el.querySelector('a').click();


    I've confirmed in the browser debug console that the selector returns the correct <a> element. (And I have other working tests that trigger click events on similar links — only difference is that those links have javascript: URLs and do not open a new window or even trigger navigation.)

    Everything works like a charm when testing manually in a browser. However, when my new tests try to click the link, nothing at all happens. No new window, but no error or log message either. My spec just runs into a timeout. (I've tried more generous timeouts, but this hasn't fixed it.)

    So, finally my questions:

    • Why is this happening?
    • Does the Angular TestBed, or Jasmine, or Karma have some hidden feature that intercepts such clicks?
    • If so, which of them is at fault here?
    • Is there any way that I could bypass this undesired behavior?
    • Is such testing feasible with Karma/Jasmine at all?
    • If not, which other tools/frameworks should I try?

    Note that this post is about integration/e2e tests. So, I do not want to mock away anything (as numerous answers to similar questions suggest). I really do want to test the interactions between the two windows (and ideally discard the new window afterwards).

    Continue reading...

Compartilhe esta Página