Adding jQuery selector control to phantomJS headless browser

时间:2015-10-30 23:24:36

标签: javascript jquery phantomjs karma-jasmine

I am trying to add jQuery selector controls to a phantomJS headless browser for testing purposes. 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. 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')

1 个答案:

答案 0 :(得分:0)

要在phantomJS / jasmine中创建DOM元素,您可以执行以下操作:

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

但是,最好在html中定义标记。 jasmine-jquery的 fixture 模块允许您加载测试使用的HTML内容。整个工作流程如下:

在myfixture.html文件中:

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

在测试中:

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