‘‘‘ -*- coding: utf-8 -*-
@Time : 2019/6/10 0010 10:39
@Author : 无邪
@File : test_data.py
@Software: PyCharm‘‘‘
‘‘‘测试用例‘‘‘
data_error=[{"tel":"","pwd":"1","expected":"手机号码或密码不能为空"},
{"tel":"1","pwd":"1","expected":"手机号码格式不正确"},
{"tel":"18438662004","pwd":"1","expected":"密码格式不正确"},
{"tel":"18438662004","pwd":"123456","expected":"错误的账号信息"},
{"tel":"18258148335","pwd":"","expected":"手机号码或密码不能为空"},
{"tel":"","pwd":"","expected":"手机号码或密码不能为空"}
]
data_right=[ {"tel":"18258148335","pwd":"148335","expected":"无邪妖君"}]
-----------------------------------------------------------------------------------------------
‘‘‘ -*- coding: utf-8 -*-
@Time : 2019/6/10 0010 9:37
@Author : 无邪
@File : base.py
@Software: PyCharm‘‘‘
from appium.webdriver import Remote
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.remote.webelement import WebElement
‘‘‘这是一个基础信息存放类‘‘‘
class Base:
def __init__(self):
caps={
"platformName": "Android", # 平台名称
"platformVersion": "5.1.1", # 系统版本
"deviceName": "127.0.0.1:62001", # 设备名称
‘automationName‘:‘uiautomator2‘,#定位toast必须配置uiautomator2
# "app":"D:\Future-release-2018.apk",
"appActivity": ".activity.MainActivity",
"appPackage": "com.lemon.lemonban" # 包名
}
self.driver=Remote(‘http://127.0.0.1:4723/wd/hub‘,caps)
# return self.driver
def wait_element(self,located)->WebElement:
w=WebDriverWait(driver=self.driver,timeout=6)
return w.until(ec.presence_of_element_located((located)))
def wait_one(self,located)->WebElement:
"""定位tost"""
w=WebDriverWait(driver=self.driver,timeout=8,poll_frequency=0.01)#默认是0.5秒
return w.until(ec.presence_of_element_located((located)))
-----------------------------------------------------------------------------------------------
‘‘‘ -*- coding: utf-8 -*-
@Time : 2019/6/10 0010 9:35
@Author : 无邪
@File : login.py
@Software: PyCharm‘‘‘
# from selenium.webdriver.common.by import By
import time
from appium.webdriver.common.mobileby import MobileBy as By
from page.base import Base
class Login(Base):
"""登录界面元素定位"""
login1=(By.ID,"com.lemon.lemonban:id/fragment_my_lemon_avatar_layout")#点击头像
tel=(By.ID,"com.lemon.lemonban:id/et_mobile")#手机号码输入框
password=(By.ID,"com.lemon.lemonban:id/et_password")#密码输入框
login_buttons=(By.ID,"com.lemon.lemonban:id/btn_login")#登录按钮
button33 = (By.ID, "com.lemon.lemonban:id/navigation_my")#我的柠檬
def image_login(self):
"""定位点击头像登录"""
self.button3()#点击我的柠檬
time.sleep(2)
self.wait_element(self.login1).click()#点击头像登录
def phone(self):
"""定位手机号码输入框"""
return self.wait_element(self.tel)
def pwd(self):
"""定位密码输入框"""
return self.wait_element(self.password)
def login_button(self):
"""点击登陆按钮"""
self.wait_element(self.login_buttons).click()
def start_login(self,call,passwd):
"""输入登录信息进行登录"""
self.phone().send_keys(call)
self.pwd().send_keys(passwd)
def button3(self):
"""定位我的柠檬"""
return self.wait_element(self.button33).click()
def hint(self,t):
"""定位toast"""
#-------------------------这个方法不行,没调通------------------------------------------------------------
# if self.wait_one((By.XPATH,"//*[contains(@text,‘手机号码或密码不能为空‘)]")):
# return self.wait_one((By.XPATH,"//*[contains(@text,‘手机号码或密码不能为空‘)]")).text
# elif self.wait_one((By.XPATH,"//*[contains(@text,‘手机号码格式不正确‘)]")):
# return self.wait_one((By.XPATH, "//*[contains(@text,‘手机号码格式不正确‘)]")).text
# elif self.wait_one((By.XPATH, "//*[contains(@text,‘密码格式不正确‘)]")):
# return self.wait_one((By.XPATH, "//*[contains(@text,‘密码格式不正确‘)]")).text
# elif self.wait_one((By.XPATH, "//*[contains(@text,‘错误的账号信息‘)]")):
# return self.wait_one((By.XPATH, "//*[contains(@text,‘错误的账号信息‘)]")).text
# else:
# return "定位失败"
#-------------------------------------下面这个方法正确----------------------------------------------------
if self.wait_one((By.XPATH, "//*[contains(@text,‘{}‘)]".format(t))):
return self.wait_one((By.XPATH, "//*[contains(@text,‘{}‘)]".format(t))).text
else:
return "定位失败"
----------------------------------------------------------------------------------------------------------------------
‘‘‘ -*- coding: utf-8 -*-
@Time : 2019/6/10 0010 11:13
@Author : 无邪
@File : test_login.py
@Software: PyCharm‘‘‘
from page.login import Login
from data.test_data import data_error,data_right
import pytest
import time
class TestLogin:
"""测试登陆"""
logins = Login()
time.sleep(2)
logins.image_login()
@pytest.mark.wrong
@pytest.mark.parametrize("data",data_error)
def test_error_login(self,data):
"""反向用例登陆"""
self.logins.start_login(data["tel"],data["pwd"])
time.sleep(2)
self.logins.login_button()
fact_result=self.logins.hint(data["expected"])
print(fact_result)
try:
assert (fact_result==data["expected"])
print("用例比对通过,实际结果{}与预期结果{}一致".format(fact_result, data["expected"]))
except AssertionError as e:
print("用例比对失败,实际结果{}与预期结果{}不符".format(fact_result,data["expected"]))
raise e
@pytest.mark.parametrize("data",data_right)
@pytest.mark.right
def test_right_login(self,data):
self.logins.start_login(data["tel"],data["pwd"])
self.logins.login_button()
---------------------------------------------------------------------------------------------------------
执行结果:
E:\lemon>pytest -m wrong -s
====================================================================== test session starts ======================================================================
platform win32 -- Python 3.6.3, pytest-4.5.0, py-1.8.0, pluggy-0.11.0
rootdir: E:\lemon
plugins: metadata-1.8.0, html-1.20.0, allure-pytest-2.6.3
collected 7 items / 1 deselected / 6 selected
test_login.py 手机号码或密码不能为空
用例比对通过,实际结果手机号码或密码不能为空与预期结果手机号码或密码不能为空一致
.手机号码格式不正确
用例比对通过,实际结果手机号码格式不正确与预期结果手机号码格式不正确一致
.密码格式不正确
用例比对通过,实际结果密码格式不正确与预期结果密码格式不正确一致
.错误的账号信息
用例比对通过,实际结果错误的账号信息与预期结果错误的账号信息一致
.手机号码或密码不能为空
用例比对通过,实际结果手机号码或密码不能为空与预期结果手机号码或密码不能为空一致
.手机号码或密码不能为空
用例比对通过,实际结果手机号码或密码不能为空与预期结果手机号码或密码不能为空一致
.