短信案例
需求
在《短信》应用中,进入发送短信页面,在姓名和内容栏中,输入对应的数据,并点击发送。
包名界面名:com.android.mms/.ui.ConversationList
发送短信页面标识:resource-id,com.android.mms:id/action_compose_new
接收者标识:resource-id,com.android.mms:id/recipients_editor
内容标识:resource-id,com.android.mms:id/embedded_text_editor
发送按钮标识:content-desc,发送
数据
[18888888888, hello]
[13333333333, abc]
要求
使用 pytest框架 + po模式 +数据参数化
结构
-base
--base_action.py
--base_driver.py
-page
--page.py
--display_page.py
--more.py
--network.py
--setting_page.py
-scripts
--test_setting.py
代码
base_action.py代码
from selenium.webdriver.support.wait import WebDriverWait
class Base():
def __init__(self,driver):
self.driver = driver
def find_element(self,loc,time = 5 ,poll = 1):
ele = WebDriverWait(self.driver,timeout=time,poll_frequency=poll).until(lambda x:x.find_element(loc[0],loc[1]))
return ele
def find_elements(self,loc,time = 5 ,poll = 1):
ele = WebDriverWait(self.driver,timeout=time,poll_frequency=poll).until(lambda x:x.find_elements(loc[0],loc[1]))
return ele
def base_click(self,loc):
self.find_element(loc).click()
def base_send_keys(self,loc,value):
self.find_element(loc).send_keys(value)
base_driver.py代码
from appium import webdriver
def init_driver():
desired_caps = dict()
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '5.1'
desired_caps['deviceName'] = '192.168.56.101:5555'
desired_caps['appPackage'] = 'com.android.settings'
desired_caps['appActivity'] = '.Settings'
return webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
page.py代码
from page.display_page import Display
from page.more import More
from page.network import Network
from page.setting_page import Setting
class Page():
def __init__(self,driver):
self.driver = driver
def setting(self):
return Setting(self.driver)
def display(self):
return Display(self.driver)
def more(self):
return More(self.driver)
def network(self):
return Network(self.driver)
display_page.py代码
from base.base_action import Base
from selenium.webdriver.common.by import By
class Display(Base):
# driver.find_element_by_id("com.android.settings:id/search").click()
look_up_click = By.ID, "com.android.settings:id/search"
# self.driver.find_element_by_id("android:id/search_src_text").send_keys("hello")
send_keys_element = By.ID, "android:id/search_src_text"
def page_lookup(self):
self.base_click(self.look_up_click)
def page_send_keys(self):
self.base_send_keys(self.send_keys_element,"hello")
more.py代码
from base.base_action import Base
from selenium.webdriver.common.by import By
class More(Base):
# self.driver.find_element_by_xpath("//*[@text = '移动网络']").click()
mobile_network_click = By.XPATH, "//*[@text = '移动网络']"
# self.driver.find_element_by_xpath("//*[@text = '首选网络类型']").click()
def page_mobile_network(self):
# self.driver.find_element_by_xpath("//*[@text = '移动网络']").click()
self.base_click(self.mobile_network_click)
network.py代码
from base.base_action import Base
from selenium.webdriver.common.by import By
class Network(Base):
network_type_click = By.XPATH, "//*[@text = '首选网络类型']"
# self.driver.find_element_by_xpath("//*[@text = '3G']").click()
type_3G_click = By.XPATH, "//*[@text = '3G']"
# self.driver.find_element_by_xpath("//*[@text = '2G']").click()
type_2G_click = By.XPATH, "//*[@text = '2G']"
def page_network_type(self):
# self.driver.find_element_by_xpath("//*[@text = '首选网络类型']").click()
self.base_click(self.network_type_click)
def page_type_3G(self):
# self.driver.find_element_by_xpath("//*[@text = '3G']").click()
self.base_click(self.type_3G_click)
def page_type_2G(self):
# self.driver.find_element_by_xpath("//*[@text = '2G']").click()
self.base_click(self.type_2G_click)
setting_page.py代码
from base.base_action import Base
from selenium.webdriver.common.by import By
class Setting(Base):
# driver.find_element_by_xpath(BY.XPATH,"//*[@text = '显示']").click()
display_element = By.XPATH, "//*[@text = '显示']"
# self.driver.find_element_by_xpath("//*[@text = '更多']").click()
more_click = By.XPATH, "//*[@text = '更多']"
def page_display(self):
self.base_click(self.display_element)
def page_more(self):
# self.driver.find_element_by_xpath("//*[@text = '更多']").click()
self.base_click(self.more_click)
test_setting.py代码
import os, sys
sys.path.append(os.getcwd())
from page.page import Page
# from page.network import Network
# from page.setting_page import Setting
from base.base_driver import init_driver
# from page.display_page import Display
# from page.more import More
"""
更多-移动网络-首选网络类型-点击2g
更多-移动网络-首选网络类型-点击3g
显示-搜索按钮-输入hello-点击返回
"""
class TestSetting:
def setup(self):
self.driver = init_driver()
# self.display = Display(self.driver)
# self.more = More(self.driver)
# self.setting = Setting(self.driver)
# self.network = Network(self.driver)
self.page = Page(self.driver)
def teardown(self):
self.driver.quit()
def test_setting01(self):
self.page.setting().page_more()
self.page.more().page_mobile_network()
self.page.network().page_network_type()
self.page.network().page_type_3G()
def test_setting02(self):
self.page.setting().page_more()
self.page.more().page_mobile_network()
self.page.network().page_network_type()
self.page.network().page_type_2G()
def test_setting03(self):
self.page.setting().page_display()
self.page.display().page_lookup()
self.page.display().page_send_keys()
通讯录案例
需求
在《通讯录》应用中,进入添加联系人页面,在姓名和电话栏中,输入对应的数据。
包名界面名:com.android.contacts/.activities.PeopleActivity
添加联系人页面标识:resource-id,com.android.contacts:id/floating_action_button
姓名标识:text,姓名
电话标识:text,电话
因为appium每次启动应用时都会"重置应用",才会出现本地保存,若不想重置,可直接在启动参数加一行代码
desired_caps['noReset'] = True 即可
数据
[zhangsan, 18888888888]
[lisi, 13333333333]
[wangwu, 17777777777]
要求
使用 pytest框架 + po模式 + 数据参数化。
结构
-base
--base_action.py
--base_driver.py
-page
--page.py
--page_contact_list.py
--page_new_contact.py
-scripts
--test_add_contact.py
代码
base_action.py代码
from selenium.webdriver.support.wait import WebDriverWait
class BaseAction:
# 初始化driver
def __init__(self,driver):
self.driver = driver
# 创建基础查找元素的方法
def find_ele(self,loc,time = 5, poll = 1):
ele = WebDriverWait(self.driver,timeout = time,poll_frequency=poll).until(lambda x:x.find_element(loc[0],loc[1]))
return ele
# 创建基础的点击动作
def base_click(self,loc):
self.find_ele(loc).click()
# 创建基础的输入文本方法
def base_send_keys(self,loc,text):
self.find_ele(loc).send_keys(text)
base_driver.py代码
from appium import webdriver
# 将driver用方法封装
def init_driver():
desired_caps = dict()
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '5.1'
desired_caps['deviceName'] = '192.168.56.101:5555'
desired_caps['appPackage'] = 'com.android.contacts'
desired_caps['appActivity'] = '.activities.PeopleActivity'
# 不重置程序
desired_caps['noReset'] = True
return webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
page.py代码
from page.page_contact_list import PageContactList
from page.page_new_contact import PageNewContact
class Page():
def __init__(self,driver):
self.driver = driver
# 创建PageContactList对象入口
def creat_contect_list(self):
return PageContactList(self.driver)
# 创建PageNewContact对象入口
def creat_new_contect(self):
return PageNewContact(self.driver)
page_contact_list.py代码
from selenium.webdriver.common.by import By
from base.base_action import BaseAction
class PageContactList(BaseAction):
# 点击新增联系人元素
ele_add_contact = By.ID,"com.android.contacts:id/floating_action_button"
# 点击新增联系人
def page_add_click(self):
self.base_click(self.ele_add_contact)
page_new_contact.py代码
from selenium.webdriver.common.by import By
from base.base_action import BaseAction
class PageNewContact(BaseAction):
# 定位输入姓名元素
ele_send_out_name = By.XPATH,"//*[@text = '姓名']"
# 定位输入电话元素
ele_send_out_phone = By.XPATH,"//*[@text = '电话']"
# 定位返回元素
ele_click_back = By.CLASS_NAME,"android.widget.ImageButton"
# 页面输入姓名操作
def page_send_out_name(self,text):
self.base_send_keys(self.ele_send_out_name,text)
# 页面输入手机号码操作
def page_send_out_phone(self,text):
self.base_send_keys(self.ele_send_out_phone,text)
# 页面点击返回操作
def page_click_back(self):
self.base_click(self.ele_click_back)
test_add_contact.py代码
import os, sys
sys.path.append(os.getcwd())
import time
import pytest
from base.base_driver import init_driver
from page.page import Page
class TestCase():
def setup(self):
self.driver = init_driver()
self.page = Page(self.driver)
def teardown(self):
time.sleep(3)
self.driver.quit()
# 参数化+业务流程
@pytest.mark.parametrize(("name","phone"),[("zhangsan","18888888888"),("lisi", "13333333333"),("wangwu","17777777777")])
def test_add_contact(self,name,phone):
self.page.creat_contect_list().page_add_click()
self.page.creat_new_contect().page_send_out_name(name)
self.page.creat_new_contect().page_send_out_phone(phone)
self.page.creat_new_contect().page_click_back()
作为一个软件测试的过来人,我想尽自己最大的努力,帮助每一个伙伴都能顺利找到工作。所以我整理了下面这份资源,现在免费分享给大家,有需要的小伙伴可以关注【公众号:开心螺蛳粉】自提!
软件测试面试文档
我们学习必然是为了找到高薪的工作,下面这些面试题是来自阿里、腾讯、字节等一线互联网大厂最新的面试资料,并且有字节大佬给出了权威的解答,刷完这一套面试资料相信大家都能找到满意的工作。
行动吧,在路上总比一直观望的要好,未来的你肯定会感谢现在拼搏的自己!如果想学习提升找不到资料,没人答疑解惑时,请及时加入群:1150305204,里面有各种测试开发资料和技术可以一起交流哦。