我正在研究将Selenium与Python结合使用的框架。除了测试报告,一切似乎都正常。到目前为止,我正在使用日志记录模型,但是我不确定它在现代自动化中是否足够好。 我根本不使用任何东西来编写数据驱动框架的所有组件。到目前为止,我的框架包含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"
答案 0 :(得分:0)
由于我是从头开始使用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:消息:元素不可交互 (会议信息:chrome = 74.0.3729.169) (驱动程序信息:chromedriver = 74.0.3729.6(255758eccf3d244491b8a1317aa76e1ce10d57e9-refs / branch-heads / 3729 @ {#29}),platform = Mac OS X 10.14.5 x86_64)