1
使用phantomjs-jasmine做一個簡單的測試模擬使用Phantomjs和測試程序按鈕點擊茉莉
//example_spec.js
describe("Click button", function() {
it ("should be become 3", function() {
var i = 0;
var button_element = $('#button');
console.log(button_element.text());
while(i < 3) {
button_element[0].click();
console.log($('#counter').text());
i ++;
}
console.log($('#counter').text());
expect($('#counter').text()).toEqual('3');
});
});
//example.js
var main = function() {
var button = document.getElementById('button');
button.addEventListener('click', function(){
var count = document.getElementById('counter');
count.innerText = parseInt(count.innerText) + 1;
});
}
window.addEventListener('load', main);
window.addEventListener('load', main);
//index.html
....
<p id='counter'>0</p>
<button id='button'></button>
....
測試結果真是奇怪
hantomjs lib/run_jasmine_test.coffee spec/TestRunner.html
Starting...
0
0
0
Click button : should be become 3
Error: Expected '1' to equal '3'.
Finished
-----------------
1 spec, 1 failure in 0.033s.
ConsoleReporter finished
一些事情一定是錯在我的代碼,任何想法?
//example-updated-jquery-version.js
var main = function() {
var button = $('#button');
$('#button').on('click', function(){
$('#counter').text(parseInt($('#counter').text()) + 1);
})
}
嗨@mamoo,這是讓我知道innerText()是不是要走的路,我已經將代碼更改爲jquery版本,但它仍然不起作用 – mko 2013-03-27 01:54:16
我已經將代碼上傳到github https: //github.com/markson/jasmine-phantomjs-test-example,請看一看 – mko 2013-03-27 01:54:37
這是因爲你在執行你的茉莉花測試之後附加'click'事件回調。注意你的操作順序;) – mamoo 2013-03-27 12:50:37