python3 appium移动端自动化_移动端自动化测试解决方案(Appium + Python) -

对测试用例的编写我们采用Python + Behave的方案。

behave是一款行为驱动开发(BDD)的测试框架,配合python用以进行测试用例的编写。接下来我们开始安装behave。

安装Behave

利用pip3安装Appium-Python-Client,作用是让Appium支持Python的环境。

pip3 install Appium-Python-Client

安装Behave

pip3 install behave

查看Behave支持的语言

behave --lang-list

查看对应语言的关键字

behave --lang-help zh-CN

Translations for Chinese simplified / 简体中文

And: 而且<

Background: 背景

But: 但是<

Examples: 例子

Feature: 功能

Given: 假如<

Scenario: 场景

Scenario Outline: 场景大纲

Then: 那么<

When: 当<

Behave安装完毕之后我们开始编写测试脚本。

自动化测试运行方式一:通过behave编写测试用例并运行脚本

1、创建如下目录结构

├── app # 待测app

│ └── TestApp.app

└── features

├── calculate.feature # behave待测功能定义

├── environment.py # 环境配置

└── steps

└── step.py # 测试steps

2、测试求和功能

创建calculate.feature,输入如下内容

#language: zh-CN

功能: 求和

场景: 计算两个数相加

假如 第一个值输入 10

而且 第二个值输入 20

当 点击 求和按钮

那么 结果应该为30

3、配置环境

创建environment.py,输入如下内容,下列以真机配置为例

# -*- coding: utf-8 -*

import os

from appium import webdriver

def before_feature(context, feature):

app = '/Users/jackie-pc/Desktop/TestDemo/app/TestApp.app'

context.driver = webdriver.Remote(

command_executor='http://127.0.0.1:4723/wd/hub',

desired_capabilities={

'app': app,

'platformName': 'iOS',

'deviceName': '机型',

'platformVersion': '手机版本号',

'bundleId': 'App的bundleID',

'udid': '你的手机udid'

})

def after_feature(context, feature):

context.driver.quit()

Tip:

解释下里面的内容:app 是安装包的绝对路径,你可以打开命令行然后把你的包拖进去即可获取。udid可以通过itunes(音乐)链接你的真机进行获取。command_executor,即是你的Appium的服务器地址,你是通过Appium-Python-Client这句指令让Appium响应Python。

4、创建steps

创建step.py,根据测试用例编写脚本。

5、运行测试

behave

自动化测试运行方式二:直接编写测试Python脚本文件

上面是通过behave编写测试用例进行自动化测试的,如果你对测试用例要求度不高,你可以直接用IDE去编写Python测试脚本然后执行也可以。

附上一个简单的登录流程py测试脚本文件代码:

# -*- coding: utf-8 -*

import unittest

import os

from appium import webdriver

from time import sleep

class appiumSimpleTezt(unittest.TestCase):

def setUp(self):

app = os.path.abspath('/Users/legion/Desktop/AppiumTest/apps/CCB_Test.ipa')

self.driver = webdriver.Remote(

command_executor='http://127.0.0.1:4723/wd/hub',

desired_capabilities={

'app': app,

'platformName': 'iOS',

'platformVersion': '13.2',

'deviceName': 'iphone XR',

'bundleId': 'org.zzmetro.cn',

'udid': '00008020-001E35E12692002E'

}

)

def test_push_view(self):

sleep(2)

#next_view_button = self.driver.find_element_by_xpath("//XCUIElementTypeButton[@name='tabbar_我的']")

next_view_button = self.driver.find_element_by_accessibility_id("tabbar_我的")

next_view_button.click()

# next_view_button = self.driver.find_element_by_xpath("(//XCUIElementTypeButton[@name='TBUIAutoTest_Property_noLoginB'])[1]")

next_view_button = self.driver.find_element_by_name("loginButton")

next_view_button.click()

next_textField = self.driver.find_element_by_accessibility_id("TBUIAutoTest_Property_phoneTextFiled")

next_textField.set_value("15228856302")

sleep(2)

next_view_button = self.driver.find_element_by_accessibility_id("TBUIAutoTest_Property_verfiCodeB")

next_view_button.click()

sleep(2)

next_view_button1 = self.driver.find_element_by_accessibility_id("TBUIAutoTest_Property_agreeB")

next_view_button1.click()

sleep(2)

next_textView = self.driver.find_element_by_accessibility_id("TBUIAutoTest_Property_textView")

# //XCUIElementTypeTextView[@name="TBUIAutoTest_Property_textView"]

next_textView.set_value("1234")

def tearDown(self):

sleep(2)

# self.driver.quit()

if __name__ == '__main__':

suite = unittest.TestLoader().loadTestsFromTestCase(appiumSimpleTezt)

unittest.TextTestRunner(verbosity=2).run(suite)

特别说明:

上述代码中的

app = os.path.abspath('/Users/legion/Desktop/AppiumTest/apps/CCB_Test.ipa')

填写绝对路径,其中包是支持.app、.ipa文件类型。并且你的ipa文件上传至服务器后还能支持其下载地址。

小结

最佳的方案是用behave编写测试用例之后,然后针对测试用例中的场景用Python的IDE编写测试脚本。这里使用的IDE是PyCharm。

python元素定位

安卓:采用基本元素定位即可实现

driver.find_element_by_id("ddeeffv")

iOS:综合使用以下三种获取方式

self.driver.find_element_by_accessibility_id("TBUIAutoTest_Property_phoneTextFiled")

self.driver.find_element_by_name("("loginButton")"),

self.driver.find_element_by_xpath("//XCUIElementTypeButton[@name='tabbar_我的']")

python基本事件获取(iOS):

点击事件:

next_view_button = self.driver.find_element_by_name("loginButton")

next_view_button.click()

输入框事件:

next_textField = self.driver.find_element_by_accessibility_id("TBUIAutoTest_Property_phoneTextFiled")

next_textField.set_value("15228856302")

文本清空

.clear()

获取富文本属性

.get_attribute('value')

参考链接:

使用python+Behave编写测试用例

在这里非常感谢每一位愿意在简书进行分享的朋友!!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现移动端播放器的自动化测试需要使用到AppiumPython编程语言。以下是一些可能的测试场景: 1. 播放视频:在应用中选择一个视频并播放,检查视频是否正常播放,包括视频质量、声音质量等。 2. 暂停和继续播放:在视频播放过程中,暂停播放一段时间,然后继续播放,检查视频是否继续正常播放。 3. 调整音量:在视频播放过程中,调整音量大小,检查音量是否正常调整。 4. 全屏播放:在视频播放过程中,切换到全屏模式,检查视频是否正常全屏播放。 5. 切换视频分辨率:在视频播放过程中,切换不同的分辨率,检查视频是否正常播放。 6. 视频广告:在视频播放过程中,出现广告,检查广告是否正常播放。 7. 视频缓冲:在视频播放过程中,模拟网络延迟或低速网络,检查视频是否正常缓冲。 8. 视频播放列表:测试视频播放列表功能,检查是否可以正常播放多个视频。 通过使用Appium的API和Python编程语言,可以实现自动化测试脚本的编写和执行。可以使用Appium的UI Automator Viewer工具来查看应用程序的UI元素,并使用Appium的API来模拟用户操作。例如: ```python from appium import webdriver desired_caps = { 'platformName': 'Android', 'deviceName': 'device_name', 'appPackage': 'com.example.app', 'appActivity': 'MainActivity' } driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps) # 点击播放按钮 play_button = driver.find_element_by_id('com.example.app:id/play_button') play_button.click() # 等待视频播放 time.sleep(10) # 切换到全屏模式 fullscreen_button = driver.find_element_by_id('com.example.app:id/fullscreen_button') fullscreen_button.click() # 等待全屏模式 time.sleep(5) # 调整音量大小 volume_slider = driver.find_element_by_id('com.example.app:id/volume_slider') volume_slider.drag_to(0.5, 0, duration=1) # 暂停播放 pause_button = driver.find_element_by_id('com.example.app:id/pause_button') pause_button.click() # 等待暂停播放 time.sleep(2) # 继续播放 play_button.click() # 等待继续播放 time.sleep(2) driver.quit() ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值