获取错误信息" this.TakeScreenshot不是函数"在量角器

时间:2017-04-06 06:37:01

标签: javascript automation jasmine protractor

我在一个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.

1 个答案:

答案 0 :(得分:0)

首先要知道isPresent(检查DOM中是否存在元素)和isDisplayed(检查DOM中是否存在元素并且可见)之间存在差异。如果DOM中不存在元素,则方法isDisplayed()将很难失败。您现在正在捕捉此错误,这也是您想要的吗?

其次,每个function都可以拥有自己的this范围。这意味着您的this.TakeScreenshot("ErrorSS")可以引用this - function(error){}范围,而不是您要引用的this

这个;-) doc可能有助于解决这个问题。我通常使用的是arrow - 函数,请参阅链接。