自动化框架搭建(一)

更新时间:2023-06-28 16:59:50 阅读: 评论:0

⾃动化框架搭建(⼀)基本路径如图:
然后贴⼀下代码:
#driver.py
from lenium import webdriver
from lenium.webdriver import Remote
#启动浏览器驱动
def browr():
dc = {'browrName':'chrome'}
driver=Remote(command_executor='127.0.0.1:4444/wd/hub',
desired_capabilities =dc
)
return driver
if __name__=="__main__":
dr=browr()
罗马军队<("/")
dr.quit()
#function.py
from lenium import webdriver
import os
#截图函数
def inrt_img(driver,file_name):
ba_dir=os.path.dirname(os.path.dirname(__file__))
ba_dir=str(ba_dir)
ba_dir=place('\\','/')
ba=ba_dir+'/report/image/'+file_name
<_reenshot_as_file(file_name)
if __name__=='__main__':
driver=webdriver.Chrome()
<('/')
inrt_img(driver,'baidu.jpg')
driver.quit()
中国驾考网
#myunit.py
from lenium import webdriver
from driver import browr
import unittest
import os
class MyTest(unittest.TestCa):
def tUp(lf):
lf.driver=browr()
lf.driver.implicitly_wait(10)
lf.driver.maximize_window()
def tearDown(lf):
lf.driver.quit()
#ba.py
class Page(object):
'''
页⾯基础类,⽤于所有页⾯继承
'''
bbs_url='。。。登陆⽹址。。。'
def __init__(lf, lenium_driver, ba_url=bbs_url, parent=None):
lf.ba_url = ba_url
lf.driver = lenium_driver
lf.timeout = 30
lf.parent = parent
def _oprn(lf,url):
url = lf.ba_url+url
(url)
_page() , 'Did not land on %s'%url
def find_element(lf,*loc):
return lf.driver.find_element(*loc)
def find_elements(lf,*loc):
return  lf.driver.find_elements(*loc)
def open(lf):
lf._oprn(lf.url)
def on_page(lf):
return lf.driver.current_url == ( lf.ba_url + lf.url )
def script(lf,src):
return ute_script(src)
#login_ba.py
from action_chains import ActionChains from by import By
from lenium import webdriver
from .ba import Page
from time import sleep
class Login(Page):
'''
⽤户登录界⾯
'''
url='/'
#Action
# bbs_login_ur_loc=(By.XPATH,"//div[@title='海雾-镇守']")
# bbs_login_button_loc=(By.CLASS_NAME,"roll-nav")
ur_login_success_loc = (By.CLASS_NAME, "font-bold")
login_null_loc = (By.CSS_SELECTOR,'[type*=red]')
# def bbs_login(lf):
#    lf.find_element(*lf.bbs_login_ur_loc).click()
#    sleep(1)
#    lf.find_element(*lf.bbs_login_button_loc).click()
login_urname_loc=(By.ID,"LoginName")
login_password_loc = (By.ID, "PassWord")
login_button_loc = (By.ID, "submit")
#登录⽤户名
def login_urname(lf,urname):
lf.find_element(*lf.login_urname_loc).nd_keys(urname)    # 登录密码
def login_password(lf, password):
lf.find_element(*lf.login_password_loc).nd_keys(password)    # 点击登录按钮
def login_button(lf):
lf.find_element(*lf.login_button_loc).click()
#定义统⼀登录⼊⼝
def ur_login(lf,urname="",password=""):
'''
⽤获取到的⽤户名密码登录
:param urname:
落翅女的黄昏
:param password:梦见被人骂
:return:
'''
lf.open()
高差法
#lf.bbs_login()
lf.login_urname(urname)
lf.login_password(password)
lf.login_button()
sleep(1)
#⽤户名错误提⽰
def ur_error_hint(lf):
sleep(3)
return lf.switch_to_alert().text
#⽤户名为空提⽰
def ur_error_null(lf):
sleep(3)
return  lf.find_element(*lf.login_null_loc).text
# 密码错误提⽰
def pawd_erroe_hint(lf):
sleep(3)
return lf.switch_to_alert().text
#密码为空提⽰
def pawd_error_null(lf):
sleep(3)
return  lf.find_element(*lf.login_null_loc).text
#登录成功⽤户名
def ur_login_success(lf):
return  lf.find_element(*lf.ur_login_success_loc).text
#login_sta.py
from time import sleep
供电所长
import unittest, random, sys
sys.path.append("./model")
sys.path.append("./page_obj")逃走的英文
from model import myunit, function,driver
from page_obj.login_page import Login
class loginTest(myunit.MyTest):
'''
登录测试
'''
#测试⽤户登录
def ur_login_verify(lf,urname="",password=""):
Login(lf.driver).ur_login(urname,password)
def test_login1(lf):
'''
⽤户名,密码为空
:return:
'''
lf.ur_login_verify()
po = Login(driver)
lf.asrtEqual(po.ur_error_null(),"请输⼊⽤户名!")
lf.asrtEqual(po.pawd_erroe_null(),"请输⼊密码!")
function.inrt_img(lf.driver,"ur_pawd_empty.jpg")
def test_login2(lf):
'''
⽤户名正确,密码为错误
:return:
'''
lf.ur_login_verify(urname="159********",password="111111")        po = Login(lf.driver)
lf.asrtEqual(po.pawd_erroe_hint(),"错误的帐号或密码")
function.inrt_img(lf.driver,"pawd_empty.jpg")
def test_login3(lf):
'''
⽤户名为错误,密码正确
:
return:
'''
lf.ur_login_verify(urname='159********',password="123456")        po = Login(lf.driver)
lf.asrtEqual(po.ur_error_hint(), "错误的帐号或密码")
function.inrt_img(lf.driver,"ur_empty.jpg")
def test_login4(lf):
'''
⽤户名和密码不匹配
:return:
'''
lf.ur_login_verify(urname='159********', password="123456")        po = Login(lf.driver)
lf.asrtEqual(po.ur_error_hint(),"错误的帐号或密码")
function.inrt_img(lf.driver,"ur_pawd_error.jpg")
def test_login5(lf):
'''
⽤户名、密码正确
:return:
'''
lf.ur_login_verify(urname="159********",password="123456")        sleep(5)
po = Login(lf.driver)
lf.asrtEqual(po.ur_login_success(),"159********")
function.inrt_img(lf.driver,"login_success.jpg")
if __name__=="__main__":
unittest.main()
#run_bbs_test.py
from HTMLTestRunner import HTMLTestRunner
from import MIMEText
from email.header import Header
import smtplib
import unittest
import time
import os
#=================定义发送邮件===========================
def nd_main(file_new):
f = open(file_new , 'rb')
mail_body = f.read()
f.clo()
msg = MIMEText(mail_body , 'html' , 'utf-8' )
msg['Subject'] = Header ('⾃动化测试报告' , 'utf-8')
smtp = smtplib.SMTP()
smtp.login( "" , "123456" )
smtp.ndmail("" , "" , msg.as_string())
smtp.quit()
print("email has out ! ")
#=================查找最新测试报告===========================
def new_report(testreport):
lists = os.listdir(testreport)
lists.sort(key=lambda fn: ime(testreport + "\\" + fn))
file_new = os.path.join(testreport , lists[-1])
print(file_new)
return file_new
if __name__ == "__main__":
now = time.strftime("%Y-%m-%d %H_%M_%S")
filename = "./bbs/report/" + now + "result.html"
fp = open(filename , 'wb')
runner = HTMLTestRunner(stream = fp , title = "魅族社区⾃动化测试报告" , description = "环境: Windows 7 浏览器: Chrome")
discove = unittest.defaultTestLoader.discover("./bbs/test_ca" , pattern="*_sta.py")
runner.run(discove)
fp.clo() # 关闭⽣成的报告
file_path = new_report("./bbs/report") #查找新⽣的报告
nd_main(file_path) #调⽤发邮件模块
⽬前未解决问题:
        1.登陆账户或者密码为空时,提⽰语的捕捉尚未成功
        2.密码或账户错误时,弹出的提⽰框的捕捉,驱动的带⼊问题
        3.整体框架尚未吃透,理解还有⽋缺
        4.邮件模块未做调整,还存在问题
        5.分布式搭建⽆法完成,不加上hub和node节点,单纯使⽤lenium_rver是可以成功的
⽬前已解决问题:
        1.分布式⽆法运⾏,解决⽅式:删去hub和node节点,直接运⾏在本机,待代码调试完成后再回头研究
尿痛怎么办        2.参考的魅族社区设计已变更,根据测试⽹站的相应调整已结束,账号密码正确的成功登录已完成
        3.web端元素的捕捉熟练度⼤⼤增长,find_element_by和BY.⽤法已经熟然于⼼
版本⼆⽬标:
        1.整体运⾏成功不报错

本文发布于:2023-06-28 16:59:50,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/82/1060254.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:密码   登录   完成   错误   调整
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图