我如何使用 javascript 自动化测试更改 javascript 文件中的 url
How Do i change a url in a javascript file using javascript Automation Testing
我试图在我的自动化测试中修改 javascript 文件中的 URL。这样我就可以指出服务的存根版本。我用于测试的框架是 Serenity-js(使用量角器)
在我的 HTML DOM 我有:
<script type="text/javascript" src="main.js"></script>
main.js
var DataService = (function () {
function DataService(http) {
this.http = http;
}
DataService.prototype.getStudentDetails = function (id) {
var parcel = this.http
.get('http://127.0.0.1/endpoint/' + id)
.map(function (res) {
return __WEBPACK_IMPORTED_MODULE_2__student_model__["a"].createStudent(res.json());
});
return student;
};
return DataService;
}());
我需要更改的部分是127.0.0.1/endpoint
我知道我用 $.document.write
或 $().append
冷改变 HTML DOM 但不知道如何 change/overwirte 一个 DOM元素。
这很简单,但是有很多方法可以做到!
- 作为 运行 测试前设置阶段的一部分,您可以使用
sed
或 awk
脚本预处理 main.js
文件并替换所有127...
字符串与其他内容一起出现。
- 更改生产系统中的代码以从包含所有 url 端点的配置变量中读取。然后您可以在测试脚本中更改该对象。
- 使用您的测试脚本将函数替换为其他内容。这是 @alok 在评论中建议的,但是评论缺失
prototype
,并且该方法也不应该被调用,所以代码应该是这样的:
#3 示例:
const newFnString = DataService.prototype.getStudentDetails.toString()
.replace("127.0.0.1", "something.com");
DataService.prototype.getStudentDetails = new Function(newFnString);
当然,您可以将 DataService.prototype.getStudentDetails
替换为任何给定函数,因为您可以完全控制它(除非它隐藏在闭包中)。
P.S。试图通过让 javascript 更难被篡改来保护你的系统只是通过默默无闻的安全——它不是真实的。任何攻击者都可以以任何方式读取网络请求,并在 curl
或 Postman 中重播它们。
我试图在我的自动化测试中修改 javascript 文件中的 URL。这样我就可以指出服务的存根版本。我用于测试的框架是 Serenity-js(使用量角器)
在我的 HTML DOM 我有:
<script type="text/javascript" src="main.js"></script>
main.js
var DataService = (function () {
function DataService(http) {
this.http = http;
}
DataService.prototype.getStudentDetails = function (id) {
var parcel = this.http
.get('http://127.0.0.1/endpoint/' + id)
.map(function (res) {
return __WEBPACK_IMPORTED_MODULE_2__student_model__["a"].createStudent(res.json());
});
return student;
};
return DataService;
}());
我需要更改的部分是127.0.0.1/endpoint
我知道我用 $.document.write
或 $().append
冷改变 HTML DOM 但不知道如何 change/overwirte 一个 DOM元素。
这很简单,但是有很多方法可以做到!
- 作为 运行 测试前设置阶段的一部分,您可以使用
sed
或awk
脚本预处理main.js
文件并替换所有127...
字符串与其他内容一起出现。 - 更改生产系统中的代码以从包含所有 url 端点的配置变量中读取。然后您可以在测试脚本中更改该对象。
- 使用您的测试脚本将函数替换为其他内容。这是 @alok 在评论中建议的,但是评论缺失
prototype
,并且该方法也不应该被调用,所以代码应该是这样的:
#3 示例:
const newFnString = DataService.prototype.getStudentDetails.toString()
.replace("127.0.0.1", "something.com");
DataService.prototype.getStudentDetails = new Function(newFnString);
当然,您可以将 DataService.prototype.getStudentDetails
替换为任何给定函数,因为您可以完全控制它(除非它隐藏在闭包中)。
P.S。试图通过让 javascript 更难被篡改来保护你的系统只是通过默默无闻的安全——它不是真实的。任何攻击者都可以以任何方式读取网络请求,并在 curl
或 Postman 中重播它们。