下面是我使用 casperjs 和 mockjax 的代码。但是,mockjax 不能拦截 ajax 调用。它返回 ajax 响应而不是模拟响应。此外, $.mockjax.mockedAjaxCalls().length 这不会打印任何东西。知道我在这里做什么吗?
除了外部评估上下文之外,有什么方法可以实际打印 $.mockjax.mockedAjaxCalls() 的值吗?请注意,mock4.png 确实显示了一个屏幕截图,表明 mockjax 回调没有发生。
var casper = require("casper").create({
verbose: true,
logLevel: 'error',
clientScripts: ["jquery.mockjax.js", "jquery.js", "json2.js"]
});
casper.on('remote.message', function(msg) {
this.echo('remote message caught: ' + msg);
})
casper.start('http://xyz:9000/abc', function() {
this.evaluate(function() {
this.capture('1.png')
$.mockjax({
url: '/foo1',
responseTime: 100,
dataType: 'json',
responseText: {
sessionTimeoutRedirectUrl: "https://foo/hello",
errorCode: "error.session.timeout"
}
});
});
this.capture('xyz123.png');
});
casper.waitForSelector('#order-wrapper', function() {
this.click('#method-field-cc');
this.capture('mock2.png');
});
casper.waitForSelector('#cc-card-number', function() {
this.capture('mock.png');
this.fillSelectors('form[id="cc-form"]', {
'input[id="cc-card-number"]': '4112344112344113',
'input[id="cc-first-name"]': 'first',
'input[id="cc-last-name"]': 'last',
'select[id="cc-expiry-month"]': '10',
'select[id="cc-expiry-year"]': '2016',
'input[id="tax-field-postal-code"]': '95051',
'input[id="cc-security-code"]': '123'
}, true);
this.click('#review-newcc');
this.capture('mock3.png');
});
casper.wait(1000, function() {
this.echo("I've waited for a second.");
casper.evaluate(function() {
this.echo("I've waited for a second." + $.mockjax.mockedAjaxCalls().length)
console.log($.mockjax.mockedAjaxCalls());
});
});
casper.evaluate(function() {
this.echo("I've waited for a second." + $.mockjax.mockedAjaxCalls().length)
});
casper.wait(1000, function() {
this.capture('mock4.png')
});
casper.run();