selenium自动化登录模块HTMLTestRunner测试报告

1.下载HTMLTestRunner.py放到python的Lib目录下,python3之后的,文件要修改以下内容:

第94行,将import StringIO修改成import io

第539行,将self.outputBuffer = StringIO.StringIO()修改成self.outputBuffer = io.StringIO()

第642行,将if not rmap.has_key(cls):修改成if not cls in rmap:

第766行,将uo = o.decode('latin-1')修改成uo = e

第772行,将ue = e.decode('latin-1')修改成ue = e(网络上很多这句是没写的,遗漏了。这句不改会有提示错误且测试报告无法生成)

第774行,将ue = e.decode('latin-1')修改成ue = e(网络上写的是775行是错误的,应该为774行)

第631行,将print >> sys.stderr, '\nTime Elapsed: %s' % (self.stopTime-self.startTime)修改成print(sys.stderr, '\nTime Elapsed: %s' % (self.stopTime-self.startTime))

2.登录模块代码CSDNlogin.py:

复制代码
import time
import unittest

from selenium.webdriver.common.by import By

import CSDNconf
class CSDNlogin(unittest.TestCase):
    def testLogin(self):

        CSDNconf.driver.get("https://passport.csdn.net/")
        CSDNconf.driver.maximize_window()
        #点击密码登录
        CSDNconf.driver.find_element(By.XPATH,"/html/body/div[2]/div/div[2]/div[2]/div[2]/div/div[1]/span[4]").click()
        #输入用户名
        CSDNconf.driver.find_element(By.XPATH,"/html/body/div[2]/div/div[2]/div[2]/div[2]/div/div[2]/div/div[1]/div/input").send_keys("627168856@qq.com")
        #输入密码
        CSDNconf.driver.find_element(By.XPATH,"/html/body/div[2]/div/div[2]/div[2]/div[2]/div/div[2]/div/div[2]/div/input").send_keys("zhangaihua@315717")
        # 勾选我已阅读并同意 服务条款 和 隐私协议
        CSDNconf.driver.find_element(By.XPATH,"/html/body/div[2]/div/div[2]/div[2]/div[2]/div/div[2]/p/div/i").click()
        #点击登录按钮
        CSDNconf.driver.find_element(By.XPATH,"/html/body/div[2]/div/div[2]/div[2]/div[2]/div/div[2]/div/div[4]/button").click()
        time.sleep(5)
        CSDNconf.driver.quit()

3.main.py

复制代码
# This is a sample Python script.
import unittest
import HTMLTestRunner
import HTMLReport
from CSDNlogin import CSDNlogin


# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.


def print_hi(name):
    # Use a breakpoint in the code line below to debug your script.
    print(f'Hi, {name}')  # Press Ctrl+F8 to toggle the breakpoint.


# Press the green button in the gutter to run the script.
if __name__ == '__main__':
   testunit = unittest.TestSuite()
   # 添加测试用例到测试套件中
   testunit.addTest(CSDNlogin("testLogin"))#CSDNlogin是类名testLogin是函数名
   print(testunit)
   # 定义个报告存放路径
   filename = 'D:\\Pythonproject\\CSDNUItest\\report\\result.html'
   fp = open(filename, 'wb')
   # 定义测试报告
   runner = HTMLTestRunner.HTMLTestRunner(stream=fp, title=u'测试报告', description=u'用例执行情况:')
   # 运行测试用例
   runner.run(testunit)
   # 关闭报告文件
   fp.close()


# See PyCharm help at https://www.jetbrains.com/help/pycharm/

4.输出报告:

相关推荐
天远Date Lab22 分钟前
零信任架构实战:基于天远企业股权变更构建自动化招投标供应商股东穿透审查网关
java·人工智能·架构·自动化
hzxxxz27 分钟前
3个模块搭稳一条自动化链路:定时任务、幂等与对账
运维·自动化
搞科研的小刘选手1 小时前
【IEEE出版,快至会后3-4个月检索 | 北方民族大学主办 | 中国·银川(线上+线下)】2026年机器人、智能计算与自动化学术会议(RAIC 2026)
机器人·自动化·学术会议·智能计算·会议推荐·北方民族大学·银川
大耳朵爱学习2 小时前
Andrew Ng深度解析:普通人如何利用AI把想法变成工具,实现工作自动化(小白程序员必看)
运维·人工智能·ai·自动化·大模型·就业·大模型学习
2601_967987372 小时前
Python在自动化测试中有哪些应用?
python自动化测试·selenium·appium·pytest·性能测试
4SAPI3 小时前
企业级大模型工作流基础设施选型指南:4SAPI、硅基流动与 OpenRouter 的架构权衡
运维·人工智能·架构·自动化
东舟技术3 小时前
从人工目检到AI视觉:智能座舱瞬态故障的自动化捕获与溯源
功能测试·测试工具·自动化
Tel199253080043 小时前
旋转变压器型编码器转多种绝对值协议 旋变转SSI/BISS-C/ENDAT2.2 RS422RTU协议+增量 转换器
单片机·自动化·工业自动化·仪器仪表·工控设备
工业机器人视觉检测设备4 小时前
云汇智能铝制机盖视觉检测设备落地 筑牢汽车零部件装配质量防线
自动化·视觉检测·自动化设备·检测设备
天远API4 小时前
零信任架构实战:基于天远企业股权变更构建自动化私募股权投后控制权穿透与异动预警网关
大数据·人工智能·架构·自动化