gpt4 book ai didi

javascript - 使用 Protractor spy On()进行jQuery ajax()导致错误 "ajax() method does not exist"

转载 作者:行者123 更新时间:2023-12-02 17:03:13 24 4
gpt4 key购买 nike

我试图测试ajax用于表单提交。测试的 Protractor 代码为:

describe('login.php', function() {

it("should use ajax on submit", function() {
browser.get('/login.php');
spyOn($, "ajax");
$("form#frmLogin [type='submit']").click();
expect($.ajax).toHaveBeenCalled();
});
});

失败并显示消息:

Error: ajax() method does not exist

出了什么问题?

其他测试进展顺利。这是 Windows 电脑。 Protractor 配置:

exports.config = {
allScriptsTimeout: 11000,

specs: [
'e2e/*.js',
],

capabilities: {
'browserName': 'chrome',
'chromeOptions': {
args: ['--test-type']
}
},

chromeOnly: true,

baseUrl: 'http://mysite.local/',

framework: 'jasmine',

jasmineNodeOpts: {
defaultTimeoutInterval: 30000
}
};

我的环境有问题吗?

最佳答案

您无法监视 jQuery,因为它是在您的浏览器中定义的,而不是在 Protractor 测试中定义的。您的 Protractor 测试是在浏览器外部运行的节点进程。它使用 webdriver 协议(protocol)与浏览器通信。

我建议您针对这种情况在 karma 中编写一个单元测试。

关于javascript - 使用 Protractor spy On()进行jQuery ajax()导致错误 "ajax() method does not exist",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25502975/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com