简体   繁体   English

Angular / Jasmine / Chutzpah:单元测试DOM元素(Sweetalert)

[英]Angular/Jasmine/Chutzpah: Unit Testing DOM Element (Sweetalert)

I'm trying to write a Unit Test that checks if a modal window ( sweetalert ) is present in the headless browser. 我正在尝试编写一个单元测试,以检查无头浏览器中是否存在模式窗口( sweetalert )。 To test if it's present, I can use this jQuery accessor 要测试它是否存在,我可以使用此jQuery访问器

$('*').hasClass('sweet-overlay')

From testing sweetalert in Chrome, I do not believe this window is added to the DOM asynchronously. 通过在Chrome中测试sweetalert,我不认为此窗口是异步添加到DOM的。 Also, according to the documentation, there isn't a load event. 另外,根据文档,没有加载事件。

However, in the test suite, the unit test finishes before the sweetalert window is added to the DOM. 但是,在测试套件中,单元测试在将sweetalert窗口添加到DOM之前完成。 I can verify this by logging to the console. 我可以通过登录到控制台来验证这一点。

So the question is, how do I test this? 所以问题是,我该如何测试? Is there a way to add a watch on the phanton.js DOM and listen for changes? 有没有办法在phanton.js DOM上添加监视并监听更改?

Note this is in an Angular application, so if there is a way to do this with Angular, that would great. 请注意,这是在Angular应用程序中进行的,因此,如果有一种使用Angular进行此操作的方法,那将很好。

EDIT - I am not using Selenium. 编辑-我不使用硒。 the title clearly states I'm using Jasmine. 标题清楚地表明我正在使用茉莉花。

Hope this helps anyone else running into an issue with buttons clicks and Jasmine unit tests - 希望这可以帮助其他任何遇到按钮单击和Jasmine单元测试问题的人-

Turns out there is a timing issue with button clicks and assertions in Jasmine. 事实证明,Jasmine中的按钮单击和断言存在计时问题。 The unit test would execute before the button click event. 单元测试将在按钮单击事件之前执行。

In the code I was testing, I had the sweet alert window launching in a button click handler. 在我测试的代码中,我在按钮单击处理程序中启动了甜蜜警报窗口。

I refactored some of that code so the button click calls another method, showSweetAlert(). 我重构了一些代码,因此单击按钮调用了另一个方法showSweetAlert()。

In my unit test, I can call showSweetAlert() and everything works like a charm. 在我的单元测试中,我可以调用showSweetAlert(),并且一切都像一个超级按钮。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM