1. 程式人生 > >python UI自動化實戰記錄四:測試頁面1-pageobject

python UI自動化實戰記錄四:測試頁面1-pageobject

該部分記錄測試頁面1-IndexPage,所有首頁上的元素定位、操作、獲取屬性等方法都寫在該類中。

1 首頁類繼承自BasePage

2 首頁類第一部分寫的是所有的定位器

3 首頁類第二部分類的方法,包括提取頁面元素,頁面元素的操作、獲取屬性、驗證元素屬性正確性等

4 當用例中需要用到該頁面的某元素或操作時,補充方法到該類中,再在用例中呼叫

 

 

#coding:utf-8
"""
首頁
LOCATOR : 所有頁面元素的定位器
首頁頁面元素的定位和操作均寫在該類中
"""

from .basepage import BasePage
from selenium.webdriver.common.by import
By import logging;logging.basicConfig(level=logging.INFO) class IndexPage(BasePage): """ 首頁類 """ #LOCATOR _stylelist_LOCATOR = (By.CSS_SELECTOR, "#styleList li[data-style]") _current_LOCATOR = (By.CSS_SELECTOR,"#styleList .current") _latestInfoList_LOCATOR = (By.CSS_SELECTOR, "
ul#latestInfoList>li") _mystyle_name__LOCATOR = (By.ID, "myStyle_name_") _mystyle_name_LOCATOR = (By.ID, "myStyle_name") _style_desc_LCOATOR = (By.CLASS_NAME, "style-desc") _myStyle_remark_LOCATOR = (By.ID, "myStyle_remark") _read_strategy_LOCATOR = (By.CLASS_NAME, "read-strategy
") _style_desc_item_LOCATOR = (By.CLASS_NAME, "style-desc-item") _profit_percent_LOCATOR = (By.CLASS_NAME, "profit-percent") _style_name_LOCATOR = (By.CLASS_NAME,"style-name") _style_description_LOCATOR = (By.CLASS_NAME, "style-description") def open_index_page(self,sso): """開啟首頁""" domain = "http://測試域名" indexurl = '/index' url = domain+ indexurl + '?' + sso self.openpage(url) self.getscreenshot("Indexpage開啟頁面") def get_styleList(self): """獲取策略風格列表""" styleList = self.getelements(*IndexPage._stylelist_LOCATOR) #策略列表 return styleList def get_current_style(self): """獲取當前策略資訊""" current = self.getelement(*IndexPage._current_LOCATOR) #當前策略 self.current_style = current.get_attribute("data-style") #當前策略型別 self.current_name = current.text #當前策略名稱 def choose_style(self,styleindex): """選擇index = styleindex的策略風格""" stylelist = self.get_styleList() #獲取策略列表 try: style = stylelist[styleindex] self.select_style = style.get_attribute("data-style") # 選擇的策略型別 self.select_name = style.text #選擇的策略名稱 style.click() #點選策略 self.wait(1) self.getscreenshot("Indexpage策略資訊") self.get_current_style() #選擇之後獲取策略資訊 except Exception as msg: print(msg) def check_SelectEqCurrent(self): """驗證選擇的風格與顯示的風格是否一致""" if self.select_name == self.current_name and self.select_style == self.current_style: return True def get_level_info_list(self): """獲取右側顯示的策略資訊列表""" level_desc_items = self.getelements(*IndexPage._style_desc_item_LOCATOR) #取所有的策略詳情 level_info_list = [item for item in level_desc_items if item.is_displayed()] #取頁面顯示的策略詳情 return level_info_list def get_level_info(self,levelindex): """獲取策略資訊""" levellist = self.get_level_info_list() try: level = levellist[levelindex] #獲取策略資訊 percentM3 = self.getchild(level,*IndexPage._profit_percent_LOCATOR) name = self.getchild(level,*IndexPage._style_name_LOCATOR) description = self.getchild(level,*IndexPage._style_description_LOCATOR) read_strategy = self.getchild(level,*IndexPage._read_strategy_LOCATOR) self.percentM3 = float(percentM3.text[:-1])#獲取3個月收益 self.name = name.text #策略名稱 self.description = description.text #策略描述 self.read_strategy = read_strategy self.data_code = self.read_strategy.get_attribute("data-code") # 策略程式碼 except Exception as msg: print(msg) def get_mystyle(self): """獲取我的風格""" self.wait(0.5) myStyle_nameele = self.getelement(*IndexPage._mystyle_name_LOCATOR) self.myStyle_name = myStyle_nameele.text[1:-1] #我的風格 style_desc = self.getelement(*IndexPage._style_desc_LCOATOR) self.movetoelement(style_desc) #移動滑鼠至問號圖示 myStyle_intips = self.getelement_wait_presence(*IndexPage._mystyle_name__LOCATOR)#等元素顯示時識別 self.getscreenshot("Indexpage氣泡") self.myStylename_intips = myStyle_intips.text[:-1] self.mystyle_remark = self.getelement(*IndexPage._myStyle_remark_LOCATOR).text #tip裡的風格說明 def check_mystyle(self): """驗證風險等級頁面和tip是否一致""" self.get_mystyle() logging.info("使用者型別:{0},tips裡的使用者型別:{1}".format(self.myStyle_name,self.myStylename_intips)) if self.myStyle_name == self.myStylename_intips and self.myStyle_name in self.mystyle_remark: return True def check_default_ismystyle(self): """驗證預設策略與使用者風險等級匹配""" self.refresh() #重新整理,自動更新到使用者匹配的等級 self.wait(1) self.get_current_style() self.get_mystyle() logging.info("當前策略:{0},使用者風格:{1}".format(self.current_name,self.myStyle_name)) if self.current_name == self.myStyle_name: return True def clicklink_read_strategy(self,levelindex=0): """點選檢視策略""" self.get_level_info(levelindex) #選頁面顯示的第levelindex個策略 self.read_strategy.click() #點選檢視策略 self.wait(1) self.getscreenshot("Indexpage點選檢視策略跳轉到策略") return self.driver,self.data_code #跳轉到了策略頁面

 

the end!