Page Object是一种程序设计模式,将面向过程转变为面向对象(页面对象),将测试对象及单个的测试步骤封装在每个Page对象中,以page为单位进行管理。可以使代码复用,降低维护成本,提高程序可读性和编写效率。
#用page object思想实现百度首页的搜索和登陆功能
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
class BaiduPage():
url='http://www.baidu.com/'
username='XXXXXX'
password='******'
def __init__(self,driver):
self.driver=driver
def search(self,kw='51cto'):
self.driver.get(self.url)
time.sleep(2)
self.driver.find_element_by_id('kw').send_keys(kw)
self.driver.find_element_by_id('su').click()
time.sleep(2)
def login(self,username=None,password=None):
if username==None:
username=self.username
if password==None:
password=self.password
self.driver.get(self.url)
time.sleep(2)
self.driver.find_element_by_link_text('登录'.decode('utf-8')).click()
time.sleep(2)
self.driver.find_element_by_id('TANGRAM__PSP_8__userName').send_keys(self.username)
self.driver.find_element_by_id('TANGRAM__PSP_8__password').send_keys(self.password)
self.driver.find_element_by_id('TANGRAM__PSP_8__submit').click()
time.sleep(2)
def __del__(self):
pass
#self.driver.quit()
driver=webdriver.Chrome()
page=BaiduPage(driver)
page.login()
self.driver.quit()