一、遇到的问题,想要做简单的软件界面测试,能截图、能出错误报告,目前已经可跑起来软件,可用截图了,但生成报告还未实现,且出现问题较多;
二、代码内容:
1.代码内容baseDrivera01.py:
import os
import yaml
from selenium.webdriver.common.by import By
from appium import webdriver
import unittest
class baseDrivera01:
def init(self, se_driver: object) -> object:
self.driver = se_driver
# 加上上面那段函数,才能用类
@staticmethod
def android_driver() :
# 从desired_caps.yaml读取driver配置数据
# 项目根目录路径,即android-ui-autotest文件夹的路径,获取当前文件所在目录的上一级目录
BASE_PATH = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# capabilities配置文件desired_caps.py路径
# os.path.dirname返回pyth的父路径
# os.path.abspath返回pyth的绝对路径
DESIRED_CAPS_YAML_PATH = BASE_PATH + '\config\desired_caps.yaml'
print(DESIRED_CAPS_YAML_PATH)
stream = open(DESIRED_CAPS_YAML_PATH, 'r')
data = yaml.load(stream, Loader=yaml.FullLoader)
desired_caps = {'platformName' : data['platformName'],
'platformVersion' : data['platformVersion'],
'deviceName' : data['deviceName'],
'appPackage' : data['appPackage'],
'appActivity' : data['appActivity'],
'ensureWebviewsHavePages' : data['ensureWebviewsHavePages'],
'noReset' : data['noReset']}
driver = webdriver.Remote('http://' + str(data['ip']) + ':' + str(data['port']) + '/wd/hub', desired_caps)
driver.implicitly_wait(15)
return driver
if name == 'main':
Driveara = baseDrivera01(['some', 'se_driver'])
Driveara.android_driver()
2.代码内容login01.py
import unittest
from baseDrivera01 import baseDrivera01
from selenium.webdriver.common.by import By
import time
import os
import Assert.Assert
from BeautifulReport import BeautifulReport
class login01(unittest.TestCase) :
def setUp(self) -> None:
self.driver = baseDrivera01.android_driver()
# 定义失败截图方法(必须定义在测试类中)
def save_img(self, test_method) :
root_path = os.path.abspath(os.path.dirname(os.path.dirname(__file__))).replace('\\', '/')
print(root_path)
img_path = root_path + '/img'
emd= self.driver.get_screenshot_as_file('{}/{}.png'.format(img_path, test_method))
print(emd)
# def setUp(self) :
# # 初始化-登录
# baseDrivera01.baseDrvera01()
# self.driver = exzble.android_driver()
# 调用方法实列化
# 下面这个叫什么?函数装饰器
@BeautifulReport.add_test_img('测试报告test01lg_01')
# 装饰器,用例错误了,那么会自动调用save_img截图方法,存到指定目录下
# 没有报错也要的截图的情况需要再用例里面调用save_img()方法
def test_01_lg_01(self) :
# 调用类,先调用文件名baseDrivera,再调用类名,在这里用的时候用的是类的,而不是函数的;
self.driver.implicitly_wait(10)
self.driver.find_element(By.ID, "im2510.hollcon.com.im2510:id/edit_account").send_keys("admin")
self.driver.implicitly_wait(10)
self.driver.find_element(By.ID, "im2510.hollcon.com.im2510:id/edit_password").send_keys("123")
self.driver.implicitly_wait(10)
self.driver.find_element(By.ID, "im2510.hollcon.com.im2510:id/bt_login_land").click()
self.driver.implicitly_wait(10)
self.save_img('测试报告登录图')
self.driver.save_screenshot('login.png')
self.driver.get_screenshot_as_file('./images/login.png')
mr = self.driver.find_element(By.ID, "im2510.hollcon.com.im2510:id/tv_main_operation")
self.driver.implicitly_wait(10)
time.sleep(5)
print("运行完成,可进入查看结果!")
self.save_img('测试报告')
self.assertTrue(result == '测试用例失败截图')
Assert.Assert().isElementExitst(self.driver, mr, "用例执行成功!")
return driver
if name == 'main' :
# baseDrivera01.android_driver()
unittest.main()
#注释的这些都是还不知道怎么用的内容;
# suite = unittest.TestSuite()
# # 实列化TestSuit一个空的测试套件,给套件添加测试用例(方法)
# suite.addTest(login01('test_01_lg_01'))
#
# runner = unittest.TextTestRunner()
# runner.run(suite)
# loader = unittest.TestLoader()
# # addTest(类名(方法名):添加指定类中的指定方法)
#
# suite.addTests(loader.loadTestsFromModule(login01))
# # addTest(unittest.makeSuit(类名)):添加指定类中所有以test开头的方法
三、运行后的错误内容:
"D:\Program Files\python\python.exe" D:/python/appium/2510/common/login01.py
D:\python\appium\2510\config\desired_caps.yaml
D:\Program Files\python\lib\site-packages\appium\webdriver\webdriver.py:230: DeprecationWarning: desired_capabilities has been deprecated, please pass in an Options object with options kwarg
super().init(
D:/python/appium/2510/common/login01.py:21: ResourceWarning: unclosed file <_io.TextIOWrapper name='D:\python\appium\2510\config\desired_caps.yaml' mode='r' encoding='cp936'>
self.driver = baseDrivera01.android_driver()
ResourceWarning: Enable tracemalloc to get the object allocation traceback
D:/python/appium/2510
True
.D:\Program Files\python\lib\unittest\suite.py:84: ResourceWarning: unclosed
return self.run(*args, **kwds)
ResourceWarning: Enable tracemalloc to get the object allocation traceback
Ran 1 test in 18.538s
OK
运行完成,可进入查看结果!
Process finished with exit code 0
四、尝试了很多方法,目前仅做到了可用截图save_img功能截图成功,软件跑起来了;
五、想要的结果:不再报错误,目前看还有很多地方有错误,网上找了很多,还不是很明白;
这不是报错,只是一个警告,不会影响程序执行的
D:\Program Files\python\lib\site-packages\appium\webdriver\webdriver.py:230: DeprecationWarning: desired_capabilities has been deprecated, please pass in an Options object with options kwarg
super().init(
1.版本不兼容
File "D:\Program Files\python\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 247, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.InvalidSelectorException: Message: Locator Strategy '[object Object]' is not supported for this session
Stacktrace:
InvalidSelectorError: Locator Strategy '[object Object]' is not supported for this session
at AndroidUiautomator2Driver.validateLocatorStrategy (C:\Program Files\Appium\resources\app\node_modules\appium\node_modules\appium-base-driver\lib\basedriver\driver.js:392:13)
at AndroidUiautomator2Driver.findElOrElsWithProcessing (C:\Program Files\Appium\resources\app\node_modules\appium\node_modules\appium-base-driver\lib\basedriver\commands\find.js:31:8)
at AndroidUiautomator2Driver.findElement (C:\Program Files\Appium\resources\app\node_modules\appium\node_modules\appium-base-driver\lib\basedriver\commands\find.js:53:21)
at commandExecutor (C:\Program Files\Appium\resources\app\node_modules\appium\node_modules\appium-base-driver\lib\basedriver\driver.js:334:9)
at C:\Program Files\Appium\resources\app\node_modules\appium\node_modules\async-lock\lib\index.js:129:12
at AsyncLock._promiseTry (C:\Program Files\Appium\resources\app\node_modules\appium\node_modules\async-lock\lib\index.js:253:31)
at exec (C:\Program Files\Appium\resources\app\node_modules\appium\node_modules\async-lock\lib\index.js:128:9)
at AsyncLock.acquire (C:\Program Files\Appium\resources\app\node_modules\appium\node_modules\async-lock\lib\index.js:144:3)
at AndroidUiautomator2Driver.executeCommand (C:\Program Files\Appium\resources\app\node_modules\appium\node_modules\appium-base-driver\lib\basedriver\driver.js:347:39)
at AppiumDriver.executeCommand (C:\Program Files\Appium\resources\app\node_modules\appium\lib\appium.js:547:36)
at processTicksAndRejections (internal/process/task_queues.js:85:5)
at asyncHandler (C:\Program Files\Appium\resources\app\node_modules\appium\node_modules\appium-base-driver\lib\protocol\protocol.js:297:21)
2.查了下也是版本不兼容,appium-python-client和selenium的版本不兼容问题,处理成同一个版本,暂未处理;
During handling of the above exception, another exception occurred:
3.查了下说是大量访问的问题
4.上面出现的ResourceWarning: Enable tracemalloc to get the object allocation traceback问题,改了些东西,现在没有出现这个,待继续了解这块内容。