测试用例
from selenium import webdriver
from selenium.webdriver import chrome
from selenium.webdriver.common.by import By
from selenium.webdriver.common import *
import time
import unittest
from HtmlTestRunner import HTMLTestRunner
class WebTestCase(unittest.TestCase):
driver=None
def setUp(cls) -> None:
"""测试方法前自动执行"""
cls.driver =webdriver.Chrome()
cls.driver.implicitly_wait(10)
def tearDown(cls) -> None:
"""测试方法后自动执行"""
cls.driver.quit()
cls.driver=None
def test_baidu(self):
self.driver.get("http://baidu.com")
el=self.driver.find_element(By.XPATH,'//*[@id="kw"]')
time.sleep(1)
el.click()
el.send_keys("哔哩哔哩")
el=self.driver.find_element(By.XPATH,'//*[@id="su"]')
el.click()
time.sleep(1)
#点击链接/html/body/div[2]/div[3]/div[1]/div[3]/div[1]/div/div[1]/h3/a/em
el=self.driver.find_element(By.XPATH,'//*[@id="1"]/div[1]/div[1]/h3/a/em')
el.click()
time.sleep(1)
self.driver.switch_to.window(self.driver.window_handles[-1])
el=self.driver.find_element(By.XPATH,"/html/body/div[2]/div[2]/div[1]/div[1]/ul[2]/li[1]/li/div/div/span")
self.assertIn("登录",el.text)
测试套件 和测试报告
# 执行测试套件的入口
# 导包
import os
import time
import unittest
from selenium import webdriver
from test_Chrome import WebTestCase
import HtmlTestRunner
# 创建测试套件对象
suite = unittest.TestSuite()
# 添加测试用例
loader=unittest.TestLoader
case=[WebTestCase('test_baidu')]
# case=[TestBaidu('test01_baidu_bilibili')]
suite.addTests(case)
# suite = unittest.defaultTestLoader.discover('./test_case/', pattern="test*.py")
# 生成测试报告执行方法
jieguo = unittest.TextTestRunner()
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
report_dir = BASE_DIR
now = time.strftime("%Y-%m-%d %H_%M_%S")
report_name = report_dir + '/' + now + 'result.html'
with open(report_name, 'w') as f:
jieguo = HtmlTestRunner.HTMLTestRunner(stream=f, report_title='自动化测试报告,测试结果如下', descriptions='用例执行情况',open_in_browser=("D:\app\python\Lib\/ui_by_selenium\reports",jieguo))
jieguo.run(suite)
f.close()
标签:el,unittest,driver,ui,import,div,self,百度
From: https://www.cnblogs.com/cy-xt/p/17000872.html