软件测试之移动端自动化测试


# https://github.com/appium/appium/blob/master/docs/en/writing-running-appium/caps.md   查看详细参数意义
 
from appium.webdriver.common.touch_action import TouchAction
from appium import webdriver
import time
 
desired_caps = {}                                       #配置参数的字典
desired_caps['platformName'] = 'Android'                #平台名称,iOS、Android、 FirefoxOS,选择真机/模拟器对应的就好
desired_caps['platformVersion'] = '5.1.1'               #平台版本
desired_caps['deviceName'] = '真机'                     #设备名称填写任意内容(使用的移动设备或模拟器的种类)
# desired_caps['appPackage'] = 'com.dyhoa.school'       #要运行的Android应用程序的Java包
# desired_caps['appActivity'] = '.LauncherNewActivity'  #要运行的Android应用程序的主入口
desired_caps['appPackage'] = 'com.android.settings'     #要运行的Android应用程序的Java包
desired_caps['appActivity'] = '.Settings'               #要运行的Android应用程序的主入口
# desired_caps['udid'] = '8c9abb78'                     #连接的物理设备的唯一设备标识符,可通过adb devices查看(非必选)
desired_caps['unicodeKeyboard'] = True                  #设置启动编码
# desired_caps['resetKeyboard'] = False                 #重置键盘
desired_caps['noReset'] = 'True'                        #默认为False,当设置为true时不作为首次进入(在此会话之前,请勿重置应用程序状态)
 
driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub",desired_caps)
print("start")
time.sleep(4)
# 登录
# driver.find_element_by_id("com.dyhoa.school:id/image_home_message").click()
# driver.find_element_by_xpath("//android.widget.TextView[@text='密码登录 >']").click()
# login_pho = driver.find_element_by_xpath("//android.widget.EditText[@text='输入手机号']")
# login_pho = driver.find_element_by_xpath("//*[contains(@text,'输入手机号')]")  #方法2  contains模糊查找
# login_pho.send_keys("18810762963")
# login_pwd = driver.find_element_by_xpath("//android.widget.EditText[@text='输入密码']")
# # login_pwd = driver.find_element_by_id("com.dyhoa.school:id/login_pwd")
# login_pwd.send_keys("1111")
# driver.find_element_by_id("com.dyhoa.school:id/btn_login").click()
 
# driver.get_screenshot_as_file('pay.png')    #手机截屏
 
# driver.set_network_connection(6)    #0为什么都不开,1为飞行模式,2为wifi,4为移动网络,6为wifi与网络
 
# for x in range(3):    #循环步数,点击三次,keyevent(25)
#     driver.keyevent(27)
 
# print(driver.device_time)   #获取手机系统时间
 
# driver.background_app(10)   #放置后台启动 时间一到 会再起显示
 
# size  = driver.get_window_size()      #获取屏幕分辨率
 
# driver.find_element_by_xpath("//android.view.View[@text'设置']").click()    #通过class与text 定位
# driver.find_element_by_xpath("//*[contains(@text'设置')]").click()      #通过text内容模糊查找
# driver.find_element_by_xpath("//*[@resource-id='com.android.settings:id/search']").click()    #通过id键值查找
 
#上下滑动
# end = driver.find_element_by_xpath("//*[contains(@text,'蓝牙')]")
# start = driver.find_element_by_xpath("//*[contains(@text,'游戏防沉迷')]")
# driver.drag_and_drop(start,end)     #上下滑动
 
#获取xy点
# locat = driver.find_element_by_id("com.android.settings:id/relative").location
 
# 点击通知内容
# driver.open_notifications()
# driver.find_element_by_id("android:id/status_bar_latest_event_content").click()
 
# print('网络为',driver.network_connection)        #获取当前手机网络
 
# 通过元素定位方式敲击屏幕
# TouchAction(driver).tap(driver.find_element_by_xpath("//*[contains(@text,'蓝牙')]")).perform()
 
# 通过xy点定位方式敲击屏幕
# TouchAction(driver).tap(x=0,y=943).perform()
 
# 通过元素定位方式    手指按压后离开,轻触
# TouchAction(driver).press(driver.find_element_by_xpath("//*[contains(@text,'蓝牙')]")).release().perform()
 
# 通过xy点定位方式    手指按压后离开,轻触
# TouchAction(driver).press(x=0,y=944).release().perform()
 
# 通过元素定位方式    手指长按
# TouchAction(driver).long_press(driver.find_element_by_xpath("//*[contains(@text,'蓝牙')]"),3000).release().perform()
 
# 通过xy点定位方式    手指长按
# TouchAction(driver).long_press(x=0,y=944,duration=3000).release().perform()
 
 
end = driver.find_element_by_xpath("//*[contains(@text,'WLAN')]")
start = driver.find_element_by_xpath("//*[contains(@text,'存储')]")
driver.drag_and_drop(start,end)     #上下滑动
TouchAction(driver).tap(driver.find_element_by_xpath("//*[contains(@text,'安全')]")).perform()
TouchAction(driver).tap(driver.find_element_by_xpath("//*[contains(@text,'屏幕锁定方式')]")).perform()
edit = driver.find_element_by_id("com.android.settings:id/password_entry")
edit.send_keys("123456")
time.sleep(1)
TouchAction(driver).tap(driver.find_element_by_xpath("//*[contains(@text,'下一步')]")).perform()
time.sleep(1)
TouchAction(driver).tap(driver.find_element_by_xpath("//*[contains(@text,'图案')]")).perform()
# TouchAction(driver).press(x=178,y=640).wait(1000).move_to(x=536,y=640).release().perform()
TouchAction(driver).press(x=244,y=967).wait(1000).move_to(x=479,y=0).wait(1000).move_to(x=0,y=475).wait(1000).move_to(x=-479,y=474).release().perform()
 
print("end")
 

测试模拟器通讯录并添加联系人,生成测试报告。
import time
from appium import webdriver
from appium.webdriver.common.touch_action import TouchAction
from selenium.webdriver.support.wait import WebDriverWait

class Test_Utils():
    def setup(self):
        desired_caps = {}  # 配置参数的字典
        desired_caps['platformName'] = 'Android'  # 平台名称,iOS、Android、 FirefoxOS,选择真机/模拟器对应的就好
        desired_caps['platformVersion'] = '5.1.1'  # 平台版本
        desired_caps['deviceName'] = '模拟器'  # 设备名称填写任意内容(使用的移动设备或模拟器的种类)
        desired_caps['appPackage'] = 'com.android.contacts'  # 要运行的Android应用程序的Java包
        desired_caps['appActivity'] = '.activities.PeopleActivity'  # 要运行的Android应用程序的主入口
        desired_caps['unicodeKeyboard'] = True  # 设置启动编码
        desired_caps['resetKeyboard'] = False  # 重置键盘
        # desired_caps['noReset'] = 'False'                          #默认为False,当设置为true时不作为首次进入(在此会话之前,请勿重置应用程序状态)
        self.driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", desired_caps)

    def teardown(self):
        print("end结束")

    def waitXpath(self, xpath):
        return WebDriverWait(self.driver, 5, 0.5).until(lambda x: x.find_element_by_xpath(xpath))

    def waitId(self, id):
            return WebDriverWait(self.driver, 5, 0.5).until(lambda x: x.find_element_by_id(id))

    def test_001(self):
        self.waitId("com.android.contacts:id/floating_action_button").click()
        self.waitXpath("//*[contains(@text,'本地保存')]").click()
        self.driver.find_element_by_xpath("//*[contains(@text,'姓名')]").send_keys("杨勇勇")
        self.driver.find_element_by_xpath("//*[contains(@text,'姓名拼音')]").send_keys("yyy")
        self.driver.find_element_by_xpath("//*[contains(@text,'昵称')]").send_keys("勇勇")
        self.driver.find_element_by_xpath("//*[contains(@text,'电话')]").send_keys("18810762963")
        time.sleep(2)
        self.driver.drag_and_drop(self.driver.find_element_by_xpath("//*[contains(@text,'电子邮件')]"),
        self.driver.find_element_by_xpath("//*[contains(@text,'杨勇勇')]"))  # 上下滑动
        time.sleep(2)
        self.driver.find_element_by_xpath("//*[contains(@text,'电子邮件')]").send_keys("@18810762963.com")
        self.driver.find_element_by_xpath("//*[contains(@text,'地址')]").send_keys("北京天安门")
        self.driver.find_element_by_xpath("//*[contains(@text,'公司')]").send_keys("故宫")
        TouchAction(self.driver).tap(x=79, y=168).perform()

    def test_002(self):
        print("测试2")

    def test_003(self):
        print("测试3")

# 在当前类中的Terminal中执行:pytest -s 类名.py --html=./测试报告名.html
# pytest -s BaseUtils.py --html=./123.html
————————————————
 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值