admin管理员组文章数量:1277886
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 TestBedpileComponents();
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).
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 TestBedpileComponents();
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).
Share Improve this question edited Feb 25 at 10:10 VLAZ 29.1k9 gold badges62 silver badges84 bronze badges asked Feb 24 at 19:14 meequemeeque 133 bronze badges 4 |1 Answer
Reset to default 0Found the solution myself:
It was a browser issue all along. Karma/Jasmine are behaving just fine, and open links in a new window/tab as my tests intend.
Instead, the browser's popup blocker was at fault. I had thought that I had ruled out popup blockers before posting here, because I did not see any notifications about blocked windows. Turns out that Chromium (which I used for testing) does not show such notifications, but Firefox does. At least in my respective configurations.
Setting up exception rules has resolved the issue for both browsers.
My bad.
本文标签:
版权声明:本文标题:javascript - How to use KarmaJasmine to test web application that opens a link in a new windowtab? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741246047a2364896.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
It's a demo/educational webapp
This already should be a reason to not over-engineer a test. Karma / Jasmine suite is really bad and do a poor job when testing scenarios between iFrame / not native window. Consider using Jest, Angular Testing Library, Storybook or other alternative that are way stable and reliable, if you really want to test a demo/educational app. – Jacopo Sciampi Commented Feb 25 at 9:56