项目需求
用appium写了一个自动化脚本,用来测试手机上的文件管理器的刷新、与添加书签功能是否正常
项目架构图
代码实现
base文件夹
base_action.py
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
class BaseAction:
def __init__(self, driver):
self.driver = driver
def click(self,loc):
ele = self.find_element(loc)
ele.click()
def input_text(self,loc,text):
ele = self.find_element(loc)
ele.send_keys(text)
def find_elements(self, loc, timeout=5.0, poll=1.0):
return WebDriverWait(self.driver, timeout, poll)
.until(lambda x: x.find_elements(loc[0], loc[1]))
def find_element(self, loc, timeout=5.0, poll=1.0):
return WebDriverWait(self.driver, timeout, poll)
.until(lambda x: x.find_element(loc[0], loc[1]))
def screenshot(self,filename):
self.driver.get_screenshot_as_file("./screen/"+filename+".png")
# 模拟滑动手机屏幕,一次滑一页
def scroll_page_one_time(self, direction='down'):
window_size = self.driver.get_window_size()
window_height = window_size['height']
window_width = window_size['width']
up_y = window_height*0.25
down_y = up_y*3
left_x = window_width*0.25
right_x = left_x * 3
center_x = window_width * 0.5
center_y = window_height * 0.5
if direction == 'down':
# 由下向上滑一页
self.driver.swipe(center_x,down_y,center_x,up_y)
elif direction == 'up':
# 由上向下滑一页
self.driver.swipe(center_x,up_y,center_x,down_y)
elif direction == 'left':
# 由左向右滑一页
self.driver.swipe(left_x,center_y,right_x,center_y)
elif direction == 'right':
# 由右向左滑一页
self.driver.swipe(right_x,center_y,left_x,center_y)
else:
raise Exception('请输入正确的direction参数 down、up、left、right')
base_driver.py
import os,sys
sys.path.append(os.getcwd())
from appium import webdriver
def init_driver():
# server 启动参数
desired_caps = {}
# 设备信息
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '5.1'
desired_caps['deviceName'] = '192.168.56.101:5555'
# app的信息
desired_caps['appPackage'] = 'com.cyanogenmod.filemanager'
desired_caps['appActivity'] = '.activities.NavigationActivity'
# 解决输入中文
desired_caps['unicodeKeyboard'] = True
desired_caps['resetKeyboard'] = True
desired_caps['noReset'] = True # 保留软件数据,否则每次一开始都要有一些权限问题,会报错的
# 声明我们的driver对象
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
return driver
page文件夹
file_page.py
import os, sys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
sys.path.append(os.getcwd())
from base.base_action import BaseAction
class FilePage(BaseAction):
# 菜单
menu_btn = By.ID, 'com.cyanogenmod.filemanager:id/ab_actions'
# 刷新
refresh_btn = By.XPATH, "//*[contains(@text,'刷新')]"
# 添加到书签
book_mark_btn = By.XPATH, "//*[contains(@text,'添加到书签')]"
# 添加到快捷方式
short_cut_btn = By.XPATH, "//*[contains(@text,'添加快捷方式')]"
# set as home
set_as_home_btn = By.XPATH, "//*[contains(@text,'Set as home')]"
# 侧边栏
side_menu_btn = By.ID, 'android:id/home'
# 文件列表的标题
dir_list_btn = By.ID, 'com.cyanogenmod.filemanager:id/navigation_view_item_name'
# 目录imageview的特征
path_name = By.ID, 'com.cyanogenmod.filemanager:id/breadcrumb_item'
# 侧边栏书签的特征
side_menu_book_mark = By.ID, 'com.cyanogenmod.filemanager:id/bookmarks_item_name'
def __init__(self, driver):
BaseAction.__init__(self, driver)
# 点击菜单栏
def menu_click(self):
self.click(self.menu_btn)
# 点击刷新
def refresh_click(self):
self.click(self.refresh_btn)
# 点击添加到书签
def mark_book_click(self):
self.click(self.book_mark_btn)
# 点击添加到快捷方式
def short_cut_click(self):
self.click(self.short_cut_btn)
# 点击set_as_home
def set_as_home_btn_click(self):
self.click(self.set_as_home_btn)
# 点击侧边栏
def side_menu_click(self):
self.click(self.side_menu_btn)
# 获取当前屏幕目录列表中,第一个目录的名字
def get_first_dir_name(self):
dir_ele= self.find_elements(self.dir_list_btn)
return dir_ele[0].text
# 获取目录的最后一个名字
def get_last_path_name(self):
path_ele = self.find_elements(self.path_name)
return path_ele[-1].text
# 判断书签是否添加成功
def get_side_menu_book_marks(self):
book_marks = self.find_elements(self.side_menu_book_mark)
book_mark_list = list()
for i in book_marks:
book_mark_list.append(i.text)
return book_mark_list
scripts文件夹
test_file.py
import os, sys
sys.path.append(os.getcwd())
from base.base_driver import init_driver
from page.file_page import FilePage
class Test_File:
def setup(self):
self.driver = init_driver()
self.filepage = FilePage(self.driver)
# 测试刷新功能是否正常
def test_refresh(self):
# 先获取当前屏幕目录列表中,第一个目录的名字
name = self.filepage.get_first_dir_name()
# 模拟屏幕中的下滑操作
self.filepage.scroll_page_one_time('down')
# 点击菜单栏中的刷新
self.filepage.menu_click()
self.filepage.refresh_click()
# 再次获取屏幕目录列表中,第一个目录的名字,比较两次名字是否相同
# 如果相同那么就证明刷新可用
name2 = self.filepage.get_first_dir_name()
print(name)
print(name2)
assert name == name2
# 测试添加书签功能是否正常
# 思路
def test_book_mark(self):
# 获取path的最后一个name
name = self.filepage.get_last_path_name()
# 点击菜单
self.filepage.menu_click()
# 点击添加到书签
self.filepage.mark_book_click()
# 点击侧边栏
self.filepage.side_menu_click()
# 判断书签是否添加成功
assert name in self.filepage.get_side_menu_book_marks()
将项目上传到github
1、进入到项目文件夹,输入 git init 命令 (让项目里有个git管家)
2、git add .让项目中所有文件收到git管理
3、git commit - m 'first commit' 提交到git
4、git remote add origin https://github.com/sunt998/filemanager.git让本地的filemanager项目与github上面的filemanager仓库建立连接
5、git push -u origin master将本地的filemanager项目推送到远程的github的filemanager仓库
如果不想将report文件夹和.idea上传到github仓库上去,只需在项目里面添加一个.gitignore文件,文件末尾添加:
注意.gitignore文件,在创建github仓库时,勾选下图会自动生成: