App端自动化测试----PO模型

1.basepage包

publicmethod.py模块, 存放公共方法

from appium.webdriver.common.touch_action import TouchAction
class PublicMethod:
    def __init__(self,driver):
        self.driver = driver
    #元素定位
    def locator(self,*loc):
        return self.driver.find_element(*loc)
    #清空
    def clear(self,*loc):
        self.locator(*loc).clear()
    #输入
    def input(self,test,*loc):
        self.locator(*loc).send_keys(test)
    #点击
    def click(self,*loc):
        self.locator(*loc).click()
    #点击坐标轴
    def click_axis(self,n,m):
        TouchAction(self.driver).tap(x =n,y = m).perform()
    #滑动(上下左右滑动)
    def swipe(self,start_x,start_y,end_x,end_y,duration=0):
        #获取屏幕的尺寸
        window_size = self.driver.get_window_size()
        x = window_size["width"]
        y = window_size["height"]
        self.driver.swipe(start_x=x*start_x,
                          start_y=y*start_y,
                          end_x= x*end_x,
                          end_y=y*end_y,
                          duration=duration)

2.pages包

(1) pageone.py

from daywork.app端自动化测试.夜神模拟器七猫.PO模型.basepage.publicmethod import PublicMethod
from appium.webdriver.common.mobileby import MobileBy

class PageOne(PublicMethod):
    def __init__(self,driver):
        PublicMethod.__init__(self,driver)
    def click_girls(self):
        self.click(MobileBy.XPATH,"/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.support.v4.view.ViewPager/android.widget.RelativeLayout/android.widget.LinearLayout/android.widget.LinearLayout/android.widget.LinearLayout/android.widget.HorizontalScrollView/android.widget.LinearLayout/android.widget.TextView[2]")
    def click_search(self):
        self.click(MobileBy.ID,"com.kmxs.reader:id/book_store_strip_title_search")

(2) pagetwo.py模块

from daywork.app端自动化测试.夜神模拟器七猫.PO模型.basepage.publicmethod import PublicMethod
from appium.webdriver.common.mobileby import MobileBy

class PageTwo(PublicMethod):
    def __init__(self,driver):
        PublicMethod.__init__(self,driver)
    def inputdata(self,text):
        self.input(text,MobileBy.ID,"com.kmxs.reader:id/search_text")
    def click_search(self):
        self.click(MobileBy.ID,"com.kmxs.reader:id/search_tv")
    def click_bookname(self):
        self.click(MobileBy.XPATH,"/hierarchy/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.support.v7.widget.RecyclerView/android.widget.LinearLayout[2]/android.widget.LinearLayout/android.widget.LinearLayout/android.widget.RelativeLayout/android.widget.TextView")

(3) pagethree.py模块

from daywork.app端自动化测试.夜神模拟器七猫.PO模型.basepage.publicmethod import PublicMethod
from appium.webdriver.common.mobileby import MobileBy

class PageThree(PublicMethod):
    def __init__(self,driver):
        PublicMethod.__init__(self,driver)
    def click_add(self):
        self.click(MobileBy.ID,"com.kmxs.reader:id/book_detail_foot_add_book_tv")

3.data包

info.yaml文件

caps:
    "platformName": "Android"
    "deviceName": "127.0.0.1:62001"
    "appPackage": "com.kmxs.reader"
    "appActivity": "com.kmxs.reader.home.ui.HomeActivity"

4.readdata包

readyaml.py

import yaml,os
def readYaml(path):
    with open(path,"r",encoding="utf-8") as file:
        data = yaml.load(stream=file,Loader=yaml.FullLoader)
        return data
if __name__ == '__main__':
    #os.path.dirname(__file__)当前文件的上一级目录
    #os.path.abspath(path)找到路径的绝对路径
    rootpath = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
    dd = os.path.join(rootpath , "config\config.yaml")
    print(dd)
    print(readYaml(dd))

5. testcase包

import pytest,time,os
from appium import webdriver
from daywork.app端自动化测试.夜神模拟器七猫.PO模型.pages.pageone import PageOne
from daywork.app端自动化测试.夜神模拟器七猫.PO模型.pages.pagetwo import PageTwo
from daywork.app端自动化测试.夜神模拟器七猫.PO模型.pages.pagethree import PageThree
from daywork.app端自动化测试.夜神模拟器七猫.PO模型.readdata.readyaml import readYaml
class TestPO():
    @classmethod
    def setupclass(cls):
        # caps = {}
        # caps["platformName"] = "Android"
        # caps["deviceName"] = "127.0.0.1:62001"
        # caps["appPackage"] = "com.kmxs.reader"
        # caps["appActivity"] = "com.kmxs.reader.home.ui.HomeActivity"
        rootpath = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
        dd = os.path.join(rootpath, "config\config.yaml")
        data = readYaml(dd)
        cls.driver = webdriver.Remote("http://localhost:4723/wd/hub", data["caps"])
        time.sleep(30)
    def test1(self):
        pageone=PageOne(self.driver)
        pageone.click_girls()
        pageone.click_search()
    def test2(self):
        pagetwo=PageTwo(self.driver)
        pagetwo.inputdata("朝花夕拾")
        pagetwo.click_search()
        pagetwo.click_bookname()
    def test3(self):
        pagethree=PageThree(self.driver)
        pagethree.click_add()
    @classmethod
    def teardownclass(cls):
        cls.driver.quit()
if __name__ == '__main__':
    pytest.main(['-s',"potest.py"])

猜你喜欢

转载自blog.csdn.net/qq_44954371/article/details/126733244