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.输出报告:

相关推荐
守城小轩1 小时前
基于Chrome140的Quora账号自动化(关键词浏览)——运行脚本(三)
运维·自动化·chrome devtools·指纹浏览器·浏览器开发
未来之窗软件服务1 小时前
幽冥大陆(五十五)ASR SetThreadInformation C语言识别到自动化软件
运维·自动化·asr·东方仙盟·操作系统级别错误
乾元7 小时前
用 AI 做联动:当应用层出现问题,网络如何被“自动拉入决策回路”
运维·开发语言·网络·人工智能·ci/cd·自动化
csdn_aspnet7 小时前
MobaXterm 运维实战、技巧与自动化
运维·自动化·mobaxterm
Wpa.wk8 小时前
自动化测试(java) - PO模式了解
java·开发语言·python·测试工具·自动化·po模式
凤希AI伴侣8 小时前
架构重构与AI能力聚焦:一人开发的自动化未来 凤希AI伴侣 · 开发日记 · 2025年12月20日
人工智能·重构·自动化·凤希ai伴侣
极地星光9 小时前
软件发布中.symbols文件夹单独发布全指南:从需求解析到自动化落地
运维·自动化
工会主席-阿冰10 小时前
使用pytest-selenium插件,ui自动化示例
selenium·pytest
池央10 小时前
IPIDEA赋能跨境电商:Amazon商品比价自动化采集实战
网络·自动化·php
不想画图10 小时前
Ansible自动化运维工具及基础用法
运维·自动化·ansible