简体   繁体   中英

Failed to start an Appium session, err was: Error: Bad app:

I want to automated Facebook app on my android device.

I have tried to init a driver like this:

    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","Android Emulator");
        capabilities.setCapability("platformVersion", "4.4");
        capabilities.setCapability("platformName","Android");
        capabilities.setCapability("app", app.getAbsolutePath());


//        capabilities.setCapability("appPackage", "com.example.android.apis");
//        capabilities.setCapability("appActivity", ".ApiDemos");
        driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
    }

and I got:

info: --> POST /wd/hub/session {"desiredCapabilities":{"app":"/Users/eladb/WorkspaceQa/myAppiumExm/../../../data/app/ /Facebook.apk","platformVersion":"4.4","browserName":"","platformName":"Android","deviceName":"Android Emulator"}}

info: Client User-Agent string: Apache-HttpClient/4.3.4 (java 1.5)

error: Failed to start an Appium session, err was: Error: Bad app: /Users/data/app/ /Facebook.apk. App paths need to be absolute, or relative to the appium server install dir, or a URL to compressed file, or a special app name. cause: Error: Error locating the app: ENOENT, stat '/Users/data/app/ /Facebook.apk'

info: [debug] No appActivity desired capability or server param. Parsing from apk.
info: [debug] No appPackage desired capability or server param. Parsing from apk.
info: [debug] Using local app from desired caps: /Users/data/app/ /Facebook.apk
info: [debug] Got configuration error, not starting session
info: [debug] Cleaning up appium session
info: [debug] Error: Bad app: /Users/data/app/ /Facebook.apk. App paths need to be absolute, or relative to the appium server install dir, or a URL to compressed file, or a special app name. cause: Error: Error locating the app: ENOENT, stat '/Users/data/app/ /Facebook.apk'
    at null.<anonymous> (/Applications/Appium.app/Contents/Resources/node_modules/appium/lib/devices/android/android-common.js:56:13)
    at /Applications/Appium.app/Contents/Resources/node_modules/appium/lib/devices/device.js:81:16
    at FSReqWrap.oncomplete (fs.js:99:15)
info: [debug] Responding to client with error: {"status":33,"value":{"message":"A new session could not be created. (Original error: Bad app: /Users/data/app/ /Facebook.apk. App paths need to be absolute, or relative to the appium server install dir, or a URL to compressed file, or a special app name. cause: Error: Error locating the app: ENOENT, stat '/Users/data/app/ /Facebook.apk')","origValue":"Bad app: /Users/data/app/ /Facebook.apk. App paths need to be absolute, or relative to the appium server install dir, or a URL to compressed file, or a special app name. cause: Error: Error locating the app: ENOENT, stat '/Users/data/app/ /Facebook.apk'"},"sessionId":null}
info: <-- POST /wd/hub/session 500 8.318 ms - 628 

I have tried:

        capabilities.setCapability("app", "Facebook.apk"); 

and I got:

error: Failed to start an Appium session, err was: Error: Bad app: null. App paths need to be absolute, or relative to the appium server install dir, or a URL to compressed file, or a special app name. cause: Error: You passed in an app package as the 'app' capability, but didn't include appActivity. We need to know that too in order to start your app

info: [debug] No appActivity desired capability or server param. Parsing from apk.
info: [debug] No appPackage desired capability or server param. Parsing from apk.
info: [debug] Got configuration error, not starting session
info: [debug] Cleaning up appium session
info: [debug] Error: Bad app: null. App paths need to be absolute, or relative to the appium server install dir, or a URL to compressed file, or a special app name. cause: Error: You passed in an app package as the 'app' capability, but didn't include appActivity. We need to know that too in order to start your app
    at null.<anonymous> (/Applications/Appium.app/Contents/Resources/node_modules/appium/lib/devices/android/android-common.js:56:13)
    at androidCommon.configureApp (/Applications/Appium.app/Contents/Resources/node_modules/appium/lib/devices/android/android-common.js:69:14)
    at androidCommon.configure (/Applications/Appium.app/Contents/Resources/node_modules/appium/lib/devices/android/android-common.js:37:10)
    at Appium.configure (/Applications/Appium.app/Contents/Resources/node_modules/appium/lib/appium.js:250:15)
    at null.<anonymous> (/Applications/Appium.app/Contents/Resources/node_modules/appium/lib/appium.js:118:10)
    at Appium.start (/Applications/Appium.app/Contents/Resources/node_modules/appium/lib/appium.js:129:5)
    at Object.exports.createSession [as handle] (/Applications/Appium.app/Contents/Resources/node_modules/appium/lib/server/controller.js:182:16)
    at next_layer (/Applications/Appium.app/Contents/Resources/node_modules/appium/node_modules/express/lib/router/route.js:113:13)
    at Route.dispatch (/Applications/Appium.app/Contents/Resources/node_modules/appium/node_modules/express/lib/router/route.js:117:5)
    at /Applications/Appium.app/Contents/Resources/node_modules/appium/node_modules/express/lib/router/index.js:222:24
info: [debug] Responding to client with error: {"status":33,"value":{"message":"A new session could not be created. (Original error: Bad app: null. App paths need to be absolute, or relative to the appium server install dir, or a URL to compressed file, or a special app name. cause: Error: You passed in an app package as the 'app' capability, but didn't include appActivity. We need to know that too in order to start your app)","origValue":"Bad app: null. App paths need to be absolute, or relative to the appium server install dir, or a URL to compressed file, or a special app name. cause: Error: You passed in an app package as the 'app' capability, but didn't include appActivity. We need to know that too in order to start your app"},"sessionId":null}
info: <-- POST /wd/hub/session 500 9.616 ms - 712 

error: Unexpected error: Error: Can't set headers after they are sent.
    at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:335:11)
    at ServerResponse.res.set.res.header (/Applications/Appium.app/Contents/Resources/node_modules/appium/node_modules/express/lib/response.js:561:10)
    at ServerResponse.res.send (/Applications/Appium.app/Contents/Resources/node_modules/appium/node_modules/express/lib/response.js:133:12)
    at ServerResponse.res.json (/Applications/Appium.app/Contents/Resources/node_modules/appium/node_modules/express/lib/response.js:210:15)
    at ServerResponse.res.send (/Applications/Appium.app/Contents/Resources/node_modules/appium/node_modules/express/lib/response.js:121:21)
    at /Applications/Appium.app/Contents/Resources/node_modules/appium/lib/server/helpers.js:42:9
    at safely (/Applications/Appium.app/Contents/Resources/node_modules/appium/lib/server/helpers.js:306:5)
    at Layer.module.exports.catchAllHandler [as handle] (/Applications/Appium.app/Contents/Resources/node_modules/appium/lib/server/helpers.js:41:3)
    at trim_prefix (/Applications/Appium.app/Contents/Resources/node_modules/appium/node_modules/express/lib/router/index.js:261:17)
    at /Applications/Appium.app/Contents/Resources/node_modules/appium/node_modules/express/lib/router/index.js:225:9 context: [POST /wd/hub/session {"desiredCapabilities":{"app":"Facebook.apk","platformVersion":"4.4","browserName":"","platformName":"Android","deviceName":"Android Emulator"}}]
TypeError: Cannot set property 'onResetTimeout' of null
    at Appium.configure (/Applications/Appium.app/Contents/Resources/node_modules/appium/lib/appium.js:252:30)
    at null.<anonymous> (/Applications/Appium.app/Contents/Resources/node_modules/appium/lib/appium.js:118:10)
    at Appium.start (/Applications/Appium.app/Contents/Resources/node_modules/appium/lib/appium.js:129:5)

how can I make it work?

Update

I have tried:

    @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","Android Emulator");
        capabilities.setCapability("platformVersion", "4.4");
        capabilities.setCapability("platformName","Android");
        capabilities.setCapability("app", app.getCanonicalPath());

//        capabilities.setCapability("appPackage", "com.example.android.apis");
//        capabilities.setCapability("appActivity", ".ApiDemos");
        driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);

and got:

org.openqa.selenium.SessionNotCreatedException: A new session could not be created. (Original error: Bad app: /Users/data/app/Facebook.apk. App paths need to be absolute, or relative to the appium server install dir, or a URL to compressed file, or a special app name. cause: Error: Error locating the app: ENOENT, stat '/Users/data/app/Facebook.apk') (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 900 milliseconds
Build info: version: '2.43.1', revision: '5163bceef1bc36d43f3dc0b83c88998168a363a0', time: '2014-09-10 09:43:55'
System info: host: 'eladb-macbookpro.roam.corp.google.com', ip: '10.0.0.2', os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.9.5', java.version: '1.8.0_25'

I cannot verify it at the moment but my guess is that you have a blank space generating some problems here:

1. private AppiumDriver driver;
2.
3. @Before
4. public void setUp() throws Exception {
5.    File classpathRoot = new File(System.getProperty("user.dir"));
6.    File appDir = new File(classpathRoot, "../../../data/app/ "); // <== HERE
7.    File app = new File(appDir, "Facebook.apk");
8.    ...

Appium log is telling you that the generated path is:

"app":"/Users/eladb/WorkspaceQa/myAppiumExm/../../../data/app/ /Facebook.apk"

That space in line 6 at "app/ " is causing the final path to be "/Users/eladb/WorkspaceQa/myAppiumExm/../../../data/app/ /Facebook.apk" . I would rewrite line 6 like this:

File appDir = new File(classpathRoot, "../../../data/app/");

Instead of "app/ " , use "...app/"

Canonical paths

I haven't been using Java for a while so I do not remember IO APIs how they work, however you should find a better way to get the absolute path to you appfile because you have a final address which using the parent folder pattern ../ to access a resource... This is not the best. The final path should be:

"app":"/Users/data/app/Facebook.apk"

Am I right? In this answer you can see that probably you should be using getCanonicalPath rather than getAbsolutePath .

If my guess is right, the following code should work:

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","Android Emulator");
    capabilities.setCapability("platformVersion", "4.4");
    capabilities.setCapability("platformName","Android");
    capabilities.setCapability("app", app.getCanonicalPath());
    driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
}

If you want to automate an existing app...

So looks like you might want to automate an existing app, the set of capabilities is as follows for Android:

public void setUp() throws Exception {
    DesiredCapabilities capabilities = new DesiredCapabilities();
    capabilities.setCapability(CapabilityType.BROWSER_NAME, "");
    capabilities.setCapability("deviceName","Android Emulator");
    capabilities.setCapability("platformVersion", "4.4");
    capabilities.setCapability("platformName","Android");
    capabilities.setCapability("appPackage", "<java-package-name-of-the-app>");
    capabilities.setCapability("appActivity", "<android-app-activity-name>");
    driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
}

Those new capabilities: appPackage and appActivity can be found in your app's property if you inspect Settings.

我什么都没做,只是在Mac中清理空间,它起作用了。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM