使用量角器spyOn()为jQuery ajax()导致错误"ajax()方法不存在"

Using protractor spyOn() for jQuery ajax() cause error "ajax() method does not exist"

本文关键字:quot ajax 量角器 方法 不存在 spyOn 错误 jQuery      更新时间:2023-09-26

我试着测试ajax是用来提交表单的。测试量角器代码为:

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 PC。量角器相依:

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,因为它是在浏览器中定义的,而不是在量角器测试中定义的。量角器测试是在浏览器外部运行的节点进程。它使用webdriver协议与浏览器通信。

我建议您为这种情况编写一个karma单元测试。