pyse基于selenium(webdriver)进行了简单的二次封装,比selenium所提供的方法操作更简洁。

Posted IT自动化测试与运维

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了pyse基于selenium(webdriver)进行了简单的二次封装,比selenium所提供的方法操作更简洁。相关的知识,希望对你有一定的参考价值。

https://github.com/defnngj/pyse

虫师的web 自动化框架

pyse

WebUI automation testing framework based on Selenium and unittest.

介绍:

pyse基于selenium(webdriver)进行了简单的二次封装,比selenium所提供的方法操作更简洁。

特点:

  • 默认使用CSS定位,同时支持多种定位方法(id\name\class\link_text\xpath\css)。

  • 本框架只是对selenium(webdriver)原生方法进行了简单的封装,精简为大约30个方法,这些方法基本能够胜任于我们的web自动化测试。

  • 以测试类为单位,自动打开和关闭浏览器,减少浏览器的打开/关闭次数,节省时间。

  • 自动生成/report/目录,以及html测试报告生成。

  • 自带断言方法,断言title、URL 和 text。

安装说明:

  • Python3.5+ : https://www.python.org/

  • Selenium3.6.0+ : https://pypi.python.org/pypi/selenium

  • 进入pyse/目录,执行:

> python setup.py install

例子:

请查看demo/test_case.py目录

import pyseclass BaiduTest(pyse.TestCase):    def test_baidu(self):        ''' baidu search key : pyse '''
        self.open("https://www.baidu.com/")        self.type("#kw", "pyse")        self.click("#su")        self.assertTitle("pyse_百度搜索")if __name__ == '__main__':
    runner = pyse.TestRunner()
    runner.run()

运行测试用例说明:

  • 测试用例文件命名必须以“test”开头。

  • 默认情况下使用 Chrome 浏览器运行测试用例。

  • 元素定位方式默认使用 CSS 语法 #kw, 也可以显示的使用 css=>#kw

  • pyse的TestCase类中默认封装了assertTitleassertUrl 和 assertText等断言。

  • TestRunner() 默认匹配当前目录下"test*.py"的文件并执行。当然也可以指定测试目录,例如: TestRunner("path/you/project/test_case/") # 注意用斜线"/"表示路径。

  • 执行run()方法运行测试用例并生成测试报告,在调试测试用例过程中可以使用 debug() 方法将不会生成HTML测试报告。

支持的浏览器及驱动:

指定运行的浏览器:

import pyseclass YouTest(pyse.TestCase):    @classmethod
    def setUpClass(cls):        cls.driver = Pyse("chrome")    
    def test_case(self):        #……

支持的浏览器:

cls.driver = Pyse("firefox")   #Firefoxcls.driver = Pyse("chrome")    # Chromecls.driver = Pyse("ie")        #IEcls.driver = Pyse("opera")     #Operacls.driver = Pyse("edge")      #Edgecls.driver = Pyse("chrome_headless")  #Chrome headless模式

geckodriver(Firefox):https://github.com/mozilla/geckodriver/releases

Chromedriver(Chrome):https://sites.google.com/a/chromium.org/chromedriver/home

IEDriverServer(IE):http://selenium-release.storage.googleapis.com/index.html

operadriver(Opera):https://github.com/operasoftware/operachromiumdriver/releases

MicrosoftWebDriver(Edge):https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver

==========================================================

元素定位:

pyse支持多种定位方式,id、name、class、link text、xpath和css。把定位方法与定位内容一体,写起更加简洁。

    <form id="form" class="fm" action="/s" name="f">
      <span class="bg s_ipt_wr quickdelete-wrap">
        <input id="kw" class="s_ipt" autocomplete="off" maxlength="255" value="" name="wd">

定位方式(推荐使用 CSS):

# 默认支持CSS语法driver.type(".s_ipt","pyse")     #cssdriver.type("#su","pyse")        #cssdriver.type("id=>kw", "pyse")  #iddriver.type("class=>s_ipt", "pyse")  #class定位driver.type("name=>wd", "pyse")  #namedriver.type("xpath=>//*[@class='s_ipt']","pyse")  #xpathdriver.type("xpath=>//*[@id='kw']","pyse")        #xpathdriver.click_text("link_text=>新闻") #link text (点击百度首页上的"新闻"链接)

==========================================================

css选择器参考手册: http://www.w3school.com.cn/cssref/css_selectors.asp

测试报告

conda upgrade --all

conda activate pthon36

conda install selenium

cd pyse

python setup.py install

IEDriverServer.exe  chromedriver.exe

下载浏览器驱动放到下面的目录

D:\Anaconda3\envs\python36

谷歌浏览器如何打开出错,一定是 版本问题,驱动2.37 可以打开谷歌6.5 6.6

驱动2.35 可以打开谷歌6.3

http://chromedriver.storage.googleapis.com/index.html


----------ChromeDriver v2.37 (2018-03-16)----------
Supports Chrome v64-66
Resolved issue 2304: Test ChromeDriverSiteIsolation.testCanClickOOPIF fails on Chrome v66 for all desktop platforms [[Pri-1]]
Resolved issue 1940: Many window command endpoints are unimplemented in Chromedriver [[Pri-2]]
Resolved issue 1937: Get element rect is not implemented [[Pri-2]]
Resolved issue 2329: ChromeDriver does not allow value of 0 for extensionLoadTimeout option [[Pri-2]]

----------ChromeDriver v2.36 (2018-03-02)----------
Supports Chrome v63-65
Resolved issue 1819: testIFrameWithExtensionsSource is failing on Chrome v60+ [[Pri-1]]
Resolved issue 2221: Add command-line option to log INFO level to stderr [[Pri-2]]
Resolved issue  450: ChromeDriver hangs when switching to new window whose document is being overwritten [[Pri-2]]
Resolved issue 2235: Add option to control the wait for extension background pages [[Pri-2]]
Resolved issue 2234: fixed webview_devtools_remote_ is not right [[Pri-2]]
Resolved issue 2223: Unable to load extension if background page name starts with / [[Pri-2]]
Resolved issue 2280: ChromeDriver should be more extensible [[Pri-]]
Resolved issue 2231: Pixel 2 and Pixel 2 XL not working in Mobile Emulation [[Pri-]]
Resolved issue 746266: Chromedriver does not support OOPIF

----------ChromeDriver v2.35 (2018-01-10)----------
Supports Chrome v62-64
Resolved issue 2191: Executing javascript code fails if the script returns no result [[Pri-1]]
Resolved issue 2183: Connections Aren't Persistent [[Pri-2]]
Resolved issue 2207: Some mobile emulation devices do not work [[Pri-2]]
Resolved issue 2177: Get local storage returns command names in Chrome 63 [[Pri-2]]
Resolved issue 2179: absolute time on log entries [[Pri-2]]

----------ChromeDriver v2.34 (2017-12-10)----------
Supports Chrome v61-63
Resolved issue 2025: Incorrect navigation on Chrome v63+ [['Pri-0']]
Resolved issue 2034: Error looking for "Timeline.start" in Chrome [['Pri-2']]
Resolved issue 1883: Unable to emulate android devices with Chromedriver 2.30 [['Pri-2']]
Resolved issue 2103: Touch in mobile emulation doesn't work [[]]

----------ChromeDriver v2.33 (2017-10-03)----------
Supports Chrome v60-62
Resolved issue 2032: ChromeDriver crashes while creating DNS resolver [['Pri-1']]
Resolved issue 1918: Get/SetWindowSize & Get/SetWindowPosition commands are failing on Chromev62+ [['Pri-1']]
Resolved issue 2013: Android 8.0.0 webviews not supported [['Pri-2']]
Resolved issue 2017: In mobileEmulation "element is not clickable" if it is outside the visible area [['Pri-2']]
Resolved issue 1981: chromedriver does not respect excludeSwitches flag [['Pri-2']]
Partially Resolved issue 2002: Add Cookie is not spec compliant [[]]
Resolved issue 1985: FindElement raises the wrong error [[]]

----------ChromeDriver v2.32 (2017-08-30)----------
Supports Chrome v59-61
Resolved issue 1852: Error 'Element is not clickable at point' occurs on Chrome v61+ [['Pri-1']]
Resolved issue 1950: DeleteCookie in Chrome v62 and Chromedriver v2.31 fails [['Pri-2']]
Resolved issue 1942: ChromeDriver response in W3C mode is not standard compliant [['Pri-2']]
Resolved issue 1910: Fails to retrieve default prompt text on Chrome v62+ [['Pri-2']]
Resolved issue 1900: LaunchApp command throws UnknownError: cannot get automation extension on Mac and Windows Chrome v62+ [['Pri-2']]
Resolved issue 1885: unknown error: Cannot use 'in' operator to search for 'String' in null [['Pri-2']]
Resolved issue 1882: Chromedriver URI for Execute Async Script is not spec compliant [['Pri-3']]
Resolved issue 1621: Implement session/<session id>/window/fullscreen command [['Pri-3']]
Resolved issue 1896: Characters '3' and '#' are not being entered using SendKeys on Mac Chrome v62+ [[]]
Resolved issue 1894: chromedriver 2.31 should be linked with glibc 2.17 [[]]
Resolved issue 1740: ChromeDriver session handshake needs to be W3C compliant [[]]
Resolved issue 1898: Wrong error is thrown while sending text to non prompt dialog. [[]]
Resolved issue 1888: Chromedriver doesn't throw an exception when uploading an invalid file with selenium [[]]

----------ChromeDriver v2.31 (2017-07-21)----------
Supports Chrome v58-60
Resolved issue 1804: Flakiness when retrieving cookies using GetCookies command on Windows [['Pri-1']]
Resolved issue 1879: Implement /session/{session id}/cookie/{name} command [['Pri-2']]
Resolved issue 1860: WebDriverException: Message: unknown error: bad inspector message:"" when attempting to get page_source [['Pri-2']]
Resolved issue 1293: Timed out receiving message from renderer on window.location change [['Pri-2']]
Resolved issue 1849: ChromeDriver 2.30 AddCookie command uses incorrect default path [['Pri-2']]
Resolved issue 1256: switchToFrame should throw StaleElementReferenceException in case the element is stale [['Pri-2']]
Resolved issue 1786: Feature request: change chromeOptions to goog:chromeOptions [['Pri-3']]
Resolved issue 1772: Chromedriver running against canary chrome w/ headless flag requires XVFB for sendKey interactions [[]]
Resolved issue 727979: Failed to set Chrome's command line file on device for long command line [['Pri-3']]

----------ChromeDriver v2.30 (2017-06-07)----------
Supports Chrome v58-60
Resolved issue 644: ChromeDriver does not delete the profile & scoped_dir* folders after test exits [['Pri-1']]
Resolved issue 1114: unknown error:Maximum call stack size exceeded on calling any webdriver command [['OS-All', 'Pri-1']]
Resolved issue 1773: ChromeDriver crashes when using console.time on Chromev58+ [['OS-All', 'Pri-1']]
Resolved issue 1526: mobileEmulation not working for the listed devices in the chrome [['Pri-2']]
Resolved issue 1777: NoSuchFrameException is thrown when attempting to focus on a frame with protocol "chrome-extension://" within a page with protocol "https://" on Chrome 58 [['OS-All', 'Pri-2']]
Resolved issue 1779: testWindowMaximize is failing on Mac for Chrome v60+ [['OS-Mac', 'Pri-2']]
Resolved issue 1775: testShouldHandleNewWindowLoadingProperly failing on Chrome v60+ [['OS-All', 'Pri-2']]
Resolved issue 1796: Chromedriver failed to connect to Chrome on CrOS with latest canary build [[]]
Resolved issue 1353: Sending text to Alert / Confirm dialog should throw element not visible exception [[]]


以上是关于pyse基于selenium(webdriver)进行了简单的二次封装,比selenium所提供的方法操作更简洁。的主要内容,如果未能解决你的问题,请参考以下文章

官网pyse的修改版

AppUi自动化框架pyse.py代码

自动化selenium脚本开发(示例2:区划管理)

PySe-008-开启浏览器的手机模式

自动化selenium脚本开发(示例2:目录清单审核发布认领认领审核)

Selenium高手必备:基于WebDriver的Web UI自动化