首页 > 解决方案 > 使用 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"

标签: pythonseleniumlogging

解决方案


由于我正在从头开始使用 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)


推荐阅读