Python-基於數據驅動模式的自動化測試框架搭建的的逐步實現(一)
-------無封裝,只有一個py文件
後續關於 <Python-基於數據驅動模式的自動化測試框架搭建的的逐步實現> 的博客例子全部基於:126郵箱登錄並新建聯繫人,這個例子來實現的;
最開始寫自動化腳本的時候,沒有封裝,就是一個py文件裏面跑所有的功能;
那麼,最開始是不用任何的封裝,來寫一個腳本,實現上述例子的功能,腳本如下:
#encoding=utf-8 import time import unittest import traceback from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.common.exceptions import TimeoutException, NoSuchElementException class Create(object): def __init__(self): self.driver=webdriver.Chrome(executable_path='c:\\Python27\\chromedriver') def create(self): wait=WebDriverWait(self.driver,10) try: url='http:\\www.126.com' self.driver.get(url) assert '126' in self.driver.page_source print u'page is opened' time.sleep(1) iframe=self.driver.find_element_by_id('x-URS-iframe') self.driver.switch_to_frame(iframe) wait.until(lambda x: x.find_element("xpath","//input[@name='email']")).clear() wait.until(lambda x: x.find_element("xpath", "//input[@name='email']")).send_keys('xxxxxxx') wait.until(lambda x: x.find_element("xpath",u"//*[text()='密碼']//following-sibling::input[2]")).clear() wait.until(lambda x: x.find_element("xpath", u"//*[text()='密碼']//following-sibling::input[2]")).send_keys('xxxxxxxxx') wait.until(lambda x:x.find_element("id","dologin")).click() time.sleep(1) self.driver.switch_to.default_content() wait.until(lambda x:x.find_element("xpath",u"//li[@title='點擊查看更多']")).click() wait.until(lambda x:x.find_element("xpath",u"// *[text() = '通訊錄']")).click() time.sleep(2) assert u"新建聯繫人" in self.driver.page_source print u"contants login in successed" wait.until(lambda x:x.find_element("xpath",u"//*[text()='新建聯繫人']")).click() wait.until(lambda x:x.find_element("id","input_N")).send_keys('gaokunkun') wait.until(lambda x: x.find_element("xpath", "//div[@id='iaddress_MAIL_wrap']//dl//dd//div//input")).send_keys('xxxxxxxxx.163.com') wait.until(lambda x: x.find_element("xpath", "//div[@id='iaddress_TEL_wrap']//dl//dd//div//input")).send_keys('xxxxxxxxx') wait.until(lambda x:x.find_element("xpath",u"//*[text()='確 定']")).click() time.sleep(1) assert "gaokun" in self.driver.page_source print 'add contact successed' except NoSuchElementException,e: print traceback.print_exc() except TimeoutException,e: print traceback.print_exc() except Exception,e: print traceback.print_exc() if __name__=='__main__': c=Create() c.create()