Appium [已解决] 真机已安装 app,不要每次都重新安装 apk ,如何执行其他用例时能获取到 driver 属性信息

strayeagle · 2014年09月25日 · 最后由 fengcanfly 回复于 2015年10月14日 · 1717 次阅读

真机上已经调用如下类方法进行了安装 app

class installApp():
    '''  安装app   '''
    def installAndroidApp(self):
        '''  定义安装app函数--安卓     '''
        # Returns abs path relative to this file and not cwd
        PATH = lambda p: os.path.abspath(os.path.join(os.path.dirname(__file__), p))

        desired_caps = {}
        desired_caps['platformName'] = 'Android'
        desired_caps['platformVersion'] = '4.3'
        desired_caps['deviceName'] = 'Android Emulator'
        desired_caps['app'] = PATH(r'G:\workspace\src\common\testApp\testApp.apk')
        desired_caps['app-Package'] = 'com.android.test'
        desired_caps['app-Activity'] = '.test'

        driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
        time.sleep(2)

        return driver

执行下一个用例的时候,也需要使用 driver 的属性方法,可是不能再调用上面的函数了,因为调用还是会再次安装 app,
请问:如何在不安装 app 的情况下,获取 driver 的属性方法呢?

查询了下资料,说是将

desired_caps['app'] = PATH(r'G:\workspace\src\common\testApp\testApp.apk')
代码注释掉,不过我试了,注释掉上面这行后,依然报错:

#!/usr/bin/env python
# -*- encoding:UTF-8 -*-

from appium import webdriver
import time,os

class getDriverInfo():
    '''    获取driver属性信息的类     '''

    def androidDriverInfo(self):
        '''   定义获取driver属性信息的函数--安卓     '''
        # Returns abs path relative to this file and not cwd
        #PATH = lambda p: os.path.abspath(os.path.join(os.path.dirname(__file__), p))

        desired_caps = {}
        desired_caps['platformName'] = 'Android'
        desired_caps['platformVersion'] = '4.3'
        desired_caps['deviceName'] = 'Android Emulator'
        #desired_caps['app'] = PATH(r'G:\workspace\src\common\testApp\testApp.apk')
        desired_caps['app-Package'] = 'com.android.test'
        desired_caps['app-Activity'] = '.test'

        driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
        time.sleep(2)
        return driver


if __name__ == '__main__':
    driverInfo = getDriverInfo()
    app = driverInfo.androidDriverInfo()
    print app.contexts()

报错的信息如下:
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 164, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: u"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)"

请问大家是如何解决执行测试用例的时候,不要每次都重新安装 apk ,就可以获取 driver 属性信息的?

共收到 11 条回复 时间 点赞

不是很明白,但是你的意思是不要每次都重新安装 apk 吧?

匿名 #2 · 2014年09月26日

重连 Session 应该可以,不过,你这样后面每一个脚本都依赖于前面的脚本,相当于没有初始化了,真的好吗?

#1 楼 @lihuazhang 是的,执行下个用例,就不用再需要安装 app,直接使用 driver 属性信息

设置全局 driver,后面 case 都掉它!

#4 楼 @luis 关键是不知道如何获取这个全局的 driver 啊

在 阿里-sh-lihuazhang C-上海 - 西陆之风 @ 上海-L.T. 的帮助下,终于解决了这个问题

from appium import webdriver
from common.testCaseBase import desired_capabilities


class getDriverInfo():
    '''   获取driver属性信息的类     '''

    def androidDriverInfo(self):
        '''  定义获取driver属性信息的函数--安卓     '''

        desired_caps = desired_capabilities.get_desired_capabilities('WeChat_480.apk')

        driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
        print '\n for test',driver,'\n'
        return driver

    def iOSDriverInfo(self):
        '''  定义安装app函数-iOS     '''
        pass


if __name__ == '__main__':
    driverInfo = getDriverInfo()
    driver = driverInfo.androidDriverInfo()

如上代码调用的时候,安装 app,如果 app 已经安装了,在不想安装 app 的情况下, 可通过 dos 窗口,通过启动 appium 带上 --no-reset 即可避免执行用例的时候再次安装 app
--no-reset 参数解释 如下:
--no-reset Don't reset app state between sessions (IOS: don't
delete app plist files; Android: don't uninstall app
before new session)

如上解释可通过 dos 窗口执行 appium --no-reset -h 获得

原来的代码也可以使用,总之一句话,app 已安装,在不想每次执行用例都需要再次安装 app 的情况下,启动 appium 带上--no-reset 即可

from common.testCaseBase import desired_capabilities

#6 楼 @strayeagle 你这个模块是自己写的吗?
我根据例子里面的 import desired_capabilities 引入报没有这个模块

import unittest

from appium import webdriver
from appium.common.exceptions import NoSuchContextException
import desired_capabilities
    import desired_capabilities
ImportError: No module named desired_capabilities

@mongnet
desired_capabilities 类内容如下:

#!/usr/bin/env python
#-*- coding:UTF-8 -*-

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import os

# Returns abs path relative to this file and not cwd
PATH = lambda p: os.path.abspath(
    os.path.join(os.path.dirname(__file__), p)
)


def get_desired_capabilities(app):
    desired_caps = {
        'platformName': 'Android',
        'platformVersion': '4.3',
        'deviceName': 'Android Emulator',
        'appPackage' : 'com.xxx.phone',
        'appActivity' : 'com.xxx.ui.FlowPager',
        'app': PATH(r'../testApp/' + app)
    }

    return desired_caps


if __name__ =='__main__':
    app = get_desired_capabilities('testBase_2.1.apk')
    print app


谢谢 @strayeagle 的回复,我开始还以为这个模块是系统的了

11楼 已删除


客户端的勾选 no reset

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