封装的实现与调用、
数据驱动:动态数据变量化,传参调用
AutoDriver文件夹
from selenium import webdriver
class AutoDriver001(object):
def __init__(self):
self.driver=webdriver.Firefox()
self.base_url='http://www.baidu.com'
def max_window(self):
self.driver.maiximize_window()
def bro_quit(self):
self.driver.quit()
def open_url(self,url):
self.driver.get(self.base_url+url)
def find_element(self,by,selector):
if by=='id':
return self.driver.find_element_by_id(selector)
elif by=='name': return self.driver.find_element_by_name(selector):
return self.driver.find_element_by_name(selector)
elif by=='class_name':
return self.driver.find_element_by_class_name(selector)
elif by=='xapath':
return self.driver.find_element_by_xpath(selector)
elif by=='link_text':
return self.driver.find_element_by_link_text(selector)
elif by=='partial_link_text':
return self.driver.find_element_by_partial_link_text(selector)
elif by=='css_selector':
return self.driver.find_element_by_css_selector(selector)
elif by=='tag_name':
return self.driver.find_element_by_tag_name(selector)
Data文件夹csv
xian,123456
wang,123456
Page文件夹
from 文件夹.文件名 import AutoDriver001
#基类(父类)
class BasePage(object):
def __init__(self,dd:AutoDriver001):
self.d1=dd
#登录按钮点击
def login_click(self):
self.d1.find_element('xpath','登录按钮路径').click()
#登录输入
def login_text(self,username,password):
self.d1.find_element('name','username').send_keys(username)
self.d1.find_element('name','password').send_keys(password)
self.d1.find_element('xpath','立即登录按钮路径').click()
#用户中心
def user_cen(self):
self.d1.find_element('xpath','用户中心按钮路径').click()
#子类(继承父类 可以使用父类的东西,父类不可使用子类的东西)
from 文件夹.文件名 import BasePage
from 文件夹.文件名 import AutoDriver001
class SubPage(BasePage):
def __init__(self,ddd:AutoDriver001):
super() .__init__(ddd)
#安全退出
def safe_quit(self):
self.d1.find_element('xpath','安全退出按钮路径').click()
Report文件夹
TestCase文件夹
调用
import unittest
import time
class Demo(unittest.TestCase):
def setUp(self):
self.d1.AutoDriver001()
self.dc.SubPage(self.d1)
self.d1.max_window()
def tearDown(self):
self.d1.bro_quit()
def test_login(self):
self.d1.open_url('/')
time.sleep(1)
csv_file=open(r'csv文件路径',mode='r',encoding='utf-8')
csv_data=csv.reader(csv_file)
for i in csv_data:
login_dict={
'username':i[0],
'password':i[1]
}
self.dc.login_click()
self.dc.login_text(login['username'],login['password'])
self.dc.user_cen()
self.dc.safe_quit()