案例分享,Appium+Python实现APP启动页跳转到首页!

下面以 MSN news 为例,实现启动APP后跳转到首页的功能,包含使用list进行元素定位、try except else 进行是否首次启动APP判断,logging 进行日志记录等功能。

 

 

 

 

Python资源共享群:484031800

一、场景:

1.启动APP后连续跳过welcom、interest 、what’s new页面到首页

2.判断是否是首次启动,如果首次启动通过出现welcom页面,如果不是首次启动则直接进入interest页面

3.使用logging模块记录日志

Gif图片文件:

 

 

 

 

 

二、实践

2.1、启动APP后连续跳过welcom、interest 、what’s new页面到首页

welcom页面

 

 

 

 

 

1.首先启动Appium进行session回话

2.APP启动到welcom页面,点击not now按钮,跳转到下一个页面

分析:这里我使用list元素进行定位,通过观察可以知道class name 和sign in name名称相同,如果直接使用classname那么就会定会到当前页面首个class name元素,导致定位失败。

list定位获取一组class名称,通过数组下标进行区分

a=driver.find_element_by_id()

a[1].click()

3.代码:

skipwel=driver.find_elements_by_class_name('android.widget.Button')

skipwel[1].click()

2.2、判断是否是首次启动,如果首次启动通过出现welcom页面,如果不是首次启动则直接进入interest页面

首次启动app时,会弹出welcome页面,但是第二次启动时该页面就会消失,出现interest页面那么对于这种判断我们该如何处理呢

这里我们使用try except 语句进行判断

try except 语法:

 

 

 

 

 

工作原理:

如果try语句发生异常,则执行except 匹配名称后语句

如果try语句未发生异常,执行else后面语句

如果try语句发生异常,except语句后内容没有匹配成功,异常将被递交到上层的try

处理思路:

定位interest页面,如果定位失败,证明出现的是welcome,执行welcome语句;否则执行interest页面语句

代码:

def welcome():

logging.info('skip welcome')

skipwel=driver.find_elements_by_class_name('android.widget.Button')

skipwel[1].click()

try:

driver.find_elements_by_class_name('android.widget.Button')

except NoSuchElementException:

welcome()

else:

skipinterest=driver.find_elements_by_class_name('android.widget.Button')

skipinterest[0].click()

interets 页面

原理同welcom 页面

skipinterest=driver.find_elements_by_class_name('android.widget.Button')

skipinterest[0].click()

what‘s new page

原理同welcom 页面

skipwhatnew=driver.find_elements_by_class_name('android.widget.Button')

skipwhatnew[1].click()

2.3、使用logging 模块记录日志

日志是用来帮我们定位问题,通过日志可以更准准确的确定问题所在,我们可以设定日志级别、日志的输出格式等

场景:

在启动APP时,输出start app 日志

导航页跳转到首页过程,输出执行的页面为interest 页面还是welcome 页面

2.3.1、单个py文件日志

在单个py文件里日志输出形式如下:

1.日志流程

首先,导入日志模块

然后,basicconfig 方法创建记录器,为记录做基本配置。

日志级别:level

日志输出路径:Filename

日志输出格式:Format

最后,在需要添加日志的地方输出日志即可,logging.info(’start app‘)

代码实现

import logging

logging.basicConfig(level=logging.INFO,filename='456.log',format='%(asctime)s %(filename)s[line:%(lineno)d]%(levelname)s%(message)s')

#判断是否首次启动App,跳过导航页到首页

def welcome():

logging.info('skip welcome')

skipwel=driver.find_elements_by_class_name('android.widget.Button')

skipwel[1].click()

try:

driver.find_elements_by_class_name('android.widget.Button')

except NoSuchElementException:

logging.info('run welcome')

welcome()

else:

logging.info('run interest')

skipinterest=driver.find_elements_by_class_name('android.widget.Button')

skipinterest[0].click()

3.运行

在代码中456.log中查看日志输出信息

 

 

 

 

 

2.3.2 日志模块

上面讲述的是单个功能日志的流程,整个APP测试下来有很多功能都需要用到日志,如果每个功能都重复编写,会造成代码冗余

所以,下面我们把日志的配置参数抽离出来,需要时直接调用即可

1.首先建立log.conf文件,存储配置参数等信息

log.conf文件代码:

[loggers]

keys=root,infoLogger

[logger_root]

level=DEBUG

handlers=consoleHander,fileHandler

[logger_infoLogger]

handlers=consoleHander,fileHandler

qualname=infoLogger

propagat=0

[handlers]

keys=consoleHander,fileHandler

[handler_consoleHander]

class=StreamHandler

level=INFO

formatter=form02

args=(sys.stdout,)

[handler_fileHandler]

class=FileHandler

level=INFO

formatter=form01

args=('456.log','a')

[formatters]

keys=form01,form02

2.在需要引用log的py文件中引用log.conf文件

 

 

 

 

 

将上文中如下代码删除

logging.basicConfig(level=logging.INFO,filename='456.log',

format='%(asctime)s %(filename)s[line:%(lineno)d]%(levelname)s%(message)s')

更换为:

CON_LOG='log.conf'

logging.config.fileConfig(CON_LOG)

logging=logging.getLogger()

三、报错提示

总结运行过程中报错提示及解决方法

1.Eclipse运行 启动app 代码,提示launching new configuration

 

 

 

 

 

Eclipse

解决方法:

Project -> Properties -> Run/Debug Settings:

1. select "Launching New_configuration5"

2. Delete

2.ImportError: cannot import name 'webdriver'

 

 

 

 

 

解决方法:

from selenium import webdriver

3.TypeError: 'WebElement' object does not support indexing

 

 

 

 

 

解决:把find_element 改为find_elements?

4.IndexError: list index out of range

 

 

 

 

 

解决:

有可能是数组越界,里面数值从0开始标记

再有可能list是一个空的,没有一个元素

进行list[0]就会出现该错误

5.module 'logging' has no attribute 'basicConfig'

 

 

 

 

 

解决:

自己就命名了一个logging.py的文件,在代码中import的是自己新建的这个,找不到basicConfig,文件名改成logging_test.py后,正常运行。

6.int' object is not callable

 

 

 

 

 

解决:发现代码中写的是logging.DEBUG,应该为logging.debug()

<think>嗯,用户问的是在使用AppiumPython和pytest框架进行App自动化测试时,如果有两款App,应该怎么搭建框架比较合适。我需要先理解用户的需求。用户可能是在同时测试两个不同的应用,或者是一个主应用和一个辅助应用,比如电商和支付App。这种情况下,框架的设计需要考虑如何管理不同App的配置、驱动以及测试用例的组织。 首先,我应该回想一下常见的Appium框架结构。通常会有配置文件管理不同设备或App的参数,比如capabilities。如果有两个App,可能需要分别配置它们的desired capabilities,比如不同的appPackage和appActivity。然后,考虑测试用例的组织,是否要分开目录,或者用不同的标记来区分。还有驱动实例的管理,比如每个App是否需要独立的driver,或者在测试中切换driver。 接下来,pytest框架的特点是利用fixture来管理setup和teardown,所以可能需要为每个App创建不同的fixture,这样在测试用例中可以根据需要调用对应的fixture。同时,配置管理可以用配置文件或类来区分不同App的参数,比如用YAML或JSON文件存储两个App的信息,然后在运行时动态加载。 另外,用户可能需要跨App的操作,比如从一个应用跳转到另一个应用,这时候需要处理上下文切换或者多个driver实例。Appium本身支持多个driver,但需要注意管理它们的生命周期,避免冲突。此外,测试数据的管理也可能需要区分,不同App的测试数据应该分开存放,避免混淆。 还有日志和报告部分,需要确保两个App的测试结果能够正确记录和区分。pytest的插件如pytest-html可以生成报告,但需要确认是否支持多App的展示,或者在日志中添加App的标识。 可能用户没有明确提到的点是是否需要并行测试,或者两个App是否有依赖关系。如果并行测试,可能需要更复杂的配置,比如同时启动两个模拟器或真机,但通常Appium在一个会话中只能控制一个设备,所以可能需要多线程或分布式执行。不过用户的问题可能更倾向于框架结构而不是并行执行,所以暂时先不考虑这点。 总结下来,框架搭建的关键点包括:配置管理、驱动管理、测试用例组织、fixture设计、数据管理和报告生成。需要将这些部分模块化,使得两个App的配置和测试能够互不干扰,同时保持代码的可维护性和扩展性。</think>在使用Appium+Python+pytest框架测试双App时,建议采用以下分层架构设计(附代码示例): --- ### 一、项目结构设计 ```bash project/ ├── apps/ # 安装包目录 │ ├── app1.apk │ └── app2.apk ├── configs/ # 配置目录 │ ├── app1_config.yaml │ ├── app2_config.yaml │ └── pytest.ini ├── drivers/ # 驱动管理 │ ├── app1_driver.py │ └── app2_driver.py ├── page_objects/ # 页面对象 │ ├── app1/ │ │ ├── login_page.py │ │ └── home_page.py │ └── app2/ │ ├── payment_page.py │ └── order_page.py ├── test_cases/ # 测试用例 │ ├── test_app1/ │ │ └── test_login.py │ └── test_app2/ │ └── test_payment.py ├── conftest.py # pytest全局fixture └── utils/ # 工具类 ├── common.py └── adb_helper.py ``` ### 二、核心实现方案 1. **配置管理** - 使用YAML区分App配置 ```yaml # app1_config.yaml platformName: Android platformVersion: 13 deviceName: emulator-5554 appPackage: com.example.app1 appActivity: .MainActivity noReset: false ``` 2. **驱动管理** - 创建独立Driver实例 ```python # drivers/app1_driver.py from appium import webdriver def init_app1_driver(config): capabilities = config['app1'] driver = webdriver.Remote('http://localhost:4723/wd/hub', capabilities) driver.implicitly_wait(10) return driver ``` 3. **pytest Fixture** - 为每个App创建独立fixture ```python # conftest.py import pytest from drivers import app1_driver, app2_driver @pytest.fixture(scope='session') def app1(config): driver = app1_driver.init_app1_driver(config) yield driver driver.quit() @pytest.fixture(scope='session') def app2(config): driver = app2_driver.init_app2_driver(config) yield driver driver.quit() ``` 4. **跨App测试示例** - 使用标记控制测试范围 ```python # test_cases/test_cross_app.py import pytest @pytest.mark.app1 @pytest.mark.app2 def test_payment_flow(app1, app2): # 在App1下单 app1.find_element('id1').click() # 切换到App2支付 app2.activate_app('com.example.app2') app2.find_element('pay_btn').click() # 验证支付结果 assert app2.find_element('success_tip').text == '支付成功' ``` ### 三、最佳实践建议 1. **上下文管理策略**: - 使用`driver.contexts`处理WebView切换 - 通过`driver.start_activity()`显式启动目标Activity - 跨App操作时注意添加显式等待 2. **并行优化方案**: ```ini # pytest.ini [pytest] addopts = -n auto # 使用pytest-xdist并行执行 ``` 3. **异常处理增强**: ```python # utils/common.py def safe_click(element): try: element.click() except WebDriverException: element.tap() ``` ### 四、扩展方案(针对复杂场景) 1. **多设备管理**: ```python # 启动两个设备 driver1 = webdriver.Remote('http://localhost:4723', caps1) driver2 = webdriver.Remote('http://localhost:4724', caps2) ``` 2. **智能等待策略**: ```python def wait_multiple_contexts(driver, timeout=30): WebDriverWait(driver, timeout).until( lambda d: len(d.contexts) > 1 ) ``` 这种架构设计的主要优势: 1. 配置隔离:通过独立配置文件避免参数冲突 2. 职责分离:不同App的页面对象和测试用例物理隔离 3. 灵活组合:通过pytest标记实现App测试/跨App测试自由组合 4. 扩展性强:新增App只需复制模式化结构即可 建议根据实际业务场景选择是否需要保持双Driver实例,对于强关联的App组合(如电商+支付),推荐保持双实例共存;对于独立业务系统,采用动态初始化更节省资源。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值