python-archieve-projects/1.26 自动化测试/new_auto_test.py

72 lines
2.8 KiB
Python
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import pandas as pd
from selenium import webdriver
import time
import csv
from selenium.webdriver.support import expected_conditions as EC
import os
driver = webdriver.Chrome(r"X:\test_website\chromedriver.exe")
driver.implicitly_wait(10)
driver.maximize_window()
def touch_case_files():
"""
得到所有测试文件
:return: 文件列表
"""
data_path = os.path.abspath('./cases')
case_files = []
for root, dirs, files in os.walk(data_path):
for file in files:
file = "./cases/" + file
case_files.append(file)
return case_files
def login_test(case_form, case_files):
result = []
xls = pd.read_excel(case_form)
for test_id in xls['id编号']:
for file in case_files:
if test_id in file:
print(test_id)
with open(file) as csv_file:
reader = csv.reader(file)
for row in reader:
driver.get('http://localhost')
# username
driver.find_element('name', 'username').send_keys(row[0]) # 输入用户名
# password
driver.find_element('name', 'userpwd').send_keys(row[1]) # 输入密码
# login button
driver.find_element('xpath', '/html/body/div[1]/div[3]/form/div[3]/input').click()
time.sleep(2)
# 测试
if EC.alert_is_present():
alert = driver.switch_to.alert
state = alert.text
alert.accept()
st = "账号:" + row[0] + " 密码:" + row[1] + " 测试不通过[" + state + "]"
print(st)
result.append(st)
else:
driver.get('http://localhost/admin_head.php')
account = driver.find_element('name', 'username').text
if account == row[0]:
st = "账号:" + row[0] + " 密码:" + row[1] + " 测试通过"
if test_id == 'login-001':
print("!!!!注意,该项与预设表格有出入!!!!")
result.append(st)
else:
st = "账号:" + row[0] + " 密码:" + row[1] + " 测试不通过[账户不正确]"
print(st)
result.append(st)
time.sleep(5)
return result
if __name__ == '__main__':
files = touch_case_files()
rest = login_test("./测试用例.xls", files)
print(rest)