网易126邮箱自动化测试
项目介绍
项目简介
学习了关于自动化测试后,测试网易126邮箱的主要功能。目的是为了对网易126邮箱的主要功能:登录、退出登录、添加联系人、删除联系人、管理分组、添加星标联系人、取消星标联系人、发送邮件、接收邮件、查看邮件、草稿箱相关功能进行自动化测试。旨在提高测试效率与质量。
测试地址
https://mail.126.com/
项目设计
1.python编程语言设计测试脚本
2.webdriver驱动浏览器并操作页面元素
3.二次封装webdriver 操作方法
4.通过第三方插件pytest-html生成测试报告
设计测试用例
环境准备与使用工具
工具:
1、windows系统
2、Chrome浏览器(版本:128.0.6613.138)
3、PyCharm
环境:
1、Python 3.11
2、配置安装:Selenium 4.24.0 (pip install selenium)
3、配置安装:Pytest 8.3.3 (pip install pytest)
4、配置安装:Pytest-html 4.1.1 (pip install pytest-html)
5、配置安装:webdriver-manager (pip install webdriver-manager)
目录结构
NewwangyiemailTest
-common
--_init_.py
--head.py
-tests
--_init_.py
--test_01_Login.py
--test_02_Content.py
--test_03_MainPage.py
--test_04_Edit.py
代码实现
common包下的创建Chrome浏览器web驱动对象并进行封装
from selenium import webdriver
from selenium.webdriver.ie.service import Service
from webdriver_manager.chrome import ChromeDriverManager
class WebDriver:
driver=""
def __init__(self):
self.driver=webdriver.Chrome(service=Service(ChromeDriverManager().install()))
wwebdriver=WebDriver()
tests包下的页面功能测试
注册功能手动测试
登录功能
账号正确,密码正确
from enum import auto
from time import sleep
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.expected_conditions import alert_is_present
from selenium.webdriver.support.wait import WebDriverWait
from common.head import wwebdriver
class TestLogin:
url=""
driver=""
@classmethod
def setup_class(cls):
cls.driver=wwebdriver.driver
cls.url="https://mail.126.com/"
cls.driver.get(cls.url)
def test_ZhuCeTest03(self):
self.driver.find_element(By.NAME,"email").clear()
self.driver.find_element(By.NAME,"password").clear()
self.driver.find_element(By.NAME,"email").send_keys("gaoyuqi2002")
self.driver.find_element(By.NAME,"password").send_keys("GYQ20021013gz")
sleep(1)
self.driver.find_element(By.CSS_SELECTOR,"#dologin").click()
sleep(2)
可以正常退出登录
def test_ZhuCeTest08(self):
self.driver.switch_to.default_content()
sleep(2)
hover = self.driver.find_element(By.CSS_SELECTOR, "#_mail_icon_0_0")
hover.click()
# 悬停页面ActionChains
ActionChains(self.driver).move_to_element(hover).perform()
sleep(3)
self.driver.find_element(By.XPATH, "//div[starts-with(@id,'_mail_') and text()='退出登录']").click()
sleep(3)
txt = self.driver.find_element(By.XPATH, "//*[@id='info']/h1").text
print(txt)
未注册直接登录
def test_ZhuCeTest01(self):
self.driver.maximize_window()
self.driver.implicitly_wait(3)
# 定位到框架
# self.driver.switch_to.frame(self.driver.find_element(By.XPATH,"//iframe[starts-with(@id,'x-URS-iframe')]"))
self.driver.switch_to.frame(self.driver.find_element(By.CSS_SELECTOR,"iframe"))
self.driver.find_element(By.NAME,"email").clear()
self.driver.find_element(By.NAME,"password").clear()
# 输入账号密码
self.driver.find_element(By.NAME,"email").send_keys("wangwu2024")
self.driver.find_element(By.NAME,"password").send_keys(" GYQ20021013gz")
sleep(1)
# 找到登录直接点击
self.driver.find_element(By.CSS_SELECTOR,"#dologin").click()
print("登录失败")
sleep(3)
账号密码都不正确
def test_ZhuCeTest04(self):
self.driver.maximize_window()
self.driver.get("https://mail.126.com/")
self.driver.switch_to.frame(self.driver.find_element(By.XPATH,"//iframe[starts-with(@id,x-URS-iframe)]"))
self.driver.find_element(By.NAME,"email").clear()
self.driver.find_element(By.NAME,"password").clear()
self.driver.find_element(By.NAME, "email").send_keys("gaoyuqi200")
self.driver.find_element(By.NAME, "password").send_keys("GYQ20021013g")
sleep(1)
self.driver.find_element(By.CSS_SELECTOR, "#dologin").click()
sleep(3)
print(self.driver.find_element(By.CLASS_NAME,"ferrorhead").text)
账号密码都为空
def test_ZhuCeTest05(self):
self.driver.find_element(By.NAME,"email").clear()
self.driver.find_element(By.NAME,"password").clear()
sleep(1)
self.driver.find_element(By.CSS_SELECTOR, "#dologin").click()
sleep(3)
print(self.driver.find_element(By.CLASS_NAME,"ferrorhead").text)
账号错误,密码正确
def test_ZhuCeTest06(self):
self.driver.find_element(By.NAME,"email").clear()
self.driver.find_element(By.NAME,"password").clear()
self.driver.find_element(By.NAME, "email").send_keys("gaoyuqi200")
self.driver.find_element(By.NAME, "password").send_keys("GYQ20021013gz")
sleep(1)
self.driver.find_element(By.CSS_SELECTOR, "#dologin").click()
sleep(3)
print(self.driver.find_element(By.CLASS_NAME,"ferrorhead").text)
账号正确密码错误
def test_ZhuCeTest07(self):
self.driver.find_element(By.NAME, "email").clear()
self.driver.find_element(By.NAME, "password").clear()
self.driver.find_element(By.NAME, "email").send_keys("gaoyuqi2002")
self.driver.find_element(By.NAME, "password").send_keys("GYQ20021013")
sleep(1)
self.driver.find_element(By.CSS_SELECTOR, "#dologin").click()
sleep(3)
print(self.driver.find_element(By.CLASS_NAME, "ferrorhead").text)
通讯录功能
增加好友功能
from time import sleep
from selenium.common import NoSuchElementException
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from common.head import wwebdriver
class TestContent:
driver=""
url=""
@classmethod
def setup_class(cls):
cls.driver=wwebdriver.driver
cls.url="https://mail.126.com/"
cls.driver.get(cls.url)
def test_AddContentTest01(self):
# 登录
self.driver.maximize_window()
self.driver.switch_to.frame(self.driver.find_element(By.CSS_SELECTOR,"iframe"))
self.driver.find_element(By.NAME, "email").clear()
self.driver.find_element(By.NAME, "password").clear()
self.driver.find_element(By.NAME, "email").send_keys("gaoyuqi2002")
self.driver.find_element(By.NAME, "password").send_keys("GYQ20021013gz")
sleep(1)
self.driver.find_element(By.CSS_SELECTOR, "#dologin").click()
# 回到当前页面
self.driver.switch_to.default_content()
sleep(3)
self.driver.find_element(By.XPATH,"//div[starts-with(@id,'_mail_tabitem') and text()='通讯录']").click()
sleep(3)
# 点击新建联系人
self.driver.find_element(By.XPATH,"//span[starts-with(@class,'nui-btn-text') and text()='新建联系人']").click()
sleep(3)
# 输入联系人姓名
name_wait=WebDriverWait(self.driver,3).until(EC.visibility_of_element_located((By.XPATH,"//input[starts-with(@id,'input_N') and @class='nui-ipt-input']")))
name_wait.send_keys("111")
sleep(20)
self.driver.find_element(By.XPATH,"//span[starts-with(@class, 'nui-btn-text') and text()='确 定']").click()
sleep(5)
try:
self.driver.find_element(By.XPATH,"//span[starts-with(@class,'nui-txt nui-txt-normal') and text()='111']")
print("添加成功")
except NoSuchElementException as e:
print(f"没有找到元素:{e}")
# 回退到首页
self.driver.back()
# 刷新页面
self.driver.refresh()
删除通讯录好友
def test_AddContentTest02(self):
# self.driver.maximize_window()
self.driver.find_element(By.XPATH,"//div[starts-with(@id,'_mail_tabitem') and text()='通讯录']").click()
sleep(1)
# # 需要删除的联系人---
self.driver.find_element(By.XPATH,"/html/body/div[2]/div[1]/div/div/div/section[1]/div/table/tbody/tr[3]/td[2]/span/span/b").click()
sleep(1)
# 点击删除
self.driver.find_element(By.XPATH,"/html/body/div[2]/div[1]/div/header/div/div[2]/div[3]/span").click()
sleep(1)
# 点击确定
self.driver.find_element(By.XPATH,"//span[text()='确 定']").click()
sleep(3)
print("删除成功")
# 回退到首页
self.driver.back()
# 刷新页面
self.driver.refresh()
管理分组–通过复制到组新建分组并移入联系人
def test_AddContentTest03(self):
self.driver.find_element(By.XPATH, "//div[starts-with(@id,'_mail_tabitem') and text()='通讯录']").click()
sleep(3)
# 选择一个联系人---222
self.driver.find_element(By.XPATH,"/html/body/div[2]/div[1]/div/div/div/section[1]/div/table/tbody/tr[2]/td[2]/span/span/b").click()
# 点击复制到组
self.driver.find_element(By.XPATH, "//span[text()='复制到组']").click()
sleep(3)
# 点击新建分组并复制
self.driver.find_element(By.XPATH, "//span[text()='新建分组并复制']").click()
sleep(10)
# 手动测试
# # 输入新建组的名字
# 回退到首页
self.driver.back()
# 刷新页面
self.driver.refresh()
首页功能
添加星标联系人
from time import sleep
from selenium.webdriver.common.by import By
from common.head import wwebdriver
class TestMainPage:
driver=""
url=""
@classmethod
def setup_class(cls):
cls.driver = wwebdriver.driver
cls.url = "https://mail.126.com/"
cls.driver.get(cls.url)
#添加星标联系人
def test_AddStartContent(self):
sleep(2)
# 点击进入通讯录
self.driver.find_element(By.XPATH, "//div[starts-with(@id,'_mail_tabitem') and text()='通讯录']").click()
sleep(2)
# 点击222联系人的星星图标
self.driver.find_element(By.XPATH,"/html/body/div[2]/div[1]/div/div/div/section[1]/div/table/tbody/tr[2]/td[3]/div[1]/b").click()
sleep(2)
# 点击111联系人的星星图标
self.driver.find_element(By.XPATH,"/html/body/div[2]/div[1]/div/div/div/section[1]/div/table/tbody/tr[1]/td[3]/div[1]/b").click()
#您已成功编辑联系人
print(self.driver.find_element(By.XPATH,"//span[text()='您已成功编辑联系人']").text)
# 回退到首页
self.driver.back()
# 刷新页面
self.driver.refresh()
取消星标联系人
def test_DelStartContent(self):
sleep(1)
# 点击进入通讯录
self.driver.find_element(By.XPATH, "//div[starts-with(@id,'_mail_tabitem') and text()='通讯录']").click()
sleep(2)
# 点击222联系人的星星图标
self.driver.find_element(By.XPATH,"/html/body/div[2]/div[1]/div/div/div/section[1]/div/table/tbody/tr[2]/td[3]/div[1]/b").click()
sleep(2)
self.driver.find_element(By.XPATH,"//span[text()='确 定']").click()
# 您已成功编辑联系人
self.driver.refresh()
print("取消成功")
# 回退到首页
self.driver.back()
# 刷新页面
self.driver.refresh()
发邮件–收件人为空
def test_SendEmail01(self):
self.driver.implicitly_wait(10)
# 点击写信
self.driver.find_element(By.XPATH,"/html/body/div[1]/nav/div[1]/ul/li[2]").click()
# # 输入主题----手动输入
sleep(10)
# 输入内容
sleep(2)
# 查找输入框
self.driver.find_element(By.XPATH,"/html/body")
# 定位到框架
self.driver.switch_to.frame(self.driver.find_element(By.XPATH,"//iframe[starts-with(@class,'APP-editor-iframe')]"))
self.driver.find_element(By.CLASS_NAME,"nui-scroll").send_keys("自动化测试")
# 回到当前页面
self.driver.switch_to.default_content()
sleep(3)
self.driver.find_element(By.XPATH,"//span[text()='发送']").click()
sleep(3)
print(self.driver.find_element(By.XPATH,"//span[text()='请填写收件人地址后再发送']").text)
# 回退到首页
self.driver.back()
# 刷新页面
self.driver.refresh()
发邮件–内容为空
def test_SendEmail02(self):
self.driver.implicitly_wait(10)
# 点击写信
self.driver.find_element(By.XPATH, "/html/body/div[1]/nav/div[1]/ul/li[2]").click()
sleep(2)
# # 输入联系人和主题----手动输入
sleep(20)
# 输入内容
# 定位到框架
self.driver.switch_to.frame(self.driver.find_element(By.XPATH,"//iframe[starts-with(@class,'APP-editor-iframe')]"))
self.driver.find_element(By.CLASS_NAME,"nui-scroll").send_keys(" ")
sleep(5)
# 回到当前页面
self.driver.switch_to.default_content()
#点击发送
self.driver.find_element(By.XPATH,"//span[text()='发送']").click()
# 回退到首页
self.driver.back()
# 刷新页面
self.driver.refresh()
收信—点击收信查看
def test_AccpectEmail01(self):
self.driver.implicitly_wait(10)
# 点击收 信
self.driver.find_element(By.XPATH,"//span[text()='收 信']").click()
sleep(2)
# 点击需要查看的信件
self.driver.find_element(By.XPATH,"//span[text()='高泽']").click()
sleep(10)
# 回退到主页
self.driver.back()
sleep(3)
self.driver.back()
# 刷新页面
self.driver.refresh()
草稿箱编辑功能
写信编辑一半
def test_HalfEdit01(self):
self.driver.implicitly_wait(10)
self.driver.find_element(By.XPATH, "/html/body/div[1]/nav/div[1]/ul/li[2]").click()
sleep(2)
#定位到iframe
self.driver.switch_to.frame(self.driver.find_element(By.XPATH, "//iframe[starts-with(@class,'APP-editor-iframe')]"))
self.driver.find_element(By.CLASS_NAME, "nui-scroll").send_keys("自动化测试")
# 回到当前标签
self.driver.switch_to.default_content()
# 点击保存草稿
self.driver.find_element(By.XPATH,"//span[text()='存草稿']").click()
print("成功保存")
# 回退到首页
self.driver.back()
# 刷新页面
self.driver.refresh()
写信编辑一半,回到首页
def test_HalfEdit02(self):
self.driver.implicitly_wait(10)
# 点击写信
self.driver.find_element(By.XPATH, "/html/body/div[1]/nav/div[1]/ul/li[2]").click()
sleep(2)
# 定位到iframe
self.driver.switch_to.frame(self.driver.find_element(By.XPATH, "//iframe[starts-with(@class,'APP-editor-iframe')]"))
self.driver.find_element(By.CLASS_NAME, "nui-scroll").send_keys("自动化测试")
# 回到当前标签
self.driver.switch_to.default_content()
# 点击首页
self.driver.find_element(By.XPATH,"/html/body/header/nav/div[1]/ul/li[1]").click()
sleep(3)
print("成功保存至邮箱")
# # 回退到首页
# self.driver.back()
# 刷新页面
self.driver.refresh()
删除草稿箱邮件
def test_HalfEdit03(self):
self.driver.implicitly_wait(10)
#点击草稿箱
self.driver.find_element(By.XPATH,"//span[text()='草稿箱']").click()
sleep(2)
# 选择邮件 ---
self.driver.find_element(By.XPATH,"/html/body/div[2]/div[1]/div/div/div/div/div[4]/div[2]/div/div[1]/label/span/b").click()
sleep(3)
# 点击删除草稿
self.driver.find_element(By.XPATH,"//span[text()='删除草稿']").click()
sleep(3)
print("草稿删除成功")
# 回退到首页
self.driver.back()
# 刷新页面
self.driver.refresh()
测试输出报告
自动生成html格式报告,其中报告里面附带用例执行日志明细
通过pytest --html=report.html
自动生成测试报告