自动化用例
案例:http://cms.duoceshi.cn/manage/login.do
一、编写用例
(1)登录
from selenium import webdriver
from time import *
from selenium.webdriver.common.keys import Keys
dx=webdriver.Chrome()
url="http://cms.duoceshi.cn/manage/login.do"
dx.get(url)
dx.maximize_window()
dx.find_element_by_id("userAccount").send_keys("admin")
sleep(2)
dx.find_element_by_id("loginPwd").send_keys("123456")
sleep(2)
dx.find_element_by_id("loginBtn").click()
(2)登录=用户中心=用户管理
from selenium import webdriver
from time import *
from selenium.webdriver.common.keys import Keys
dx=webdriver.Chrome()
url="http://cms.duoceshi.cn/manage/login.do"
dx.get(url)
dx.maximize_window()
dx.find_element_by_id("userAccount").send_keys("admin")
sleep(2)
dx.find_element_by_id("loginPwd").send_keys("123456")
sleep(2)
dx.find_element_by_id("loginBtn").click()
sleep(2)
dx.find_element_by_xpath('//*[@id="menu-user"]/dt/i').click()
sleep(2)
dx.find_element_by_link_text("用户管理").click()
(3)登录系统管理栏目设置
from selenium import webdriver
from time import *
from selenium.webdriver.common.keys import Keys
dx=webdriver.Chrome()
url="http://cms.duoceshi.cn/manage/login.do"
dx.get(url)
dx.maximize_window()
dx.find_element_by_id("userAccount").send_keys("admin")
sleep(2)
dx.find_element_by_id("loginPwd").send_keys("123456")
sleep(2)
dx.find_element_by_id("loginBtn").click()
sleep(2)
dx.find_element_by_xpath('//*[@id="menu-system"]/dt/i').click()
sleep(2)
dx.find_element_by_link_text("栏目设置").click()
二、封装
(1)每个用例都是独立(运行前调用登录)
from selenium import webdriver
from time import *
class Cms(object):
def init(self):
pass
def dl(self):
self.dx=webdriver.Chrome()
url="http://cms.duoceshi.cn/manage/login.do"
self.dx.get(url)
self.dx.maximize_window()
self.dx.find_element_by_id("userAccount").send_keys("admin")
sleep(2)
self.dx.find_element_by_id("loginPwd").send_keys("123456")
sleep(2)
self.dx.find_element_by_id("loginBtn").click()
sleep(3)
def yhgl(self):
c=self.dl()
self.dx.find_element_by_xpath('//[@id="menu-user"]/dt/i').click()
sleep(2)
self. dx.find_element_by_link_text("用户管理").click()
def lmcx(self):
c = self.dl()
self.dx.find_element_by_xpath('//[@id="menu-system"]/dt/i').click()
sleep(2)
self.dx.find_element_by_link_text("栏目设置").click()
if name == 'main':
d=Cms()
# d.dl()
# d.yhgl()
d.lmcx()
(2)每个用例执行前都会运行构造函数下登录
from selenium import webdriver
from time import *
class Cms(object):
def init(self):
self.dx=webdriver.Chrome()
url="http://cms.duoceshi.cn/manage/login.do"
self.dx.get(url)
self.dx.maximize_window()
self.dx.find_element_by_id("userAccount").send_keys("admin")
sleep(2)
self.dx.find_element_by_id("loginPwd").send_keys("123456")
sleep(2)
self.dx.find_element_by_id("loginBtn").click()
sleep(3)
def yhgl(self):
self.dx.find_element_by_xpath('//[@id="menu-user"]/dt/i').click()
sleep(2)
self. dx.find_element_by_link_text("用户管理").click()
def lmcx(self):
self.dx.find_element_by_xpath('//[@id="menu-system"]/dt/i').click()
sleep(2)
self.dx.find_element_by_link_text("栏目设置").click()
if name == 'main':
d=Cms()
# d.yhgl()
d.lmcx()
(3)
from selenium import webdriver
from time import *
class Cms(object):
def init(self):
self.dx = webdriver.Chrome()
url = "http://cms.duoceshi.cn/manage/login.do"
self.dx.get(url)
self.dx.maximize_window()
def dl(self):
self.dx.find_element_by_id("userAccount").send_keys("admin")
sleep(2)
self.dx.find_element_by_id("loginPwd").send_keys("123456")
sleep(2)
self.dx.find_element_by_id("loginBtn").click()
sleep(3)
def yhgl(self):
c=self.dl()
self.dx.find_element_by_xpath('//[@id="menu-user"]/dt/i').click()
sleep(2)
self. dx.find_element_by_link_text("用户管理").click()
def lmcx(self):
c = self.dl()
self.dx.find_element_by_xpath('//[@id="menu-system"]/dt/i').click()
sleep(2)
self.dx.find_element_by_link_text("栏目设置").click()
if name == 'main':
d=Cms()
d.dl()
d.yhgl()
d.lmcx()
标签:self,selenium,element,用例,sleep,dx,id,编写,find
From: https://www.cnblogs.com/hutongxue/p/18423084