肖sir__ python自动化之selenium 封装(9.2)

python自动化之selenium 

一、自动化用例

案例:

用例1cms环境 登录

from selenium import webdriver
from  selenium.webdriver.support.select import Select
from  time import  *
dx=webdriver.Chrome()
dx.get("http://cms.duoceshi.cn/manage/login.do")
dx.find_element_by_name("userAccount").send_keys("admin")
dx.find_element_by_name("loginPwd").send_keys("123456")
sleep(2)
dx.find_element_by_xpath('//*[@id="loginBtn"]').click()

 用例2: cms环境中登录--退出

from selenium import webdriver
from  selenium.webdriver.support.select import Select
from  time import  *
dx=webdriver.Chrome()
dx.get("http://cms.duoceshi.cn/manage/login.do")
dx.find_element_by_name("userAccount").send_keys("admin")
dx.find_element_by_name("loginPwd").send_keys("123456")
sleep(2)
dx.find_element_by_xpath('//*[@id="loginBtn"]').click()

  用例3:cms 登录--系统管理--退出  流程

from selenium import webdriver
from  time import  *
dx=webdriver.Chrome()
dx.get("http://cms.duoceshi.cn/manage/login.do")
dx.find_element_by_name("userAccount").send_keys("admin")
dx.find_element_by_name("loginPwd").send_keys("123456")
sleep(2)
dx.find_element_by_xpath("html/body/div/div/form/div[3]/label/input").click()
sleep(2)
dx.find_element_by_xpath("html/body/div/div/form/div[4]/input[1]").click()
dx.maximize_window()
sleep(2)
dx.find_element_by_xpath('//*[@id="menu-system"]/dt').click()
sleep(3)
dx.find_element_by_link_text("退出").click()

  ==========================================================

二、将三条用例写入到一个类中管理

from selenium import webdriver
from  time  import  sleep
class   cms(object):
    def __init__(self):
        pass
    def dl(self):
        self.dx = webdriver.Chrome()
        self.dx.get("http://cms.duoceshi.cn/manage/login.do")
        self.dx.find_element_by_name("userAccount").send_keys("admin")
        self.dx.find_element_by_name("loginPwd").send_keys("123456")
        self.dx.find_element_by_xpath('//*[@id="loginBtn"]').click()
    def  xtgl(self):
        dx = webdriver.Chrome()
        dx.get("http://cms.duoceshi.cn/manage/login.do")
        dx.find_element_by_name("userAccount").send_keys("admin")
        dx.find_element_by_name("loginPwd").send_keys("123456")
        sleep(2)
        dx.find_element_by_xpath("html/body/div/div/form/div[3]/label/input").click()
        sleep(2)
        dx.find_element_by_xpath("html/body/div/div/form/div[4]/input[1]").click()
        dx.maximize_window()
        sleep(2)
        dx.find_element_by_xpath('//*[@id="menu-system"]/dt').click()
        sleep(3)
        dx.find_element_by_link_text("退出").click()

    def  tc(self):
        dx = webdriver.Chrome()
        dx.get("http://cms.duoceshi.cn/manage/login.do")
        dx.find_element_by_name("userAccount").send_keys("admin")
        dx.find_element_by_name("loginPwd").send_keys("123456")
        sleep(2)
        dx.find_element_by_xpath("html/body/div/div/form/div[3]/label/input").click()
        sleep(2)
        dx.find_element_by_xpath("html/body/div/div/form/div[4]/input[1]").click()
        dx.maximize_window()
        dx.find_element_by_link_text("退出").click()


if __name__ == '__main__':
    d=cms()
    # d.dl()
    # d.tc()
    # d.xtgl()

写在一个类的用例,代码有冗余部分,我们需要优化
from selenium import webdriver
from time import sleep
class cms(object):
def __init__(self):
self.dx = webdriver.Chrome()
self.dx.get("http://cms.duoceshi.cn/manage/login.do")
self.dx.maximize_window()
self.dx.implicitly_wait(3)
def dl(self):
self.dx.find_element_by_name("userAccount").send_keys("admin")
self.dx.find_element_by_name("loginPwd").send_keys("123456")
self.dx.find_element_by_xpath('//*[@id="loginBtn"]').click()
def xtgl(self):
self.dl( ) #先调用登录用例
sleep(2)
self.dx.find_element_by_xpath('//*[@id="menu-system"]/dt').click()# 点击系统管理
sleep(3)
self.dx.find_element_by_link_text("退出").click() #点击退出

def tc(self):
self.dl()# 先调用登录用例
sleep(3)
self.dx.find_element_by_link_text("退出").click() #点击退出
if __name__ == '__main__':
d=cms()
# d.dl()
# d.tc()
d.xtgl()
在封装的过程将登录的账号设置成变量:
from selenium import webdriver
from time import sleep
class cms(object):
def __init__(self):
self.dx = webdriver.Chrome()
self.dx.get("http://cms.duoceshi.cn/manage/login.do")
self.dx.maximize_window()
self.dx.implicitly_wait(3)
def dl(self,name,pwd):
self.dx.find_element_by_name("userAccount").send_keys(name)
self.dx.find_element_by_name("loginPwd").send_keys(pwd)
self.dx.find_element_by_xpath('//*[@id="loginBtn"]').click()
def xtgl(self,name,pwd):
self.dl(name,pwd) #先调用登录用例
sleep(2)
self.dx.find_element_by_xpath('//*[@id="menu-system"]/dt').click()# 点击系统管理
sleep(3)
self.dx.find_element_by_link_text("退出").click() #点击退出

def tc(self,name,pwd):
self.dl(name,pwd)# 先调用登录用例
sleep(3)
self.dx.find_element_by_link_text("退出").click() #点击退出
if __name__ == '__main__':
d=cms() #创建cms类的对象d
# d.dl("admin","123456") #通过对象调用登录
# d.tc("admin","123456") #通过对象调用退出
d.xtgl("admin","123456")#通过对象调用系统管理
在封装好的代码中进行断言:if断言、assert 断言
from selenium import webdriver
from time import sleep
class cms(object):
def __init__(self):
self.dx = webdriver.Chrome()
self.dx.get("http://cms.duoceshi.cn/manage/login.do")
self.dx.maximize_window()
self.dx.implicitly_wait(3)
def dl(self,name,pwd):
self.dx.find_element_by_name("userAccount").send_keys(name)
self.dx.find_element_by_name("loginPwd").send_keys(pwd)
self.dx.find_element_by_xpath('//*[@id="loginBtn"]').click()
print(self.dx.title)
t=self.dx.title
assert t=="过期更新"
# if t=="过期更新":
# print("ok")
# else:
# print("no")
def xtgl(self,name,pwd):
self.dl(name,pwd) #先调用登录用例
sleep(2)
self.dx.find_element_by_xpath('//*[@id="menu-system"]/dt').click()# 点击系统管理
sleep(3)
self.dx.find_element_by_link_text("退出").click() #点击退出

def tc(self,name,pwd):
self.dl(name,pwd)# 先调用登录用例
sleep(3)
self.dx.find_element_by_link_text("退出").click() #点击退出
if __name__ == '__main__':
d=cms() #创建cms类的对象d
d.dl("admin","123456") #通过对象调用登录
# d.tc("admin","123456") #通过对象调用退出
# d.xtgl("admin","123456")#通过对象调用系统管理

案例:
from selenium import webdriver
import time
class cms(object):
dx = webdriver.Chrome()
def __init__(self):
pass
def dl(self):
self.dx.get('http://cms.duoceshi.cn/manage/login.do')
time.sleep(1)
self.dx.maximize_window()
# 用户名、密码、勾选、登陆
time.sleep(1)
self.dx.find_element_by_id('userAccount').send_keys('admin')
time.sleep(1)
self.dx.find_element_by_id('loginPwd').send_keys('123456')
time.sleep(1)
self.dx.find_element_by_id('online').click()
time.sleep(1)
self.dx.find_element_by_id('loginBtn').click()
time.sleep(1)
def yfzx(self):
self.dl()
self.dx.find_element_by_xpath('//*[@id="menu-user"]/dt').click()
time.sleep(1)
self.dx.find_element_by_link_text('用户管理').click()
time.sleep(1)
def yftj(self):
self.yfzx()
ifk = self.dx.find_element_by_xpath('//*[@id="iframe_box"]/div[2]/iframe')
self.dx.switch_to.frame(ifk) # 进入框
self.dx.find_element_by_xpath('/html/body/div/div[2]/span[1]/a[2]').click()
# 用户姓名、性别、手机、邮箱、登陆账号、登陆密码、确认密码、确认
ifk2 = self.dx.find_element_by_xpath('//*[@id="xubox_iframe1"]')
self.dx.switch_to.frame(ifk2) # 进入框
self.dx.find_element_by_xpath('//*[@id="userName"]').send_keys('zn')
self.dx.find_element_by_xpath('//*[@id="sysUserForm"]/table/tbody/tr[2]/td/label[2]').click()
self.dx.find_element_by_xpath('//*[@id="user-tel"]').send_keys('18200001000')
self.dx.find_element_by_xpath('//*[@id="user-email"]').send_keys('1820000100@qq.com')
self.dx.find_element_by_xpath('//*[@id="userAccount"]').send_keys('zn')
self.dx.find_element_by_xpath('//*[@id="loginPwd"]').send_keys('123456')
self.dx.find_element_by_xpath('//*[@id="confirmPwd"]').send_keys('123456')
time.sleep(5)
self.dx.find_element_by_xpath('//*[@id="submitBtn"]').click()
self.dx.switch_to.default_content() # 退出框
# 根据用户名称-搜索-删除-确认删除
self.dx.switch_to.frame(ifk) # 进入框
time.sleep(2)
self.dx.find_element_by_xpath('//*[@id="searchValue"]').send_keys('zn')
time.sleep(1)
self.dx.find_element_by_xpath('//*[@id="searchBtn"]').click()
if __name__ == '__main__':
dx=cms()
# dx.dl()
# dx.yfzx()
dx.yftj()



posted @ 2024-04-01 17:31  xiaolehua  阅读(203)  评论(0编辑  收藏  举报