![](/img/trans.png)
[英]In JavaScript, how do I get the URL of the JavaScript file, not the location 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,但不知道如何更改/覆盖DOM元素。
这很简单,但有很多方法可以做到这一点!
sed
或awk
脚本预处理main.js
文件,并将所有出现的127...
字符串替换为其他内容。 prototype
,并且该方法也应该没有被调用,所以代码看起来应该是这样的: #3的例子:
const newFnString = DataService.prototype.getStudentDetails.toString()
.replace("127.0.0.1", "something.com");
DataService.prototype.getStudentDetails = new Function(newFnString);
当然,您可以用任何给定的函数替换DataService.prototype.getStudentDetails
,因为您可以完全控制它(除非它隐藏在闭包中)。
PS试图通过更难以篡改javascript来保护您的系统只是通过默默无闻的安全 - 这不是真的。 任何攻击者无论如何都可以只读取网络请求,并以curl
或Postman重放它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.