使用Appium测试Android应用程序时无法创建新的远程会话

时间:2017-01-23 17:53:46

标签: selenium-webdriver automated-tests appium

我正在尝试使用Appium运行Eclipse中用java编码的测试脚本示例。我通过npm运行Appium(我使用的是Appium 1.6.3,我不是Appium的新手,我之前在Mac上和Windows上运行它没有任何问题但是这次在Ubuntu它看起来有点不同而它不适用于appium应用程序,但使用控制台)

appium --address 127.0.0.1

我正在使用

  • Java的客户机 - 4.1.2.jar
  • 硒 - java的3.0.1.jar
  • 硒 - 服务器 - 独立-3.0.1.jar
  • TestNG的-6.9.10.jar

我的java类是:

import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidElement;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;

import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

public class FirstTest {

    AndroidDriver<AndroidElement> driver;

    File path = new File("/home/emna/Téléchargements/AutomationFiles/app-release.apk");


    @BeforeClass
    public void setUp() throws MalformedURLException {

        System.out.println("app Dir.--->" + path);

        DesiredCapabilities capabilities = new DesiredCapabilities();

        capabilities.setCapability("deviceName", "SM-G360H");
        capabilities.setCapability("version", "4.4.4");
        capabilities.setCapability("platformName", "Android");
        capabilities.setCapability("platformVersion", "platform");
        capabilities.setCapability("udid", "***************");
        capabilities.setCapability("app", path.getAbsolutePath());
        capabilities.setCapability("appPackage", "com.bulldozer.gaa");
        capabilities.setCapability("appActivity",
                "com.bulldozer.gaa.activities.MainActivity");
        driver = new AndroidDriver<AndroidElement>(new URL("http://127.0.0.1:4723/wd/hub"),
                capabilities);
        driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
    }

    @Test
    public void Test1() throws InterruptedException {

        // driver.wait(10000);
        System.out.println("GAA");
        driver.findElement(By.id("acceptCheckbox")).click();
        driver.findElement(By.id("continueBtn")).click();

    }

    @AfterClass
    public void tearDown() {
        driver.quit();

    }

}

但问题在于:

[TestNG] Running:
  /tmp/testng-eclipse--2049046107/testng-customsuite.xml

app Dir.--->/home/emna/Téléchargements/AutomationFiles/app-release.apk
Jan 24, 2017 1:14:44 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Attempting bi-dialect session, assuming Postel's Law holds true on the remote end
Jan 24, 2017 1:14:59 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Falling back to original OSS JSON Wire Protocol.
Jan 24, 2017 1:15:10 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Falling back to straight W3C remote end connection
FAILED CONFIGURATION: @BeforeClass setUp
org.openqa.selenium.SessionNotCreatedException: Unable to create new remote session. desired capabilities = Capabilities [{app=/home/emna/Téléchargements/AutomationFiles/app-release.apk, appPackage=com.bulldozer.gaa, appActivity=com.bulldozer.gaa.activities.MainActivity, platformVersion=platform, platformName=Android, udid=*************, deviceName=SM-G360H, version=4.4.4}], required capabilities = Capabilities [{}]
Build info: version: '3.0.1', revision: '1969d75', time: '2016-10-18 09:48:19 -0700'
System info: host: 'tarek-Vostro-3902', ip: '127.0.1.1', os.name: 'Linux', os.arch: 'amd64', os.version: '4.4.0-59-generic', java.version: '1.8.0_91'
Driver info: driver.version: AndroidDriver
    at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:91)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:141)
    at io.appium.java_client.remote.AppiumCommandExecutor.execute(AppiumCommandExecutor.java:69)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:601)
    at io.appium.java_client.DefaultGenericMobileDriver.execute(DefaultGenericMobileDriver.java:40)
    at io.appium.java_client.AppiumDriver.execute(AppiumDriver.java:1)

此处是Appium日志文件:

    [debug] [ADB] Running '/home/emna/Android/Sdk/platform-tools/adb' with args: ["-P",5037,"-s","***************","shell","am","force-stop","io.appium.unlock"]
[debug] [Logcat] Stopping logcat capture
[debug] [AndroidDriver] Not cleaning generated files. Add `clearSystemFiles` capability if wanted.
[MJSONWP] Encountered internal error running command: Error: Error occured while starting App. Original error: Permission to start activity denied.
    at Object.wrappedLogger.errorAndThrow (lib/logger.js:60:13)
    at ADB.callee$0$0$ (../../../lib/tools/apk-utils.js:80:9)
    at tryCatch (/usr/local/lib/node_modules/appium/node_modules/babel-runtime/regenerator/runtime.js:67:40)
    at GeneratorFunctionPrototype.invoke [as _invoke] (/usr/local/lib/node_modules/appium/node_modules/babel-runtime/regenerator/runtime.js:315:22)
    at GeneratorFunctionPrototype.prototype.(anonymous function) [as next] (/usr/local/lib/node_modules/appium/node_modules/babel-runtime/regenerator/runtime.js:100:21)
    at GeneratorFunctionPrototype.invoke (/usr/local/lib/node_modules/appium/node_modules/babel-runtime/regenerator/runtime.js:136:37)
    at process._tickCallback (internal/process/next_tick.js:103:7)
[HTTP] <-- POST /wd/hub/session 500 11015 ms - 223 
[HTTP] --> POST /wd/hub/session {"capabilities":{"desiredCapabilities":{"app":"/home/emna/Téléchargements/AutomationFiles/app-release.apk","appPackage":"com.bulldozer.gaa","appActivity":"com.bulldozer.gaa.activities.MainActivity","platformVersion":"platform","platformName":"Android","udid":"************","deviceName":"SM-G360H","version":"4.4.4"},"requiredCapabilities":{}}}
[debug] [MJSONWP] Bad parameters: BadParametersError: Parameters were incorrect. We wanted {"required":["desiredCapabilities"],"optional":["requiredCapabilities","capabilities","sessionId","id","sessionId","id","sessionId","id"]} and you sent ["capabilities"]
[HTTP] <-- POST /wd/hub/session 400 5 ms - 205

4 个答案:

答案 0 :(得分:1)

您忘记了最重要的功能,即.apk的路径:

capabilities.setCapability("app", "appPath");

此外,我建议您从RemoteWebDriver更改为Android驱动程序,因为您将有更多功能:

driver = new AndroidDriver(....)

ps:您可以将java-client更新为4.1.2并将testng更新为6.9.10

答案 1 :(得分:1)

原因是因为你的Appium已经使用GUI设置运行了应用程序。

同时您尝试从代码中再次运行它。

应该有什么帮助:在Appium界面中转到Android设置(基本)

  1. 应用程序面板:不要定义/启动应用程序及其活动。

  2. 功能面板:定义并选择{平台名称,自动化名称,平台版本}

答案 2 :(得分:1)

问题在于apk文件本身,我使用了另一个。apk file与我使用的相同配置,它工作正常!

答案 3 :(得分:1)

这可能是一个问题,因为据我所知,您在客户端脚本所在的同一台机器上运行 Appium,请尝试使用“http://127.0.1.1:4723/wd/hub”。这个解决方案对我有用

 AndroidDriver<AndroidElement> driver = null;
    driver = new AndroidDriver<>(new URL("http://127.0.1.1:4723/wd/hub"), capabilities);