简体   繁体   English

向phantomJS无头浏览器添加jQuery选择器控件

[英]Adding jQuery selector control to phantomJS headless browser

I am trying to add jQuery selector controls to a phantomJS headless browser for testing purposes. 我试图将jQuery选择器控件添加到phantomJS无头浏览器中以进行测试。

Is it possible? 可能吗?

To this point we have been overwriting the check to see if the control exists and haven't made any attempts to manipulate values and test onChange events. 至此,我们已经覆盖了检查以查看控件是否存在,并且没有进行任何操纵值和测试onChange事件的尝试。

The test I am wanting to run is this: 我要运行的测试是这样的:

//arrange
//OnChange results in values being stored in cookies
$('#TestControl').val(cookieValue).trigger('change');

//act
var result = $.cookie(cookieName);

//assert
expect(result).toBe(cookieValue);

The problem is that I don't know how to actually create $('#TestControl') 问题是我不知道如何实际创建$('#TestControl')

For creating DOM elements in phantomJS/jasmine, you could do the following: 要在phantomJS / jasmine中创建DOM元素,可以执行以下操作:

var TestControl = $('<input id="TestControl"/>');
$(document.body).append(TestControl);

However, it is better to define the markup in html. 但是,最好在html中定义标记。 The fixture module of jasmine-jquery allows you to load HTML content to be used by your tests. jasmine-jquery的fixture模块允许您加载要由测试使用的HTML内容。 The overall workflow is as follows: 总体工作流程如下:

In myfixture.html file: 在myfixture.html文件中:

<div id="my-fixture"><input id="TestControl"/></div>

Inside your test: 测试内:

loadFixtures('myfixture.html')
$('#TestControl').val(cookieValue).trigger('change')
expect($('#my-fixture')).to...

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

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