Web UI自動化測試框架Seldom實戰 二
阿新 • • 發佈:2020-11-05
1.1、seldom 提供了8中定位方式,與Selenium保持一致。
- id_
- name
- class_name
- tag
- link_text
- partial_link_text
- css
- xpath
import seldom class YouTest(seldom.TestCase): def test_case(self): """a simple test case """ #開啟百度頁面 self.open("https://www.baidu.com") #根據id定位元素“kw”並輸入seldomself.type(id_="kw", text="seldom") #點選 self.click(css="#su") #斷言瀏覽器title是seldom_百度搜索 self.assertTitle("seldom_百度搜索")
8種定位用法:
self.type(id_="kw", text="seldom") self.type(name="wd", text="seldom") self.type(class_name="s_ipt", text="seldom") self.type(tag="input", text="seldom") self.type(link_text="hao123", text="seldom") self.type(partial_link_text="hao", text="seldom") self.type(xpath="//input[@id='kw']", text="seldom") self.type(css="#kw", text="seldom")
1.2、定位一組元素
有時候我們通過一種定位寫法不能找到單個元素,需要在一種定位方式中使用下標,在seldom中可以通過index
指定下標。
selenium中的寫法
driver.find_elements_by_tag_name("input")[7].send_keys("selenium")
seldom中的寫法,在seldom中不指定index
預設下標為0
。
self.type(tag="input", index=7, text="seldom")
2、seldom API
seldom 簡化了selenium中的API,在webdriver.py中以最簡單的方式操作Web頁面。所有API如下:
# Accept warning box. self.accept_alert() # Adds a cookie to your current session. self.add_cookie({'name' : 'foo', 'value' : 'bar'}) # Adds a cookie to your current session. cookie_list = [ {'name' : 'foo', 'value' : 'bar'}, {'name' : 'foo', 'value' : 'bar'} ] self.add_cookie(cookie_list) # Clear the contents of the input box. self.clear(css="#el") # It can click any text / image can be clicked # Connection, check box, radio buttons, and even drop-down box etc.. self.click(css="#el") # Mouse over the element. self.move_to_element(css="#el") # Click the element by the link text self.click_text("新聞") # Simulates the user clicking the "close" button in the titlebar of a popup window or tab. self.close() # Delete all cookies in the scope of the session. self.delete_all_cookies() # Deletes a single cookie with the given name. self.delete_cookie('my_cookie') # Dismisses the alert available. self.dismiss_alert() # Double click element. self.double_click(css="#el") # Execute JavaScript scripts. self.execute_script("window.scrollTo(200,1000);") # Setting width and height of window scroll bar. self.window_scroll(width=300, height=500) # Setting width and height of element scroll bar. self.element_scroll(css=".class", width=300, height=500) # get url. self.get("https://www.baidu.com") # Gets the text of the Alert. self.get_alert_text() # Gets the value of an element attribute. self.get_attribute(css="#el", attribute="type") # Returns information of cookie with ``name`` as an object. self.get_cookie() # Returns a set of dictionaries, corresponding to cookies visible in the current session. self.get_cookies() # Gets the element to display,The return result is true or false. self.get_display(css="#el") # Get element text information. self.get_text(css="#el") # Get window title. self.get_title() # Get the URL address of the current page. self.get_url() # Set browser window maximized. self.max_window() # Mouse over the element. self.move_to_element(css="#el") # open url. self.open("https://www.baidu.com") # Open the new window and switch the handle to the newly opened window. self.open_new_window(link_text="註冊") # Quit the driver and close all the windows. self.quit() # Refresh the current page. self.refresh() # Right click element. self.right_click(css="#el") # Saves a screenshots of the current window to a PNG image file. self.screenshots('/Screenshots/foo.png') ''' Constructor. A check is made that the given element is, indeed, a SELECT tag. If it is not, then an UnexpectedTagNameException is thrown. <select name="NR" id="nr"> <option value="10" selected="">每頁顯示10條</option> <option value="20">每頁顯示20條</option> <option value="50">每頁顯示50條</option> </select> ''' self.select(css="#nr", value='20') self.select(css="#nr", text='每頁顯示20條') self.select(css="#nr", index=2) # Set browser window wide and high. self.set_window(wide,high) # Submit the specified form. driver.submit(css="#el") # Switch to the specified frame. self.switch_to_frame(css="#el") # Returns the current form machine form at the next higher level. # Corresponding relationship with switch_to_frame () method. self.switch_to_frame_out() # Switches focus to the specified window. self.switch_to_window('main') # Operation input box. self.type(css="#el", text="selenium") # Implicitly wait.All elements on the page. self.wait(10) # Setting width and height of window scroll bar. self.window_scroll(width=300, height=500) # Returns the handle of the current window. self.current_window_handle # Returns the handle of the new window. self.new_window_handle # Returns the handles of all windows within the current session. self.window_handles #檔案上傳 # Single file upload filePath = r'C:\Users\admin\Desktop\文字文件.txt' self.type(css='.upload-button>input', text=filePath) # Multiple files upload filePath = r'C:\Users\admin\Desktop\第一文件.txt'+'\n'+r'C:\Users\admin\Desktop\第二文件.txt' self.type(css='.upload-button>input', text=filePath)
3、seldom 斷言
seldom 在case.py中提供了更加簡單的斷言方法。斷# 斷言標題是否等於"title"
self.assertTitle("title") # 斷言標題是否包含"title" self.assertInTitle("title") # 斷言URL是否等於 self.assertUrl("url") # 斷言URL是否包含 self.assertInUrl("url") # 斷言頁面是否存在“text” self.assertText("text") # 斷言警告是否存在"text" 提示資訊 self.assertAlertText("text")
4、用例失敗重跑&自動截圖
Web自動化測試常常因為各種原因導致用例失敗,而重跑機制可以進一步幫我們確定用例確實是失敗了。在seldom中設定失敗重跑非常簡單
import seldom class YouTest(seldom.TestCase): def test_case(self): """a simple test case """ self.open("https://www.baidu.com") self.type(id_="kw", text="seldom") self.click(css="#su_error") self.assertTitle("seldom_百度搜索") if __name__ == '__main__': """ rerun: 指定重跑的次數,預設為 0。 save_last_run: 是否儲存儲存最後一次執行結果,預設為False。 """ seldom.main(path="test_sample.py", rerun=3, save_last_run=False, )
檢視截圖,點選報告中的show
連結即可
5、seldom 資料驅動
5.1、通過@data() 裝飾器來引數化測試用例。
import seldom from seldom import data class BaiduTest(seldom.TestCase): #通過@data() 裝飾器來引數化測試用例。 @data([ (case1, 'seldom'), (case2, 'selenium'), (case3, 'unittest'), ]) def test_baidu(self, name, keyword): """ used parameterized test :param name: case name :param keyword: search keyword """ self.open("https://www.baidu.com") self.type(id_="kw", text=keyword) self.click(css="#su") self.assertTitle(keyword+"_百度搜索")
5.2、也可以針對測試類進行引數化, 通過data_class
方法:
import seldom from seldom import data_class @data_class( ("keyword", "assert_tile"), [("seldom", "seldom_百度搜索"), ("python", "python_百度搜索") ]) class YouTest(seldom.TestCase): def test_case(self): """a simple test case """ self.open("https://www.baidu.com") self.type(id_="kw", text=self.keyword) self.click(css="#su") self.assertTitle(self.assert_tile)
5.3、檔案引數化
parameterized.py中的file_data方法判定檔案格式,再通過conversion中的方法轉化不同檔案的引數為list。
csv_to_list() 方法csv檔案內容轉化為list。 @file_data("./data.xlsx", line=2) file: 指定csv檔案的路徑。 line: 指定從第幾行開始讀取,預設第1行。 excel_to_list() 方法excel檔案資料轉化為list。 @file_data("./data.xlsx", sheet="Sheet1", line=2) file : 指定excel檔案的路徑。 sheet: 指定excel的標籤頁,預設名稱為 Sheet1。 line : 指定從第幾行開始讀取,預設第1行。 json_to_list() 方法json檔案資料轉化為list。 @file_data("./data.json", key="login") file : 指定JSON檔案的路徑。 key: 指定字典的key,預設不指定解析整個JSON檔案。 yaml_to_list() 方法yaml檔案資料轉化為list。 @file_data("./data.yaml", key="login") file : 指定YAML檔案的路徑。 key: 指定字典的key,預設不指定解析整個YAML檔案。
例如:csv檔案引數化
import seldom from seldom import file_data class YouTest(seldom.TestCase): @file_data("./data.csv", line=2) def test_login(self, username, password): """a simple test case """ print(username) print(password)
同時還支援ddt
import seldom from ddt import ddt, file_data @ddt class YouTest(seldom.TestCase): @file_data("test_data.json") def test_case(self, word): """a simple test case """ self.open("https://www.baidu.com") self.type(id_="kw", text=word) self.click(css="#su") self.assertTitle(word + "_百度搜索") if __name__ == '__main__': seldom.main(path="test_sample.py", rerun=0, save_last_run=False, )