Appium appium1.1 版本运行手机应用,junit 抛空指针异常

tom_ma · 2014年06月17日 · 最后由 恒温 回复于 2014年06月18日 · 1822 次阅读

环境:appium1.1,windows 7,htc one

代码如下:


 private AppiumDriver driver;

        @Before
        public void setUp() throws Exception {
            // set up appium
//          File classpathRoot = new File(System.getProperty("user.dir"));
            //存放app目录:apps
//          File appDir = new File(classpathRoot, "apps");
//            File app = new File(appDir, "GHome--release-tag-v4.7.0-20140606-01.apk");
            //配置webdriver
            DesiredCapabilities capabilities = new DesiredCapabilities();
            capabilities.setCapability(CapabilityType.BROWSER_NAME, "");
            capabilities.setCapability("platformName", "Android");
            capabilities.setCapability("platform", "WINDOWS");
            capabilities.setCapability("deviceName","HTC");
            capabilities.setCapability("platformVersion", "4.2");
//          capabilities.setCapability("app", app.getAbsolutePath());
            capabilities.setCapability("appPackage", "com.sdo.sdaccountkey");
            capabilities.setCapability("appActivity", ".ui.guide.TXZFirstActivity");
//          capabilities.setCapability("appActivity", ".ui.guide.TXZFreshManGuideActivity");
            //appium的独特地方,使用自己的AppiumDriver, 它本身是继承自WebDriver
            driver = new AppiumDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
            //设置超时
            driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        }
        @Test
        public void guideFreshMan() throws InterruptedException{
        //向下滑动,appium0.18的用法
           JavascriptExecutor jse=(JavascriptExecutor)driver;
            HashMap<String, Double> scriptObject=new HashMap<String, Double>();
            scriptObject.put("startX", 400.0);
            scriptObject.put("startY", 2.0);
            scriptObject.put("endX", 1.0);
            scriptObject.put("endY", 2.0);
            scriptObject.put("duration", 0.1);
            jse.executeScript("mobile: swipe", scriptObject);
            //driver.swipe(157, 1029, 156, 1, 1);   
          //点击立即体验按钮
          WebElement expierence  = null;
        //searchHisotry = driver.findElementByXPath("//android.widget.ListView//android.widget.RelativeLayout[@index=1]");
          expierence = driver.findElementByXPath("//android.widget.RelativeLayout[last()]");
          expierence.click();   
         }
        @After
        public void tearDown() throws Exception {
            driver.quit();
        }

appium 输入日志如下:

> info: UiAutomator shut down normally
> ERROR: error: Failed to start an Appium session, err was: Error: com.sdo.sdaccountkey/.ui.guide.TXZFirstActivity never started. Current: com.sdo.sdaccountkey/.ui.guide.TXZFreshManGuideActivity
> info: Cleaning up android objects
> info: Cleaning up appium session
> info: Error: com.sdo.sdaccountkey/.ui.guide.TXZFirstActivity never started. Current: com.sdo.sdaccountkey/.ui.guide.TXZFreshManGuideActivity
>     at null.<anonymous> (D:\Appium-1.1.0\node_modules\appium\lib\devices\android\adb.js:1276:12)
>     at null.<anonymous> (D:\Appium-1.1.0\node_modules\appium\lib\devices\android\adb.js:1221:7)
>     at D:\Appium-1.1.0\node_modules\appium\lib\devices\android\adb.js:153:5
>     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: Responding to client with error: {"status":33,"value":{"message":"A new session could not be created. (Original error: com.sdo.sdaccountkey/.ui.guide.TXZFirstActivity never started. Current: com.sdo.sdaccountkey/.ui.guide.TXZFreshManGuideActivity)","origValue":"com.sdo.sdaccountkey/.ui.guide.TXZFirstActivity never started. Current: com.sdo.sdaccountkey/.ui.guide.TXZFreshManGuideActivity"},"sessionId":null}
> POST /wd/hub/session 500 26417ms - 404b

Eclipse 运行时的截图如下:

PS:同样的环境,我替换掉里面的变量,可以在手机淘宝上成功运行代码。。这尼玛算咋回事呢?有小伙伴能帮忙看下异常引起的原因吗? 万分感谢!

共收到 5 条回复 时间 点赞

另外应用启动时,logcat 输入如下:
cmp=com.sdo.sdaccountkey/.ui.guide.TXZFirstActivity} from pid 1260
06-17 16:57:02.733: I/ActivityManager(698): Start proc com.sdo.sdaccountkey for activity com.sdo.sdaccountkey/.ui.guide.TXZFirstActivity: pid=10645 uid=10096 gids={50096, 1015, 3003, 5012, 1006, 1028}
06-17 16:57:03.003: I/ActivityManager(698): START u0 {cmp=com.sdo.sdaccountkey/.ui.guide.TXZFreshManGuideActivity} from pid 10645
06-17 16:57:03.323: I/ActivityManager(698): Displayed com.sdo.sdaccountkey/.ui.guide.TXZFreshManGuideActivity: +318ms (total +614ms)

这是 log 信息是在控制台输入:abd shell logcat 然后点击你要测试的应用,然后出现的这些信息吗?我的出特别多的信息,把这些重要的都给覆盖了。

把这两句

capabilities.setCapability("appPackage", "com.sdo.sdaccountkey");
            capabilities.setCapability("appActivity", ".ui.guide.TXZFirstActivity");

去掉试试看。
另外加头像。

@lihuazhang 头像已加,另外尝试亲建议的去掉那两行后,appium 会直接报错。
info: Responding to client with error: {"status":33,"value":{"message":"A new session could not be created. (Original error: No app set; either start appium with --app or pass in an 'app' value in desired capabilities, or set androidPackage to launch pre-existing app on device)","origValue":"No app set; either start appium with --app or pass in an 'app' value in desired capabilities, or set androidPackage to launch pre-existing app on device"},"sessionId":null}

capabilities.setCapability("appActivity", ".ui.guide.TXZFirstActivity");

appActivity 是用全路径试试

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册