我在JS文件中编写了一个函数TakeScreenshot 说base.js保存屏幕截图并返回我屏幕截图路径的实现如下。
this.TakeScreenshot = function(screenShotName) {
//some stuff.....
browser.takeScreenshot().then(function (png) {
var stream = fs.createWriteStream(screenshotPath);
stream.write(new Buffer(png, 'base64'));
stream.end();
});
return screenshotPath;
};
I am calling the above function in another function called "isTruelyPresent". This function will take a screenshot if element is present/not present on UI. The implementation is as below:
this.isTrulyPresent = function(elementToCheckVisibilityOf, element2) {
return elementToCheckVisibilityOf.isDisplayed().then(function (isDisplayedd) {
var myObj;
myObj = this.TakeScreenshot(element2);
// some stuff
console.log('isDisplayed'+isDisplayedd);
return isDisplayedd;
}).then(null, function (error) {
var myObj;
myObj = this.TakeScreenshot("ErrorSS");
console.log('A NoSuchElement exception was throw because the element is NOT displayed so we return false');
return false;
});
};
Protractor script is throwing an error "this.TakeScreenshot is not a function". Could any one please help me to resolve the issue.
首先要注意,isPresent
(检查 DOM 中是否存在元素(和isDisplayed
(检查元素是否存在于 DOM 中并且可见(之间存在差异。如果 DOM 中不存在某个元素,则方法isDisplayed()
将严重失败。您现在正在捕获此错误,这也是您想要的吗?
其次,每个function
都可以有自己的this
范围。这意味着您的this.TakeScreenshot("ErrorSS")
可以引用function(error){}
的this
范围,而不是要引用的this
。
这 ;-(文档可能有助于解决此问题。我通常使用的是arrow
函数,请参阅链接。