app自动化测试框架之conftest.py函数封装

app自动化测试框架之conftest.py函数封装

conftest.py代码

# —— coding :utf-8 ——
# @time:    2020/9/14 0:12
# @IDE:     APPTest_Framework
# @Author: xxxxxxx
# @Email:  xxxxxxx@qq.com
# @File:    conftest.py
import time
import yaml
import pytest
from Common.dir_config import Desired_Cap_dir
from appium import webdriver
from PageObjects.Common_Bus import CommonBus
from PageObjects.index_page import IndexPage
from PageLocators import Common_locator as loc
from Common.base_page import BasePage
from PageObjects.login_page import LoginPage
from TestDatas import login_datas

@pytest.fixture
def startApp():
    # 准备服务器参数,与appium server进行连接 。noReset =True
    driver = baseDriver()
    # 1. 判断欢迎页面是否存在
    CommonBus(driver).handle_welcome_page()
    # 2. 判断当前用户是否已登录
    status = IndexPage(driver).get_loginStatus()
    # 3. 如果已登录,先退出
    if status=='True':
        doc = '首页页面--退出功能'
        BasePage(driver).wait_eleVisible(loc.exit_button,doc=doc)
        BasePage(driver).click_element(loc.exit_button,doc=doc)

# 除登录以外,通用的前置条件
@pytest.fixture
def loginApp():
    # 准备服务器参数,与appium server进行连接 。noReset =True
    driver = baseDriver()
    # 1. 判断欢迎页面是否存在
    CommonBus(driver).handle_welcome_page()
    # 2. 判断当前用户是否已登录. 没有登录则进行登录状态
    status = IndexPage(driver).get_loginStatus()
    if status == 'False':
        doc = '首页页面--登录功能'
        LoginPage(driver).input_username(login_datas.username)
        LoginPage(driver).input_password(login_datas.password)

    # 3. 是否有手势设置密码的框。 不设置
    CommonBus(driver).is_setGesture()

def baseDriver(server_port=4723, noReset=None, automationName=None, **kwargs):
    # 将默认配置数据读取出来
    fs = open(Desired_Cap_dir + '/desired_caps.yaml')
    desired_caps = yaml.load(fs)
    # 参数调用
    if noReset is not None:
        desired_caps['noReset'] = noReset
    if automationName is not None:
        desired_caps['automationName'] = automationName
    # 返回一个启动对象 - driver
    return webdriver.Remote('http://127.0.0.1:{}/wd/hub'.format(server_port), desired_caps)

Common_Bus.py代码

# —— coding :utf-8 ——
# @time:    2020/10/1 3:40
# @IDE:     py_AppTest
# @Author:  xxxxx
# @Email:   xxxxx@qq.com
# @File:    Common_Bus.py
from Common.basepage import BasePage
import time
from appium.webdriver.common.touch_action import TouchAction
from PageLocators import Common_locator as Cloc
import logging
# 公共业务

# 欢迎页面
class CommonBus(BasePage):
    # 处理欢迎页面
    def handle_welcome_page(self):
        """
        如果没有找到首页元素/或不包含MainActivity,就是欢迎页面
        """
        time.sleep(7)
        curAct = self.driver.current_activity
        if curAct.find("MainActivity") == -1:  # 字符串找不到返回-1
            # 滑动欢迎页面至首页
            # 左滑三次,点击立即体验
            for i in range(3):
                self.swipe_left(self.get_size())
                time.sleep(1)
            # 点击立即体验

    # 导航页面

    # 是否设置手势密码
    def is_setGesture(self, action=False,a=None,b=None,c=None,d=None):
        # 有没有设置手势,密码的弹框 -5s
        # 如果有,是设置还是不设置
        if action == False: # 点击不设置
            try:
                self.click_element(Cloc.No_setGesture)
            except Exception as e:
                logging.exception(e)
                raise e
        else:  ##手势密码 封装: 九宫格(012;345;678)
        	print("输入手势密码")
            list_pwd = self.driver.find_elements_by_class_name("android.widget.ImageView")
            TouchAction(self.driver).press(list_pwd[a]).move_to(list_pwd[a]).move_to(list_pwd[b]).wait(100).move_to(
                list_pwd[c]).wait(100).move_to(list_pwd[d]).release().perform()
            time.sleep(1)
            
            """如果为新注册,或者修改手势密码的时候,需要输入两次手势密码,如果只是登录的话就是一次"""
            try:
                ee = self.driver.find_element_by_name("请再绘制手势密码")
                list_pwd = self.driver.find_elements_by_class_name("android.widget.ImageView")
                TouchAction(self.driver).press(list_pwd[1]).move_to(list_pwd[1]).move_to(list_pwd[4]).wait(100).move_to(
                    list_pwd[7]).wait(100).move_to(list_pwd[8]).release().perform()
            except Exception:
                pass

index_page.py代码

from Common.base_page import BasePage
from PageLocators.login_locator import LoginLocator as loc

class IndexPage(BasePage):
    def get_loginStatus(self):
        # 获取当前app登录状态,已登录为True,未登录为False
        try:
            # 等待5秒,找登录/注册按钮
            doc = '首页页面--登录/注册'
            self.wait_eleVisible(loc.login_button,timeout=5,doc='')
            ele_attr = self.get_element_attribute(loc.login_button,'button',doc)
            if ele_attr=='登录':
                return False
        except:
            return True
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值