selenium+pytest 自动化测试实战项目(登录升级版)

介绍
复制代码
###### 测试的系统:
复制代码
###### 技术:selenium,pytest
复制代码
###### 测试的功能:登录,退出登录。
测试用例
用例编号 主模块 子模块 前置条件 测试步骤 预期结果 实际结果
Login_01 登录 登录 已注册 1.输入正确的用户名,不输入密码。2.点击登录 登录不成功,有相关的提示 登录不成功,提示:请输入密码
Login_02 登录 登录 已注册 1.不输入用户名,输入正确的密码。2.点击登录 登录不成功,有相关的提示 登录不成功,提示:请输入用户名
Login_03 登录 登录 已注册 1.输入正确的用户名,输入错误的密码。2.点击登录 登录不成功,有相关的提示 登录不成功,提示:登录失败 : 用户名或者密码错误
Login_04 登录 登录 已注册 1.输入错误的用户名,输入正确的密码。2.点击登录 登录不成功,有相关的提示 登录不成功,提示:登录失败 : 用户名或者密码错误
Login_05 登录 登录 已注册 1.输入错误的用户名和错误的密码。2.点击登录 登录不成功,有相关的提示 登录不成功,提示:登录失败 : 用户名或者密码错误
Login_06 登录 登录 已注册 1.输入正确的用户名和正确的密码。2.点击登录 登录成功 登录成功
LogOut_07 退出登录 退出登录 已登录 1.点击退出登录按钮 退出登录成功 退出登录成功
自动化测试程序
python 复制代码
#configuration.py
from selenium import webdriver

url = 'http://127.0.0.1/mgr/sign.html'
chrome = webdriver.Chrome()
python 复制代码
#LoginSuccessfully.py
"""
@author:zulnger(丽格)
"""
from selenium import webdriver
from time import sleep
import pytest
from configuration import driver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import NoAlertPresentException

def Login(Uname,pw):
    # 打开系统
    driver.chrome.get('http://127.0.0.1/mgr/sign.html')
    sleep(3)

    # Login_01
    # 定位元素
    userName = driver.chrome.find_element(By.ID, 'username')
    userName.clear()
    userName.send_keys(f'{Uname}')
    sleep(2)

    password = driver.chrome.find_element(By.ID, 'password')
    password.clear()
    password.send_keys(f'{pw}')
    sleep(2)

    #点击登录
    submit = driver.chrome.find_element(By.XPATH,"//div[@class='col-xs-12']/button")
    submit.click()
    sleep(3)
python 复制代码
#test_login.py
"""
@author:zulnger(丽格)
"""
from selenium import webdriver
from time import sleep
import pytest
from configuration import driver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from Login.LoginSuccessfully import *

class Test_Login:
    def test_Login_01(self):
        #Login_0
        log = Login('byhy','')

        # Confirm 弹窗
        try:
            Confirm = driver.chrome.switch_to.alert
            print("登录失败!!!")
            text = Confirm.text
            print('登录失败提示语  ------>  ', text)
            Confirm.accept()
            sleep(3)

        except NoAlertPresentException:
            print("登录成功~")
            print("当前没有 alert,跳过处理")


        assert text == '请输入密码'

    def test_Login_02(self):
        log = Login('','byhy')

        # Confirm 弹窗
        try:
            Confirm = driver.chrome.switch_to.alert
            print("登录失败!!!")
            text = Confirm.text
            print('登录失败提示语  ------>  ', text)
            Confirm.accept()
            sleep(3)

        except NoAlertPresentException:
            print("登录成功~")
            print("当前没有 alert,跳过处理")

        assert  text == '请输入用户名'

    def test_Login_03(self):
        log = Login('byhy', '8888888')

        # Confirm 弹窗
        try:
            Confirm = driver.chrome.switch_to.alert
            text = Confirm.text
            print('登录失败提示语  ------>  ', text)
            Confirm.accept()
            sleep(3)

        except NoAlertPresentException:
            print("登录成功~")
            print("当前没有 alert,跳过处理")

        assert text == '登录失败 : 用户名或者密码错误'

    def test_Login_04(self):
        log = Login('byh', '88888888')

        # Confirm 弹窗
        try:
            Confirm = driver.chrome.switch_to.alert
            text = Confirm.text
            print('登录失败提示语  ------>  ', text)
            Confirm.accept()
            sleep(3)

        except NoAlertPresentException:
            print("登录成功~")
            print("当前没有 alert,跳过处理")

        assert text == '登录失败 : 用户名或者密码错误'

    def test_Login_05(self):
        log = Login('by', '99999999')

        # Confirm 弹窗
        try:
            Confirm = driver.chrome.switch_to.alert
            text = Confirm.text
            print('登录失败提示语  ------>  ', text)
            Confirm.accept()
            sleep(3)

        except NoAlertPresentException:
            print("登录成功~")
            print("当前没有 alert,跳过处理")

        assert text == '登录失败 : 用户名或者密码错误'

    def test_Login_06(self):
        log = Login('byhy','88888888')
        sleep(2)

        logo_text = driver.chrome.find_element(By.CSS_SELECTOR,".main-header .logo-lg").text
        assert logo_text == '白月黑羽SMS'
python 复制代码
#test_loout.py
from time import sleep
import pytest
from configuration import driver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from Login.LoginSuccessfully import *
from selenium.common.exceptions import TimeoutException

def test_logout():
    Login('byhy','88888888')

    try:
        # 例:退出按钮、用户昵称、头像......
        userAvatarElement = driver.chrome.find_element(By.XPATH, '//div[@class="navbar-custom-menu"]/ul/li[2]/a')
        userAvatarElement.click()
        sleep(2)

        logOutElement = driver.chrome.find_element(By.XPATH, '//div[@class="pull-right"]/a')
        logOutElement.click()
        sleep(2)

        print('退出登录成功!!!!!')



    except TimeoutException:
        print('退出登录失败')
相关推荐
Hy行者勇哥22 分钟前
软件测试完整体系
测试工具
小白上线*^_^*2 小时前
Pytest 自动化测试框架速通指南(二)
软件测试·pytest·python测试框架·ai测试基础
TunerT_TQ4 小时前
Valhalla 静态工程审阅 #012|Hertz 源码证据驱动评测【大厂开源基础设施特辑】
测试工具·微服务·开源·github·字节跳动·cloudwego·http框架
冷凝娇20 小时前
【Postman】2026总结
测试工具·postman
天才测试猿20 小时前
实例介绍:Unittest框架及自动化测试实现流程
自动化测试·软件测试·python·selenium·测试工具·职场和发展·测试用例
TunerT_TQ21 小时前
Valhalla 静态工程审阅 #009|Continue 源码证据驱动评测【大厂开源基础设施特辑】
vscode·测试工具·开源·llm·github·jetbrains·ai编程助手
Logintern091 天前
pytest的AST 断言重写
pytest
Luminbox紫创测控2 天前
AM0太阳模拟器如何精准模拟太空环境:提升地面测试精度
测试工具·安全性测试·测试标准
我的xiaodoujiao2 天前
快速学习Python基础知识详细图文教程14--模块
开发语言·python·学习·测试工具
东舟技术2 天前
AI全链路测试智能体上线啦!
人工智能·功能测试·测试工具·自动化