做 google 联系人的自动化练习,新建了一个子类,继承了 appium 中的 Webdriver,运行后提示继承的类无 session_id 属性,求解答
1.继承类代码,在 uiTest.py 文件中,只贴出了跟本题有关的方法:
from appium import webdriver
from appium.webdriver.webdriver import WebDriver
import logging
import configparser
import os
import time
class myui(WebDriver):
"""继承appium的Webdriver类,封装一些自己的方法"""
def __init__(self,appPackage,appActivity):
"""初始化连接数据"""
self.desired_caps = {}
self.driver = None
self.desired_caps = {
'platformName': 'android',
'platformVersion': '9',
'deviceName': 'GM9',
'automationName': 'uiautomator2'
}
self.desired_caps['appPackage'] = appPackage
self.desired_caps['appActivity'] = appActivity
def connect_driver(self,hostName='http://127.0.0.1:4723/wd/hub'):
"""连接appium服务器,hostname默认为本机的服务器"""
try:
self.driver = webdriver.Remote(hostName, self.desired_caps)
print(type(self.driver))
self.driver.implicitly_wait(5)
logging.info('driver_connect success')
except Exception as myerror:
logging.error('connect error',myerror)
def findElement(self,type,element):
#能过传入type寻找类型,elment控件字串,自动查找控件,返回控件,如未找到则返回None
e = None
try:
if type=='id':
e = self.driver.find_element_by_id(element)
elif type=='class':
e = self.driver.find_elements_by_class_name(element)
except Exception as er:
logging.error('finderror:',er)
self.getScrren()
assert False
return e
2.运行代码:
from time import sleep
from MTBFTest.uiTest import myui
import pytest
import logging
import mylog
import os
class TestCase_contact():
def test_newConteact(self):
try:
conf = myui.getDate('contactconfig.ini')
driver = myui(conf.get('apppackage'),conf.get('appactivity'))
driver.connect_driver()
driver.findElement('id',conf.get('skip_id')).click()
sleep(3)
driver.tap([(946,1884)],500) #这一行代码报错
#driver.find_element_by_xpath('//*[@content-dest="创建联系人"]')
ine = driver.findElement('id',conf.get('in_name_f_id'))
conteactName = ine.find_elements_by_class_name(conf.get('in_classes'))
conteactName[0].send_keys('test')
conteactName[1].send_keys('test')
driver.find_element_by_id(conf.get('save_id')).click()
e = driver.find_element_by_xpath("//*[contains(@text,'已保存')]") # 此处为使用xpath模糊定位定位toast
text = e.get_attribute("text")
logging.info(text)
assert '已保存' in text
ui.press_keycode(4)
except Exception as e:
logging.error(e)
assert False
finally:
driver.driverquit()
if __name__=='__main__':
mylog.startLog()
pytest.main(['-q'])
3.运行结果:
4.错误提示为: AttributeError: 'myui' object has no attribute 'session_id'