Python-基于数据驱动模式的自动化测试框架搭建的的逐步实现(一)

Python-基于数据驱动模式的自动化测试框架搭建的的逐步实现(一)

                                                              -------无封装,只有一个py文件

后续关于 的博客例子全部基于: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()












你可能感兴趣的:(Python)