Appium & Webdriver (webdriverjs) - cannot execute javascript code

家住魔仙堡 提交于 2019-12-12 15:54:06

问题


I am trying to execute javascript Appium test using WD. My code should look something like this:

"use strict";

var wd = require("wd");
var chai = require("chai");
var chaiAsPromised = require("chai-as-promised");
var jQuery = require("jQuery");

chai.use(chaiAsPromised);
chai.should();
chaiAsPromised.transferPromiseness = wd.transferPromiseness;
var desired = {
    "appium-version": "1.0",
    platformName: "iOS",
    platformVersion: "7.1",
    deviceName: "iPhone Retina (3.5-inch)",
    app: "/Users/{myuser}/Library/Developer/Xcode/DerivedData/{MyApp}/Build/Products/Debug-iphonesimulator/MyApp.app",
};

var browser = wd.promiseChainRemote("0.0.0.0", 4723);


browser.init(desired).then(function() {

    var getIframe = function(){
        //var r = jQuery('#myIframe');
        return {'response':"1"};//only for testing that this method works...
    };

    return browser

        .elementByName("Show Webview").click()
        .sleep(10000)
        .contexts().then(function (contexts) { // get list of available views. Returns array: ["NATIVE_APP","WEBVIEW_1"]
            return browser.context(contexts[1]); // choose the webview context
        })
        .execute(getIframe).then(function(res){
            console.log(res);
        })
        .sleep(10000)
        .fin(function() {
            //return browser.quit();
        });
}, function(e){console.log(e);}).done(); 

I am running this code using node mytest.js.

The problem is that I cannot execute js code. In this case I am getting the following error:

Error: [execute()] Not JSON response

What am I doing wrong?

Comments:

  1. What I finally am trying to do here is access and manipulate an iFrame in my code (in the same domain), using iFrame.contentDocument

  2. My insperation for using 'execute like this is from this post

tnx, Yaniv

UPDATE:

I have managed to execute javascript using "safeExecute" method, instead of "execute". My issue now is that I do not have access to "window" object, so I cannot run "jQuery" or "window.document.getElementById"...

来源:https://stackoverflow.com/questions/26136336/appium-webdriver-webdriverjs-cannot-execute-javascript-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!