seldom-潛水

對比unittest斷言更加好用

對比selenium語句簡潔

對比pytest重跑簡單

 

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”並輸入seldom
        self.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,
                )
複製代碼
 
 
 
 
 
 
 
 
 
po 模式
 
import seldom
from poium import Page, Element


class BaiduPage(Page):
    """baidu page"""
    search_input = Element(id_="kw")
    search_button = Element(id_="su")


class BaiduTest(seldom.TestCase):
    """Baidu serach test case"""

    def test_case(self):
        """
        A simple test
        """
        page = BaiduPage(self.driver)
        page.get("https://www.baidu.com")
        page.search_input = "haha"
        page.search_button.click()
        self.assertTitle("haha_百度搜索")


if __name__ == '__main__':
    seldom.main()
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章