1.demo1.py
import time
from time import sleep
from appium import webdriver
from class45.config.config_read import yaml_load
data2 = yaml_load.read_yaml()
info = {
'platformName':'Android',
'platformVersion':'7.1.2',
'deviceName':'127.0.0.1:62001',
'appPackage':'com.tal.kaoyan',
'appActivity':'com.tal.kaoyan.ui.activity.SplashActivity',
'noReset':False
}
2.yaml文件
platformName: Android,
platformVersion: 7.1.2,
deviceName: 127.0.0.1:62001,
appPackage: com.tal.kaoyan,
appActivity: com.tal.kaoyan.ui.activity.SplashActivity,
noReset: False
ip: 127.0.0.1
port: 4723
3.读取yaml文件数据
import yaml
class yaml_load:
def read_yaml(self):
with open('../data/cs_caps.yaml','r',encoding='utf-8') as f:
data = yaml.load(f,yaml.FullLoader)
print(data)
return data
data2 = yaml_load().read_yaml()
print(data2)
4.放路径
import os
class Config:
config_dir = os.path.join(os.path.dirname(os.path.dirname(__file__)),'data')
yaml_path = config_dir+'\cs_caps.yaml'
print(yaml_path)
5.字典赋值
from appium import webdriver
from class45.config.config_read import yaml_load
def desired_caps():
data = yaml_load().read_yaml()
print(data)
info = {}
info['platformName'] = data['platformName']
info['platformVersion'] = data['platformVersion']
info['deviceName'] = data['deviceName']
info['appPackage'] = data['appPackage']
info['appActivity'] = data['appActivity']
info['noReset'] = data['noReset']
driver = webdriver.Remote('http://'+str(data['ip'])+':'+str(data['port'])+'/wd/hub',info)
driver.implicitly_wait(3)
return driver
if __name__ == '__main__':
desired_caps()
6.专门封装取消滑动
from selenium.webdriver.common.by import By
from class45.base.base_page import Base
class Common(Base):
cancel = (By.ID,'android:id/button2')
skip = (By.ID,'com.tal.kaoyan:id/tv_skip')
def check_cancel(self):
try:
self.click(self)
except Exception as e:
print('没有取消元素')
def check_skip(self):
try:
self.click(self)
except Exception as e:
print('没有跳过的元素')
def size(self):
x = self.driver.get_window_size()['width']
y = self.driver.get_window_size()['height']
return x,y
def swipeLeft(self,n,t):
size2 = self.size()
x1 = size2[0]*0.8
y1 = size2[0]*0.9
x2 = size2[0]*0.2
for i in range(n):
self.driver.swipe(x1,y1.x2,y1,t)
7.封装经常要使用的动作
from selenium.webdriver.support.wait import WebDriverWait
class Base:
def __init__(self,driver):
self.driver = driver
def locator(self,loc,timeout=10,poll=0.5):
WebDriverWait(self.driver,timeout,poll).until(lambda x:x.find_element(*loc))
def input_(self,loc,text):
self.locator(loc).send_keys(text)
def click(self,loc):
self.locator(loc).click()
8.登录封装
from selenium.webdriver.common.by import By
from class45.common.common_fun import Common
from class45.desired.desired_cap import desired_caps
class LoginView(Common):
global username_type = (By.ID,'com.tal.kaoyan:id/login_email_edittext')
global password_type = (By.ID,'com.tal.kaoyan:id/login_password_edittext')
global loginBtn = (By.ID,'com.tal.kaoyan:id/login_login_btn')
def login(self,username,password):
self.check_cancel()
self.check_skip()
self.input_(self,username_type,username)
self.input_(self,password_type,password)
self.click(self,loginBtn)
know = (By.ID, '')
warning2 = (By.ID, '')
def check_alter(self):
print('检查弹窗知道了')
try:
self.click(self.know)
except Exception as e:
print('没有我知道')
def check_alter(self):
print('检查下线提醒')
try:
self.click(self.warning2)
except Exception as e:
print('没有下线提醒')
if __name__ == '__main__':
driver = desired_caps()
lp = LoginView(driver)
lp.login('qwerty2664','qwerty123')