可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I run Appium server:
I try to run this code to post to Appium server:
public class AndroidTest { private AppiumDriver driver; @Before public void setUp() throws Exception { // File classpathRoot = new File(System.getProperty("user.dir")); // File appDir = new File(classpathRoot, "../../../data/app/"); // File app = new File(appDir, "Facebook.apk"); DesiredCapabilities capabilities = new DesiredCapabilities(); capabilities.setCapability(CapabilityType.BROWSER_NAME, ""); capabilities.setCapability("deviceName","10.0.0.9:5555"); capabilities.setCapability("platformVersion", "4.4.2"); capabilities.setCapability("platformName","Android"); //capabilities.setCapability("app", app.getCanonicalPath()); capabilities.setCapability("appPackage", "com.grindrapp.android"); capabilities.setCapability("appActivity", ".activity.SplashActivity"); driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities); }
but as you can see I get this error:
info: [debug] Error: Could not get the Java version. Is Java installed?
how can I fix this?
I'm trying to automatically open an app which i downloaded from the google play and I don't have its apk.
回答1:
I think this is the final solution for your problem.
As always, make a backup copy of the file before editing.
Edit the file /usr/local/lib/node_modules/appium/lib/devices/android/android-common.js
In the else if(stderr) branch around line 1034:
else if (stderr) { var firstLine = stderr.split("\n")[0]; if (new RegExp("java version").test(firstLine)) { javaVersion = firstLine.split(" ")[2].replace(/"/g, ''); } }
replace the code to read:
else if (stderr){ if(new RegExp("java version").test(stderr)){ var regex = "java version \"[0-9]+\.[0-9]+\.[0-9]+"; var stderrstring = '' + stderr + ''; var regmatch = stderrstring.match(regex); javaVersion = '' + regmatch + '' //convert object to string javaVersion.replace(/"/g,''); } }
The code should work with a regular java version response message plus the one that you are receiving.
回答2:
The file is located here:
/usr/local/lib/node_modules/appium/lib/devices/android/android-common.js
The code we are concerned with starting at line 1029 is:
androidCommon.getJavaVersion = function (cb) { exec("java -version", function (err, stdout, stderr) { var javaVersion = null; if (err) { return cb(new Error("'java -version' failed. " + err)); } else if (stderr) { var firstLine = stderr.split("\n")[0]; if (new RegExp("java version").test(firstLine)) { javaVersion = firstLine.split(" ")[2].replace(/"/g, ''); } } if (javaVersion === null) { return cb(new Error("Could not get the Java version. Is Java installed?")); } return cb(null, javaVersion); }); };
I am unable to test this on my system, but I think that a successful execution of java -version would return its response on stdout and not stderr. I would change the code to provide more information as to what is happening and help debug it.
You are getting the JavaVersion = null message at the end of the listing which means that there is nothing returned in err and parsing of stderr is not setting JavaVersion. It does not tell you if there is anything in stderr nor does it even look at stdout.
Here is a revised code listing. If the output is indeed returned on stderr, the code will now at least print it out so you can see what is returned. I added the same code branch for stdout as it was not checked at all.
androidCommon.getJavaVersion = function (cb) { exec("java -version", function (err, stdout, stderr) { var javaVersion = null; var firstLine = null; if (err) { return cb(new Error("'java -version' failed. " + err)); } else if (stderr) { firstLine = stderr.split("\n")[0]; if (new RegExp("java version").test(firstLine)) { javaVersion = firstLine.split(" ")[2].replace(/"/g, ''); }else{ return cb(new Error("Java version stderr string parse error: " + stderr)); } } else if(stdout){ firstLine = stdout.split("\n")[0]; if (new RegExp("java version").test(firstLine)) { javaVersion = firstLine.split(" ")[2].replace(/"/g, ''); }else{ return cb(new Error("Java version stdout string parse error: " + stdout)); } } if (javaVersion === null) { return cb(new Error("Could not get the Java version. Is Java installed?")); } return cb(null, javaVersion); }); }
So:
- make a backup copy of android-commons.js
- make the code change and give it a try.
This might work as written or the error messages will tell you what is being returned.
回答3:
The code in android-common.js is looking for the java version number on the first line. I had the similar issue, but my java -version returns result as
[~]$ java -version Picked up JAVA_TOOL_OPTIONS: -Dfile.encoding=UTF8 java version "1.7.0_76" Java(TM) SE Runtime Environment (build 1.7.0_76-b13) Java HotSpot(TM) 64-Bit Server VM (build 24.76-b04, mixed mode)
So I simply changed the code in android-common.js to split second line instead of first line
firstLine = stderr.split("\n")[0]; -> firstLine = stderr.split("\n")[1]