python - 使用 Selenium 和 Python 开发测试报告的最佳方法
问题描述
我正在使用 Selenium 和 Python 开发框架。除了测试报告外,一切似乎都很好。到目前为止,我使用的是日志模型,但我不确定它在现代自动化中是否足够好。我不使用 atom 的任何东西来编写我的数据驱动框架的所有组件。到目前为止,我的框架由 3 个文件组成: 1 - setup.py 包含我正在使用的所有函数:setup()、teardown() click()、sendKeys() 等。在这个函数中,我还导入日志记录,并且每次我执行任何操作记录写入新文件。2 - data.json - 擦拭所有元素。3-testCase.py-所有的测试用例。请告诉我如何改进我的框架。谢谢
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoAlertPresentException
import time
import logging
import json
#logging when call writes to log file
logging.basicConfig(filename='tests_Run.log',level=logging.INFO, format='%(asctime)s:%(levelname)s:%(funcName)s:%(message)s')
#example of logging usage:
def findORclick_element(self, itemInDict ,elemLocator):# This function perform click
locatorStrategy = elemLocator[:2]
locator = load_value(itemInDict ,elemLocator)
ele = None
try:
if locatorStrategy == 'id': # return by ID
ele = self.driver.find_element_by_id(locator).click()
logging.info(' - - Clicked on "{}", - - with value: "{}"'.format(elemLocator, locator))
#this is how logging file output looks like:
2019-06-11 11:53:06,856:INFO:findORclick_element: - - Clicked on "id_GET_A_QUOTE_NOW", - - with value: "menu-item-188"
2019-06-11 11:53:07,098:INFO:findORclick_element: - - Clicked on "xp_PRODUCT_LIABILITY", - - with value: "/html/body/div[1]/div/div/div/div[1]/div[1]/div[1]/form/div[2]/div[1]/div[1]/ul/li[2]/div[1]/p/a"
2019-06-11 11:53:07,392:INFO:send_keys: - - - - - - - Typed in to "id_COMPANY_NAME", - - with value: "input_41_1"
2019-06-11 11:53:07,576:INFO:send_keys: - - - - - - - Typed in to "id_DOING_BUSINESS_AS", - - with value: "input_41_3"
2019-06-11 11:53:07,792:INFO:send_keys: - - - - - - - Typed in to "id_WEBSITE", - - with value: "input_41_92"
2019-06-11 11:53:07,970:INFO:send_keys: - - - - - - - Typed in to "id_MAILING_ADDRESS_LINE_1", - - with value: "input_41_104"
2019-06-11 11:53:08,070:INFO:send_keys: - - - - - - - Typed in to "id_ADDRESS_LINE_2", - - with value: "input_41_105"
2019-06-11 11:53:08,249:INFO:send_keys: - - - - - - - Typed in to "id_CITY", - - with value: "input_41_106"
2019-06-11 11:53:08,294:INFO:findORclick_element: - - Clicked on "xp_STATE_california", - - with value: "/html/body/div[1]/div/div/div/div[1]/div[1]/div[1]/form/div[3]/div[1]/div[1]/ul/li[7]/div/select/option[6]"
2019-06-11 11:53:08,431:INFO:send_keys: - - - - - - - Typed in to "id_ZIPCODE", - - with value: "input_41_109"
2019-06-11 11:53:08,554:INFO:send_keys: - - - - - - - Typed in to "id_CONTACT_NAME_FIRST", - - with value: "input_41_6_3"
2019-06-11 11:53:08,689:INFO:send_keys: - - - - - - - Typed in to "id_CONTACT_NAME_LAST", - - with value: "input_41_6_6"
解决方案
由于我正在从头开始使用 Python 框架构建我的 Selenium,并且没有使用任何东西,而是使用 Atom 来编码 - 经过研究,我决定使用以下设计。
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoAlertPresentException
from datetime import datetime
import logging
now = datetime.now().strftime('%Y-%m-%d---%H-%M-%S')
logging.basicConfig(filename='/test_log-%s.log' % now,
level=logging.INFO, format='%(asctime)s:%(levelname)s:%(message)s\n')
driver = webdriver.Chrome()
driver.get('https://www.google.com')
try:
driver.find_element_by_name('q').send_keys('selenium')
driver.find_element_by_name('btnK').click()
except Exception as e:
driver.save_screenshot('screenshot-%s.png' % now)
logging.info(e)
driver.quit()
这是日志文件的样子,当 'except' 块捕获错误时:
2019-06-13 19:36:41,327:INFO:Message: 元素不可交互(会话信息:chrome=74.0.3729.169)(驱动程序信息:chromedriver=74.0.3729.6(255758eccf3d244491b8a1317aa76e1ce10d57e9-refs/#branch-heads/3729@ 29}),平台=Mac OS X 10.14.5 x86_64)
推荐阅读
- excel - 尝试从 Spark (crealytics) 读取 excel 文件但看不到数据
- svelte - 如何将 querySelector 范围限定为 Svelte 中的组件?
- python - 如何从变量中提取“品种”和“#1097”?
- elasticsearch - 查找所有作为父级的 elasticsearch 文档
- linker - GNU 链接器:如何在 .text 中放置 .vector 部分
- python - 当我运行程序时,矢量化根本不起作用。我的问题可能是什么?
- firebase - userid 写他的帖子的规则
- jenkins-job-dsl - DSL 版本是否可供非管理员用户使用
- ansible - ansible 2.9 中的 set_when_task_skipped=false 替换是什么?
- swift - 交互式 UINavigationController 过渡