java Appium:无法创建新会话

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26839118/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 10:44:32  来源:igfitidea点击:

Appium: A new session could not be created

javaandroidautomationappium

提问by paul

Before posting this question I went to these questions on Stackoverflow and other forums:

在发布这个问题之前,我去了 Stackoverflow 和其他论坛上的这些问题:

  1. A new session could not be created. (Original error: Instruments crashed on startup)
  2. Appium Error : A new session could not be created. (Original error: Did not get session redirect from Chromedriver)
  3. https://github.com/appium/appium/issues/2981
    and did few changes in my code below:

    Code:

    package test.appium;
    
    import io.appium.java_client.AppiumDriver;
    
    import java.io.File;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.util.concurrent.TimeUnit;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.remote.CapabilityType;
    import org.openqa.selenium.remote.DesiredCapabilities;
    import org.testng.annotations.Test;
    
    public class WordPad {
    WebDriver dr;
    
    @Test
    public void testApp() throws MalformedURLException, InterruptedException {
        String apkpath = "D:\apkdump\blackcarbon.wordpad.apk";
        File app = new File (apkpath);
        DesiredCapabilities capabilities= new DesiredCapabilities();
        capabilities.setCapability(CapabilityType.BROWSER_NAME,"");
        capabilities.setCapability("deviceName","TestOneAdb1");
        capabilities.setCapability("appium-version", "1.2.4.1");
        capabilities.setCapability("platformName","Android");
        capabilities.setCapability("app",app.getAbsolutePath());
        capabilities.setCapability("appPackage", "com.wordpad");
        capabilities.setCapability("appActivity", "com.wordpad.Main");
        dr = new AppiumDriver(new URL("http://127.0.0.1:4723/wd/hub"),capabilities);                    
        dr.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
        dr.quit();      
    }   
    }
    

    but I am still getting the error:

    info: [debug] UiAutomator shut down normally

    error: Failed to start an Appium session, err was: Error: Activity used to start app doesn't exist or cannot be launched! Make sure it exists and is a launchable activity

    info: [debug] Cleaning up android objects

    info: [debug] Cleaning up appium session

    info: [debug] Error: Activity used to start app doesn't exist or cannot be launched! Make sure it exists and is a launchable activity

    at null.<anonymous> (D:\MobileTesting\Appium\node_modules\appium\node_modules\appium-adb\lib\adb.js:1143:21)
    
    at null.<anonymous> (D:\MobileTesting\Appium\node_modules\appium\node_modules\appium-adb\lib\adb.js:172:9)
    
    at ChildProcess.exithandler (child_process.js:635:7)
    
    at ChildProcess.EventEmitter.emit (events.js:98:17)
    
    at maybeClose (child_process.js:743:16)
    
    at Process.ChildProcess._handle.onexit (child_process.js:810:5)
    

    info: [debug] Responding to client with error: {"status":33,"value":{"message":"A new session could not be created. (Original error: Activity used to start app doesn't exist or cannot be launched! Make sure it exists and is a launchable activity)","origValue":"Activity used to start app doesn't exist or cannot be launched! Make sure it exists and is a launchable activity"},"sessionId":null}

    info: [37m<-- POST /wd/hub/session [39m[31m500[39m[90m 51290.254 ms - 346[39m [90m[39m What I tried:

    1. Install app manually on emulator and check if its working: It was working
    2. Once the app is installed via code: I can play with manually on emulator, it works perfectly fine.
    3. Downgrade Appiumversion from latest 1.1.0tried but same error
    4. Specify Appiumversion capabilities in code, tried but still same error.
  1. 无法创建新会话。(原始错误:仪器在启动时崩溃)
  2. Appium 错误:无法创建新会话。(原始错误:没有从 Chromedriver 获得会话重定向)
  3. https://github.com/appium/appium/issues/2981
    并在下面的代码中做了一些更改:

    代码:

    package test.appium;
    
    import io.appium.java_client.AppiumDriver;
    
    import java.io.File;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.util.concurrent.TimeUnit;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.remote.CapabilityType;
    import org.openqa.selenium.remote.DesiredCapabilities;
    import org.testng.annotations.Test;
    
    public class WordPad {
    WebDriver dr;
    
    @Test
    public void testApp() throws MalformedURLException, InterruptedException {
        String apkpath = "D:\apkdump\blackcarbon.wordpad.apk";
        File app = new File (apkpath);
        DesiredCapabilities capabilities= new DesiredCapabilities();
        capabilities.setCapability(CapabilityType.BROWSER_NAME,"");
        capabilities.setCapability("deviceName","TestOneAdb1");
        capabilities.setCapability("appium-version", "1.2.4.1");
        capabilities.setCapability("platformName","Android");
        capabilities.setCapability("app",app.getAbsolutePath());
        capabilities.setCapability("appPackage", "com.wordpad");
        capabilities.setCapability("appActivity", "com.wordpad.Main");
        dr = new AppiumDriver(new URL("http://127.0.0.1:4723/wd/hub"),capabilities);                    
        dr.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
        dr.quit();      
    }   
    }
    

    但我仍然收到错误:

    信息:[调试] UiAutomator 正常关闭

    错误:无法启动 Appium 会话,错误是:错误:用于启动应用程序的活动不存在或无法启动!确保它存在并且是一个可启动的活动

    信息:[调试] 清理 android 对象

    信息:[调试] 清理 appium 会话

    信息:[调试] 错误:用于启动应用程序的活动不存在或无法启动!确保它存在并且是一个可启动的活动

    at null.<anonymous> (D:\MobileTesting\Appium\node_modules\appium\node_modules\appium-adb\lib\adb.js:1143:21)
    
    at null.<anonymous> (D:\MobileTesting\Appium\node_modules\appium\node_modules\appium-adb\lib\adb.js:172:9)
    
    at ChildProcess.exithandler (child_process.js:635:7)
    
    at ChildProcess.EventEmitter.emit (events.js:98:17)
    
    at maybeClose (child_process.js:743:16)
    
    at Process.ChildProcess._handle.onexit (child_process.js:810:5)
    

    信息:[调试] 响应客户端错误:{"status":33,"value":{"message":"无法创建新会话。(原始错误:用于启动应用程序的活动不存在或无法启动!确保它存在并且是一个可启动的 Activity)","origValue":"用于启动应用程序的 Activity 不存在或无法启动!确保它存在并且是一个可启动的 Activity"},"sessionId" :空值}

    信息:[37m<-- POST /wd/hub/session [39m[31m500[39m[90m 51290.254 ms - 346[39m [90m[39m] 我试过的:

    1. 在模拟器上手动安装应用程序并检查它是否工作:它正在工作
    2. 通过代码安装应用程序后:我可以在模拟器上手动玩,它工作得很好。
    3. Appium从最新1.1.0尝试但同样的错误降级版本
    4. Appium在代码中指定版本功能,尝试但仍然是同样的错误。

Let me know what I am missing, how can I launch app after installation?

让我知道我缺少什么,安装后如何启动应用程序?

回答by Abhishek Swain

Possible Causes:

可能的原因:

1. It seems the issue is with one more missing capability or improper android version:

1. 问题似乎是缺少一项功能或不正确的 android 版本:

capabilities.setCapability("platformVersion", "4.4");

As you are not using 'Selendroid' as automation mode, so the emulator or device you are using for testing should be of Android 4.2+ .

由于您没有使用“Selendroid”作为自动化模式,因此您用于测试的模拟器或设备应该是 Android 4.2+ 。

Please make this sure and mention the above desired capability in the list.

请确保这一点并在列表中提及上述所需的功能。

2. Instead of using

2.代替使用

capabilities.setCapability("appActivity", "com.wordpad.Main");

try with

尝试

capabilities.setCapability("appActivity", ".Main");

回答by kiedysktos

I had similar problem. My error message was also

我有类似的问题。我的错误信息也是

"Activity used to start app doesn't exist or cannot be launched! Make sure it exists and is a launchable activity".

“用于启动应用程序的活动不存在或无法启动!确保它存在并且是可启动的活动”。

In my case I had different main activity set in two places.

就我而言,我在两个地方设置了不同的主要活动。

First one - Appium GUI:

第一个 - Appium GUI:

Appium --> Android Settings --> Main Activity, Package and App Path

Second one - in the code:

第二个 - 在代码中:

capabilities.setCapability("appPackage", "com.company.AppName");
capabilities.setCapability("appActivity", "com.company.AppName.AppMainActivity");

Those two above have to be consistent - the same package and activity.

以上两者必须一致 - 相同的包和活动。

回答by user6136

I had the same error, and it turned out to be because I'd changed my project's directory name, so the path to the apk was wrong. A silly mistake for sure, but probably worth checking just in case.

我有同样的错误,结果是因为我更改了项目的目录名称,因此apk的路径错误。肯定是一个愚蠢的错误,但可能值得检查以防万一。