protratcor:使用executioncript在元素上执行javascript的问题

时间:2016-09-05 07:33:46

标签: javascript selenium webdriver protractor

我在下面编写了代码来对元素执行javascript。代码如下:

 this.executeScript = function(locatorType, locatorValue, script){
        var element = this.getElement(locatorType, locatorValue);
        try{
            browser.executeScript(script, element);
        }
        catch(e){
        }
    };

我是通过页面脚本中的函数调用它,如下所示:

this.clickLoginButton = function(){     elementController.executeScript(commonFunction.getValueFromJson(elementLandingPage,'landingPage.loginButton.locator'),
            commonFunction.getValueFromJson(elementLandingPage,'landingPage.loginButton.value'),"arguments[0].click();");
    };  

使用protractor执行测试脚本时,网页上没有任何反应。输入用户名和密码等其他功能也无效。使用webdriver的直接点击API点击元素正在为我工​​作。只有在我尝试执行javascript时才会出现此问题。

长时间等待后,命令提示符会显示以下错误:

<--- Last few GCs --->

  220607 ms: Mark-sweep 1387.6 (1435.4) -> 1385.7 (1435.4) MB, 27.7 / 0 ms [all
cation failure] [GC in old space requested].
  220637 ms: Mark-sweep 1385.7 (1435.4) -> 1385.7 (1435.4) MB, 31.0 / 0 ms [all
cation failure] [GC in old space requested].
  220667 ms: Mark-sweep 1385.7 (1435.4) -> 1385.7 (1435.4) MB, 28.6 / 0 ms [las
 resort gc].
  220688 ms: Mark-sweep 1385.7 (1435.4) -> 1385.7 (1435.4) MB, 27.2 / 0 ms [las
 resort gc].


<--- JS stacktrace --->

==== JS stack trace =========================================

Security context: 000000C4BBFB4639 <JS Object>
    1: wrapCallSite(aka wrapCallSite) [C:\Users\abhinav.s\AppData\Roaming\npm\n
de_modules\protractor\node_modules\source-map-support\source-map-support.js:~30
] [pc=0000025812D46013] (this=000000C4BBF041B9 <undefined>,frame=00000260792131
1 <a CallSite with map 000002B62D3ACC21>)
    2: /* anonymous */(aka /* anonymous */) [C:\Users\abhinav.s\AppData\Roaming
npm\node_modules\protractor\node_...

FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - process out of memory

有人可以让我知道解决方案。

1 个答案:

答案 0 :(得分:2)

browser.executeScript(script, element);更改为browser.executeScript(script, element.getWebElement());解决了这个问题。

正确的代码如下:

this.executeScript = function(locatorType, locatorValue, script){
        var element = this.getElement(locatorType, locatorValue);
        try{
            browser.executeScript(script, element.getWebElement());
        }
        catch(e){
        }
};