title: Appium & Webdriver (webdriverjs) - cannot execute javascript code
body: <p>I am trying to execute javascript Appium test using <a href="https://github.com/admc/wd" rel="nofollow noreferrer">WD</a>. My code should look something like this:</p>
  
  <pre><code>"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(); 
  </code></pre>
  
  <p>I am running this code using <code>node mytest.js</code>.</p>
  
  <p>The problem is that I cannot execute js code. In this case I am getting the following error:</p>
  
  <pre><code>Error: [execute()] Not JSON response
  </code></pre>
  
  <p>What am I doing wrong? </p>
  
  <p>Comments:</p>
  
  <ol>
  <li><p>What I finally am trying to do here is access and manipulate an iFrame in my code (in the same domain), using <code>iFrame.contentDocument</code> </p></li>
  <li><p>My insperation for using 'execute like this is from <a href="https://stackoverflow.com/questions/15888183/how-to-execute-custom-javascript-code-in-webdriverjs">this</a> post</p></li>
  </ol>
  
  <p>tnx,
  Yaniv</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>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"...</p>