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('退出登录失败')
相关推荐
0思必得07 小时前
[Web自动化] Selenium无头模式
前端·爬虫·selenium·自动化·web自动化
姚青&8 小时前
Pytest 参数化与调度执行
pytest
薯条不要番茄酱15 小时前
【测试实战篇】“发好论坛”接口自动化测试
python·功能测试·测试工具·单元测试·测试用例·pytest·测试覆盖率
weixin_4196583118 小时前
当Pytest遇见AI:基于Trae的接口测试用例全自动生成实践
人工智能·python·测试工具·测试用例·pytest·ai编程
hartyu20 小时前
纯PHP + Selenium + ChromeDriver方案实现原理,半自动化内容抓取
开发语言·selenium·php
0思必得020 小时前
[Web自动化] Selenium日期控件处理
前端·selenium·自动化·web自动化
佚泽2 天前
Java selenium 基本使用
java·开发语言·selenium
可可南木2 天前
3070文件格式--16--hosts文件
功能测试·测试工具·pcb工艺
Wpa.wk2 天前
局域网中两台win电脑传输文件
测试工具
西安同步高经理2 天前
标准时间间隔发生器:高精度时频计量的“基准标尺”时间合成器,脉冲发生器
测试工具