selenium seldom框架

seldom学习(一):初探seldom框架

2020-05-13  本文已影响0人  一如既往而已

最近发现在github上的seldom自动框架,准备学习记录下,感觉这个框架对新手入门比较友好,准备学习下,并分享给大家。(好像听说前身是pyse改版后为seldom了 )
github地址: https://github.com/SeldomQA/seldom

特点
安装方式:
1.先安装python环境。(此处省略,自行百度)

参考:mac 下多python环境安装和切换

2.然后先安装seldom的依赖库

随着seldom 加入更多的功能,seldom不得不依赖其他的开源库。你可以在 (在github上的seldom的)requirements.txt 文件里面看到这些依赖。

colorama==0.4.3
selenium==3.141.0
parameterized==0.7.0
poium==0.5.2

先通过 pip 命令安装这些依赖库,可以加快seldom的这安装。

$ pip install -r requirements.txt
3.然后安装seldom测试框架库

方法一:直接pip安装 (不推荐,pip 库中不是最新的,可能存在部分bug)

 $ pip install seldom

方法二:使用github上的最新代码进行安装 (推荐,更新比较及时,安装最新的)

 $ pip install -U git+https://github.com/SeldomQA/seldom.git@master

说明,建议将github上源码下下来,里面有dome和相关API功能说明,挺详细的适合理解和练手

4.安装好后查看库信息和运行命令
$ pip show seldom
Name: seldom
Version: 1.5.0
Summary: WebUI automation testing framework based on Selenium and unittest.
Home-page: https://github.com/seldomQA/seldom/
Author: bugmaster
Author-email: fnngj@126.com
License: BSD
Location: /Users/sleeli/.pyenv/versions/3.6.9/lib/python3.6/site-packages
Requires: colorama, openpyxl, parameterized, selenium
Required-by: 

也可以通过直接运行命令 -v 方式查看安装工具的版本

$ seldom -v
version 1.5.0
5.seldom 命令 查看帮助:
$ seldom -h
usage: seldom [-h] [-v] [--project PROJECT] [-r R] [-install INSTALL]

WebUI automation testing framework based on Selenium.

optional arguments:
  -h, --help         show this help message and exit
  -v, --version      show version
  --project PROJECT  Create an Seldom automation test project.
  -r R               run test case
  -install INSTALL   Install the browser driver, For example, 'chrome',
                     'firefox'.
6.创建项目:
$ seldom --project mypro
目录结构如下:

mypro/
├── test_dir/
│   ├── test_sample.py
├── reports/
└── run.py
test_dir/目录实现用例编写。
reports/ 目录存放生成的测试报告。
run.py 文件运行测试用例。
import seldom

if __name__ == '__main__':
    seldom.main(path="./test_dir",
                browser="chrome",
                driver_path="./lib/chromedriver",
                title="百度测试用例",
                description="测试环境:MAC Chrome 80.0.0",
                rerun=0)

'''
说明:
path : 指定测试目录。
browser: 指定浏览,默认chrome。
title : 指定测试项目标题。
description : 指定测试环境描述。
debug : debug模式,设置为True不生成测试用例。
rerun : 测试失败重跑
'''
import seldom
from seldom import data


class BaiduTest(seldom.TestCase):
    """Baidu serach test case"""
    def test_case(self):
        """A simple test"""
        self.open("https://www.baidu.com/")
        self.move_to_element(link_text="设置")
        self.type(id_="kw", text="哈哈")
        #self.click(css="#su")
        #self.move_to_element(xpath="//span[text()='设置']")
        self.click(link_text="搜索设置")
        self.select(css="#nr", value='20')
        self.click(class_name="prefpanelgo")
        self.sleep(2)
        self.assertAlertText("已经记录下您的使用偏好")
        self.accept_alert()
    
    @data([
        (1, 'seldom'),
        (2, 'selenium'),
        (3, 'unittest'),
    ])
    def test_baidu(self, name, search_key):
        """
         used parameterized test
        :param name: case name
        :param search_key: search keyword
        :return:
        """
        self.open("https://www.baidu.com")
        self.type(id_="kw", text=search_key)
        self.click(css="#su")
        self.assertInTitle(search_key)

if __name__ == '__main__':
    seldom.main(debug=True)


 $ seldom -r run.py 
2020-05-13 16:26:35,040 INFO Run the python version:
Python 3.6.9
Traceback (most recent call last):
  File "run.py", line 9, in <module>
    rerun=0)
  File "/Users/sleeli/.pyenv/versions/3.6.9/lib/python3.6/site-packages/seldom/running/test_runner.py", line 86, in main
    raise ValueError("Browser - driven path error,Please check if the file exists. => {}".format(driver_path))
ValueError: Browser - driven path error,Please check if the file exists. => ./lib/chromedriver
$ seldom -install chrome   # 谷歌浏览器驱动下载
2020-05-13 13:41:49,395 INFO    Downloading from: https://cdn.npm.taobao.org/dist/chromedriver/80.0.3987.106/chromedriver_mac64.zip
2020-05-13 13:41:49,395 INFO    To: /Users/sleeli/Documents/github/mypro/lib/chromedriver_mac64.zip

$ seldom -install firefox   # 火狐浏览器驱动下载
2020-05-13 13:43:51,176 INFO    Downloading from: https://github.com/mozilla/geckodriver/releases/download/v0.26.0/geckodriver-v0.26.0-macos.tar.gz

下载后在mypro 项目下的lib目录中即可找到,将run.py脚本中的driver_path="./lib/chromedriver" 进行修改
注意:如果是window应该是有.exe后缀的,我是mac所以没有后缀

7.运行项目:

此时再次运行项目即可运行OK了

$ seldom -r run.py 
2020-05-13 15:31:23,890 INFO Run the python version:
Python 3.6.9

            _      _                   
           | |    | |                  
 ___   ___ | |  __| |  ___   _ __ ___  
/ __| / _ \| | / _` | / _ \ | '_ ` _ \ 
\__ \|  __/| || (_| || (_) || | | | | |
|___/ \___||_| \__,_| \___/ |_| |_| |_| 
-----------------------------------------
                             @itest.info

2020-05-13 15:31:27,468 INFO Find element: id=kw 
2020-05-13 15:31:27,485 INFO input 'seldom'.
2020-05-13 15:31:27,677 INFO Find element: css selector=#su 
2020-05-13 15:31:28,807 INFO assertIn title: seldom_百度搜索.
2020-05-13 15:31:29,204 INFO Find element: id=kw 
2020-05-13 15:31:29,214 INFO input 'selenium'.
2020-05-13 15:31:29,353 INFO Find element: css selector=#su 
2020-05-13 15:31:30,475 INFO assertIn title: selenium_百度搜索.
2020-05-13 15:31:30,904 INFO Find element: id=kw 
2020-05-13 15:31:30,915 INFO input 'unittest'.
2020-05-13 15:31:31,058 INFO Find element: css selector=#su 
2020-05-13 15:31:32,179 INFO assertIn title: unittest_百度搜索.
2020-05-13 15:31:32,585 INFO Find element: xpath=//span[text()='设置'] 
2020-05-13 15:31:32,911 INFO Find element: link text=搜索设置 
2020-05-13 15:31:43,132 ERROR Find 0 elements through:css selector=#nr
generated html file: file:////Users/sleeli/Documents/github/mypro/demo/reports/2020_05_13_15_31_26_result.html
.1.2.3EE
8.查看报告

运行成功后,在mypro/reports/目录可以查看html版本的测试报告。


image.png
上一篇下一篇

猜你喜欢

热点阅读