Selenium3與Python3實戰Web自動化測試框架【日記1】

lee_lgw發表於2021-09-09

很多人被這門課的pom模型弄得雲裡霧裡的,根據自己的整理,總結了本課程主要的類和物件的呼叫順序:

1、資料驅動:LocalElement.ini

[RegisterElement]
user_email=id>register_email
user_email_error=id>register_email-error
user_name=id>register_nickname
user_name_error=id>register_nickname-error
password=id>register_password
password_error=id>register_password-error
code_image=id>getcode_num
code_text=id>captcha_code
code_text_error=id>captcha_code-error
register_button=id>register-btn

2、read_ini.py 讀取LocalElement.ini資料資訊:

#coding=utf-8
import configparser
class ReadIni(object):
    def __init__(self,file_name=None,node=None):
        if file_name == None:
            file_name = "E:/Teacher/Imooc/SeleniumPython/config/LocalElement.ini"
        if node == None:
            self.node = "RegisterElement"
        else:
            self.node = node
        self.cf = self.load_ini(file_name)
    #載入檔案
    def load_ini(self,file_name):
        cf = configparser.ConfigParser()
        cf.read(file_name)
        return cf
    #獲取value得值
    def get_value(self,key):
        data = self.cf.get(self.node,key)
        return data
#if __name__ == '__main__':
#    read_init = ReadIni()
#    print(read_init.get_value('user_name'))

3、find_element.py 【呼叫read_ini.py,返回元素定位物件】

#coding=utf-8
from util.read_ini import ReadIni
from log.user_log import UserLog
class FindElement(object):
    def __init__(self,driver):
        self.driver = driver
        get_user_log = UserLog()
        self.logger = get_user_log.get_log()
    def get_element(self,key):
        read_ini = ReadIni()
        data = read_ini.get_value(key)
        by = data.split('>')[0]
        value = data.split('>')[1]
        self.logger.info("定位方式:"+by+"--->定位值:"+value)
        try:
            if by == 'id':
                return self.driver.find_element_by_id(value)
            elif by == 'name':
                return self.driver.find_element_by_name(value)
            elif by == 'className':
                return self.driver.find_element_by_class_name(value)
            else:
                return self.driver.find_element_by_xpath(value)
        except:
            return None

4、register_page.py【呼叫find_element.py,返回元素定位物件】

#coding=utf-8
from base.find_element import FindElement
class RegisterPage(object):
    def __init__(self,driver):
        self.fd = FindElement(driver)
    
    #獲取郵箱元素
    def get_email_element(self):
        return self.fd.get_element("user_email")

    #獲取使用者名稱元素
    def get_username_element(self):
        return self.fd.get_element("user_name")
    #獲取密碼元素
    def get_password_element(self):
        return self.fd.get_element("password")
    #獲取驗證碼元素
    def get_code_element(self):
        return self.fd.get_element("code_text")
    #獲取註冊按鈕元素
    def get_button_element(self):
        return self.fd.get_element("register_button")
    #獲取郵箱錯誤元素
    def get_email_error_element(self):
        return self.fd.get_element("user_email_error")
    #獲取使用者名稱錯誤元素
    def get_name_error_element(self):
        return self.fd.get_element("user_name_error")
    #獲取密碼錯誤元素
    def get_password_error_element(self):
        return self.fd.get_element("password_error")
    #獲取驗證碼錯誤元素
    def get_code_error_element(self):
        return self.fd.get_element("code_text_errorr")

5、register_handle.py 【呼叫register_page.py,透過元素物件輸入值或者返回資料】

#coding=utf-8
from page.register_page import RegisterPage
import sys
from util.get_code import GetCode
from log.user_log import UserLog

class RegisterHandle(object):
    def __init__(self,driver):
        self.driver = driver
        self.register_p = RegisterPage(self.driver)
        get_user_log = UserLog()
        self.loger = get_user_log.get_log()
    #輸入郵箱
    def send_user_email(self,email):
        self.loger.info("輸入的郵箱值是:"+email)
        self.register_p.get_email_element().send_keys(email)
    
    #輸入使用者名稱
    def send_user_name(self,username):
        self.loger.info("輸入的使用者名稱是:"+username)
        self.register_p.get_username_element().send_keys(username)

    #輸入密碼
    def send_user_password(self,password):
        self.loger.info("輸入的密碼是:"+password)
        self.register_p.get_password_element().send_keys(password)
    
    #輸入驗證碼
    def send_user_code(self,file_name):
        get_code_text = GetCode(self.driver)
        code = get_code_text.code_online(file_name)
        self.register_p.get_code_element().send_keys(code)
    
    #獲取文字資訊
    def get_user_text(self,info,user_info):
        try:
            if info == 'user_email_error':
                text = self.register_p.get_email_error_element().text
            elif info == 'user_name_error':
                text = self.register_p.get_name_error_element().text
            elif info == 'password_error':
                text = self.register_p.get_password_element().text
            else:
                text = self.register_p.get_code_element().text
        except:
            text = None     
        return text
    #點選註冊按鈕
    def click_register_button(self):
        self.register_p.get_button_element().click()
    
    #獲取註冊按鈕文字
    def get_register_text(self):
        return self.register_p.get_button_element().text

6、register_business.py 【呼叫register_handle.py的send_user_XXX和get_user_text方法】
– 先呼叫send_user_XXX將case資料傳給register_handle處理,再呼叫get_user_text檢視case是否正確

#coding=utf-8
from handle.register_handle import RegisterHandle
class RegisterBusiness:
    def __init__(self,driver):
        self.register_h = RegisterHandle(driver)

    def user_base(self,email,name,password,file_name):
        self.register_h.send_user_email(email)
        self.register_h.send_user_name(name)
        self.register_h.send_user_password(password)
        self.register_h.send_user_code(file_name)
        self.register_h.click_register_button()
    
    def register_succes(self):
        if self.register_h.get_register_text() == None:
            return True
        else:
            False

'''
    執行操作
    def login_email_error(self,email,name,password,file_name):
        self.user_base(email,name,password,file_name)   
        if self.register_h.get_user_text('email_error',"請輸入有效的電子郵件地址") == None:
            #print("郵箱檢驗不成功")
            return True
        else:
            return False
'''
    def register_function(self,email,username,password,file_name,assertCode,assertText):
        self.user_base(email,username,password,file_name) #輸入郵箱(假設錯誤的郵箱),user_email_error就會有值
        if self.register_h.get_user_text(assertCode,assertText) == None: # 表示text沒值
            #print("郵箱輸入正確,檢驗不成功")
            return True
        else:
            #print("郵箱輸入錯誤,成功檢驗")
            return False # 表示text有值,郵箱輸入錯誤被校驗住了 
'''            
    #name錯誤
    def login_name_error(self,email,name,password,file_name):
        self.user_base(email,name,password,file_name)
        if self.register_h.get_user_text('user_name_error',"字元長度必須大於等於4,一箇中文字算2個字元") == None:
            #print("使用者名稱檢驗不成功")
            return True
        else:
            return False
    
    #密碼錯誤
    def login_password_error(self,email,name,password,file_name):
        self.user_base(email,name,password,file_name)
        if self.register_h.get_user_text('password_error',"最少需要輸入 5 個字元") == None:
            #print("密碼檢驗不成功")
            return True
        else:
            return False

    #驗證碼錯誤
    def login_code_error(self,email,name,password,file_name):
        self.user_base(email,name,password,file_name)
        if self.register_h.get_user_text('code_text_error',"驗證碼錯誤") == None:
            #print("驗證碼檢驗不成功")
            return True
        else:
            return False
'''        

7、first_ddt_case.py 【呼叫register_business.py的register_function 方法】

#coding=utf-8
import ddt
import unittest
import sys
sys.path.append('E:\Teacher\Imooc\SeleniumPython')
import traceback
from business.register_business import RegisterBusiness
from selenium import webdriver
import HTMLTestRunner
from util.excel_util import ExcelUtil
import os
from log.user_log import UserLog
import time
ex = ExcelUtil() # 獲取操作Excel的物件
data = ex.get_data() # 根據物件呼叫獲取資料的方法獲取資料,存放在data裡
log = UserLog()
logger = log.get_log()
file_name = "E:/Teacher/Imooc/SeleniumPython/Image/test001.png"
#郵箱、使用者名稱、密碼、驗證碼、錯誤資訊定位元素、錯誤提示資訊
@ddt.ddt
class FirstDdtCase(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Chrome()
        self.driver.get('')
        self.login = RegisterBusiness(self.driver)


    def tearDown(self):
        time.sleep(2)
        #if sys.exc_info()[0]:
        for method_name,error in self._outcome.errors:
              if error:
                  case_name = self._testMethodName
                  file_path = os.path.join(os.getcwd()+"/report/"+case_name+".png")
                  self.driver.save_screenshot(file_path)
        #print("這個是case的後置調鍵1")
        self.driver.close()
    '''
    @ddt.data(
            ['12','Mushishi01','111111','code','user_email_error','請輸入有效的電子郵件地址'],
            ['@qq.com','Mushishi01','111111','code','user_email_error','請輸入有效的電子郵件地址'],
            ['12@qq.com','Mushishi01','111111','code','user_email_error','請輸入有效的電子郵件地址']
        )
    @ddt.unpack
    '''
    @ddt.data(*data) #data裡存放著Excel裡的資料
    def test_register_case(self,data):
	    #透過list賦值方式,將Excel裡資料賦值到各個引數裡
        email,username,password,self.file_name,assertCode,assertText = data  	
        email_error = self.login.register_function(email,username,password,self.file_name,assertCode,assertText)
        self.assertFalse(email_error,"測試失敗")

if __name__ == '__main__':
    file_path = os.path.join(os.getcwd()+"/report/"+"first_case1.html")
    f = open(file_path,'wb')
    suite = unittest.TestLoader().loadTestsFromTestCase(FirstDdtCase)
    runner = HTMLTestRunner.HTMLTestRunner(stream=f,title="This is first report1",description=u"這個是我們第一次測試報告1",verbosity=2)
    runner.run(suite)

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/1020/viewspace-2826242/,如需轉載,請註明出處,否則將追究法律責任。

相關文章