首页 > 解决方案 > 使用量角器和相对路径在 vanilla/pure JS 测试中导入

问题描述

到目前为止,我一直在使用 Jasmine、Karma 和 Travis CI 测试我的非 Angular 网站(我只有 JS,没有节点,甚至没有 ES6)。

我现在正在尝试编写功能测试(如果我的词汇量不够,我的意思是我想测试“视觉/UI”测试的场景测试),谷歌将我引导到 Protractor。

输入

现在,我将完全无视 Travis CI。我发现“访问”我的页面的唯一方法是使用本地路径browser.get('file:///C:/local/path/to/project/index.html');

现在,我有这个测试

describe('The main page', function () {

  beforeEach(function () {
    // jasmine.Ajax.install();  // unabled to reuse that
    dv.ignoreSynchronization = true;
  });
  /*
  afterEach(function () {
    jasmine.Ajax.uninstall();
  });
  // */

  it('should display an error message in the alert if no parameters are provided', function () {
    browser.waitForAngularEnabled(false);
    get('/index.html'); // edited
    browser.sleep(500);
    element(by.tagName('ajs-content')).getText().then(console.log);
    expect(element(by.className('ajs-content')).getText()).toEqual(new EXCEPTIONS.NoParametersDetectedInURI().description);

  });
});

在这里,我收到一个错误Failed: EXCEPTIONS is not defined,但与 karma 不同,如果我将源文件包含在 protractor.conf.js 中

specs: [
    'src/lib/**/*.js',
    'src/js/**/*.js',
    'test/protractor/**/*.spec.js',
],

我收到类似的错误document is not defined,我认为我根本不应该导入这些,因为index.html“托管”(甚至不确定,我的意思是我使用的是本地的、绝对的路径......我对这个声明感到困惑) Selenium webdriver 上的所有这些都是为了自己的使用而导入的(我可以看到它在工作,这要归功于console.log)。

我想使用(导入)我自己的代码来使用EXCEPTIONS对象而不是硬代码toEqual("<some error message that I should never ever update in the exception.js file>"),但是由于我既没有使用节点也没有使用 ES6,所以我从来没有使用过某种module.export.

如您所见,我的导入并不是绝对必要的,但与对象的常量而不是字符串重复进行比较时感觉“更干净”。而且,也许,这些 UI 测试是“硬编码”的,但我仍在尝试找到一种以“vanilla JS”方式导入文件的方法。如果不是故意的,那就这样吧。

阿贾克斯嘲讽

我需要拦截 ajax 请求和模拟响应,但是jasmine.Ajax is undefined。我完全可以在我的“常规”测试(Jasmine+Karma)中使用它,所以我想知道我是否应该安装其他 npm 包,例如protractor-http-client,或者是否有一个特殊的配置需要使用jasmine.Ajax

特拉维斯

最后,我相对确定使用绝对(windows)路径不适用于 Travis CI,并且基于这个 SO question,我更新了我的代码以尝试index.html使用global.basePath = __dirname;和使用相对路径browser.get(global.basePath + '/index.html');(也尝试使用'\\',使用首字母file:///等...但是,如果我让页面休眠几秒钟,我总是在basePath,不像我使用绝对页面时)。

我意识到这些不是“相对”路径,而是“动态”绝对路径,但最后,即使将“\”替换为“/”并且字面上的字符串与我自己输入时完全相同:

let pagePath = global.indexPath.replace(/\\/g, "/");
console.log("trying to get", pagePath);
browser.get(basePath);
browser.sleep(5000);

你有没有遇到过这种情况?在 Travis 中运行后会'file:///C:/local/path/to/project/index.html'自动“解析”到正确的路径吗?如何使用相对路径?

我应该将每个标题分成一个问题吗?

编辑:

exception.js样本。它们基本上description是始终定义属性的错误的构造函数。我知道我在发帖时忘记了一些东西 ahah

let EXCEPTIONS = {
  DefaultException: function(_type, _description, _msg) {
    this.type = _type;
    this.description = _description;
    this.message = _msg || undefined;
  },

  NoParametersDetectedInURI: function (msg) {
    EXCEPTIONS.DefaultException.call(this,
      "NoParametersDetectedInURI",
      "No URI parameters detected",
      msg);
  },
  .
  .
  .
};

编辑2:

回答了相对路径部分(虽然还没有在 Travis 上测试过)。我必须设置`baseUrl:

exports.config = {
    baseUrl: 'file:///' + __dirname,
    .
    .
}

在配置文件中,然后get('/index.html');在测试中。

标签: javascriptprotractorkarma-jasminetravis-cifunctional-testing

解决方案


我不确定,是否与 Node.js 相关的语法,但至于导入/导出,您可以export/require像这样使用

export let EXCEPTIONS = {
---- your code ----
}
const EXCEPTIONS = require(pathToExceptionsFile)

describe('The main page', function () {
    ---- Test code here ---
    expect(element(by.className('ajs-content')).getText()).toEqual(new EXCEPTIONS.NoParametersDetectedInURI().description);
})

或者像这样

let EXCEPTIONS = {
---- your code ----
}

module.exports = EXCEPTIONS
const EXCEPTIONS = require(pathToExceptionsFile)

describe('The main page', function () {
    ---- Test code here ---
    expect(element(by.className('ajs-content')).getText()).toEqual(new EXCEPTIONS.NoParametersDetectedInURI().description);
})

我认为它也应该在 vanila JS 中工作。但只要你使用 Protractor,你就已经在使用 Node.js,所以我认为它应该可以工作。


推荐阅读