javascript - 使用量角器和相对路径在 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');
在测试中。
解决方案
我不确定,是否与 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,所以我认为它应该可以工作。
推荐阅读
- java - 在不知道超类的情况下执行接口方法
- powershell - 如何根据 azure 命令的输出在 powershell 中拆分字符串
- python - 考虑两个不同标准的字符串排序列表
- codesys - J1939 - 雅马哈方向舵位置地址
- python - 尝试解码(base64)然后解压缩(gzip)的 Python3 错误
- python - 在 Python 中使用 Windows 服务帐户访问文件共享
- reactjs - React js从子Render方法设置父组件的状态
- mongodb - 为什么 MongoDB 不使用散列连接或排序合并?
- excel - 是否可以让 excel 突出显示不符合我所需格式的单元格(例如日期为 6.2.2021 与所需的 6/2/2021)?
- android - LibGDX:在屏幕之间切换时保留纹理(在内存中)