GithubHelp home page GithubHelp logo

seldomqa / seldom Goto Github PK

View Code? Open in Web Editor NEW
777.0 51.0 324.0 10.38 MB

Seldom automation testing framework based on unittest

Home Page: https://seldomqa.github.io/

License: Apache License 2.0

Python 100.00%
selenium python test-framework unittest requests appium

seldom's Introduction

GitHub | Gitee |

PyPI version PyPI - Python Version

Seldom is an automation testing framework based on unittest.

seldom 是基于unittest 的自动化测试框架。

Features

  • web/app/api全功能测试框架
  • 提供脚手架快速创建自动化项目
  • 集成XTestRunner测试报告,现代美观
  • 提供丰富的断言
  • 提供强大的数据驱动
  • 平台化支持

Install

pip install seldom==3.6.0

If you want to keep up with the latest version, you can install with github repository url:

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

🤖 Quick Start

1、查看帮助:

seldom --help
Usage: seldom [OPTIONS]

  seldom CLI.

Options:
  --version                       Show version.
  -P, --project TEXT              Create an Seldom automation test project.
  -cc, --clear-cache BOOLEAN      Clear all caches of seldom.
  -p, --path TEXT                 Run test case file path.
  -c, --collect / -nc, --no-collect
                                  Collect project test cases. Need the
                                  `--path`.
  -l, --level [data|method]       Parse the level of use cases. Need the
                                  --path.
  -j, --case-json TEXT            Test case files. Need the `--path`.
  -e, --env TEXT                  Set the Seldom run environment `Seldom.env`.
  -b, --browser [chrome|firefox|ie|edge|safari]
                                  The browser that runs the Web UI automation
                                  tests. Need the `--path`.
  -u, --base-url TEXT             The base-url that runs the HTTP automation
                                  tests. Need the `--path`.
  -d, --debug / -nd, --no-debug   Debug mode. Need the `--path`.
  -rr, --rerun INTEGER            The number of times a use case failed to run
                                  again. Need the `--path`.
  -r, --report TEXT               Set the test report for output. Need the
                                  `--path`.
  -m, --mod TEXT                  Run tests modules, classes or even
                                  individual test methods from the command
                                  line.
  -ll, --log-level [TRACE|DEBUG|INFO|SUCCESS|WARNING|ERROR]
                                  Set the log level.
  -h2c, --har2case TEXT           HAR file converts an seldom test case.
  -s2c, --swagger2case TEXT       Swagger file converts an seldom test case.
  --help                          Show this message and exit.

2、创建项目:

> seldom -P mypro

目录结构如下:

mypro/
├── test_dir/
│   ├── __init__.py
│   ├── test_web_sample.py
│   └── test_api_sample.py
├── test_data/
│   └── data.json
├── reports/
└── confrun.py
  • test_dir/ 测试用例目录。
  • test_data/ 测试数据文件目录。
  • reports/ 测试报告目录。
  • confrun.py 运行配置文件。

3、运行项目:

  • ❌️ 在pyCharm中右键执行。

  • ✔️ 通过命令行工具执行。

> seldom -p test_dir # 运行 test_dir 测试目录


              __    __
   ________  / /___/ /___  ____ ____
  / ___/ _ \/ / __  / __ \/ __ ` ___/
 (__  )  __/ / /_/ / /_/ / / / / / /
/____/\___/_/\__,_/\____/_/ /_/ /_/  v3.x.x
-----------------------------------------
                             @itest.info
...

2022-04-30 18:37:29 log.py | INFO | -------------- Request -----------------[🚀]
2022-04-30 18:37:29 log.py | INFO | [method]: DELETE      [url]: http://httpbin.org/delete

2022-04-30 18:37:30 log.py | INFO | -------------- Response ----------------[🛬️]
2022-04-30 18:37:30 log.py | INFO | successful with status 200

2022-04-30 18:37:30 log.py | DEBUG | [type]: json      [time]: 0.725183

2022-04-30 18:37:30 log.py | DEBUG | [response]:
 {'args': {}, 'data': '', 'files': {}, 'form': {}, 'headers': {'Accept': '*/*', 'Accept-Encoding': 'gzip, deflate', 'Content-Length': '0', 'Host': 'httpbin.org', 'User-Agent': 'python-requests/2.25.0', 'X-Amzn-Trace-Id': 'Root=1-626d1168-457309ad306428ca5bcbb961'}, 'json': None, 'origin': '173.248.248.88', 'url': 'http://httpbin.org/delete'}

...

2022-04-30 18:37:36 log.py | INFO | ✅ Find 1 element: id=sb_form_q  -> input 'seldom'.
2022-04-30 18:37:39 log.py | INFO | 👀 assertIn title: seldom - 搜索.
.52022-04-30 18:37:39 log.py | INFO | 📖 https://cn.bing.com
2022-04-30 18:37:41 log.py | INFO | ✅ Find 1 element: id=sb_form_q  -> input 'poium'.
2022-04-30 18:37:42 log.py | INFO | 👀 assertIn title: poium - 搜索.
.62022-04-30 18:37:42 log.py | INFO | 📖 https://cn.bing.com
2022-04-30 18:37:43 log.py | INFO | ✅ Find 1 element: id=sb_form_q  -> input 'XTestRunner'.
2022-04-30 18:37:44 log.py | INFO | 👀 assertIn title: XTestRunner - 搜索.
.72022-04-30 18:37:44 log.py | INFO | 📖 http://www.itest.info
2022-04-30 18:37:52 log.py | INFO | 👀 assertIn url: http://www.itest.info/.
.82022-04-30 18:37:52 log.py | SUCCESS | generated html file: file:///D:\mypro\reports\2022_04_30_18_37_29_result.html
2022-04-30 18:37:52 log.py | SUCCESS | generated log file: file:///D:\mypro\reports\seldom_log.log

4、查看报告

你可以到 mypro\reports\ 目录查看测试报告。

test report

🔬 Demo

seldom继承unittest单元测试框架,完全遵循unittest编写用例规范。

demo 提供了丰富实例,帮你快速了解seldom的用法。

Web UI 测试

import seldom
from seldom import Steps


class BaiduTest(seldom.TestCase):

    def test_case_one(self):
        """a simple test case """
        self.open("https://www.baidu.com")
        self.type(id_="kw", text="seldom")
        self.click(css="#su")
        self.assertTitle("seldom_百度搜索")

    def test_case_two(self):
        """method chaining """
        Steps(url="https://www.baidu.com").open().find("#kw").type("seldom").find("#su").click()
        self.assertTitle("seldom_百度搜索")


if __name__ == '__main__':
    seldom.main(browser="chrome")

说明:

  • seldom.main() 通过 browser 指定运行的浏览器。

HTTP 测试

seldom 2.0 支持HTTP测试

import seldom


class TestRequest(seldom.TestCase):

    def test_put_method(self):
        self.put('/put', data={'key': 'value'})
        self.assertStatusCode(200)

    def test_post_method(self):
        self.post('/post', data={'key': 'value'})
        self.assertStatusCode(200)

    def test_get_method(self):
        payload = {'key1': 'value1', 'key2': 'value2'}
        self.get("/get", params=payload)
        self.assertStatusCode(200)

    def test_delete_method(self):
        self.delete('/delete')
        self.assertStatusCode(200)


if __name__ == '__main__':
    seldom.main(base_url="http://httpbin.org")

说明:

  • seldom.main() 通过 base_url 指定接口项目基本URL地址。

App 测试

seldom 3.0 支持App测试

from appium.options.android import UiAutomator2Options

import seldom
from seldom.appium_lab.keyboard import KeyEvent


class TestBingApp(seldom.TestCase):
    """
    Test Bing APP
    """

    def start(self):
        self.ke = KeyEvent(self.driver)

    def test_bing_search(self):
        """
        test bing bbs search
        """
        self.sleep(2)
        self.click(id_="com.microsoft.bing:id/sa_hp_header_search_box")
        self.type(id_="com.microsoft.bing:id/sapphire_search_header_input", text="seldom")
        self.ke.press_key("ENTER")
        self.sleep(1)
        elem = self.get_element(xpath='//android.widget.TextView[@resource-id="count"]')
        self.assertIn("个结果", elem.text.lower())


if __name__ == '__main__':
    capabilities = {
        'deviceName': 'ELS-AN00',
        'automationName': 'UiAutomator2',
        'platformName': 'Android',
        'appPackage': 'com.microsoft.bing',
        'appActivity': 'com.microsoft.sapphire.app.main.MainSapphireActivity',
        'noReset': True,
    }
    options = UiAutomator2Options().load_capabilities(capabilities)
    seldom.main(app_server="http://127.0.0.1:4723", app_info=options, debug=True)

说明:

  • seldom.main() 通过 app_info 指定App信息; app_server 指定appium server 地址。

Run the test

import seldom

seldom.main()  # 默认运行当前测试文件
seldom.main(path="./")  # 当前目录下的所有测试文件
seldom.main(path="./test_dir/")  # 指定目录下的所有测试文件
seldom.main(path="./test_dir/test_sample.py")  # 指定目录下的测试文件

📖 Document

中文文档

项目实例

基于seldom的web UI自动化项目:

https://github.com/SeldomQA/seldom-web-testing

基于seldom的接口自动化项目:

https://github.com/defnngj/seldom-api-testing

微信(WeChat)

欢迎添加微信,交流和反馈问题。

微信

Star History

Star History Chart

感谢

感谢从以下项目中得到思路和帮助。

交流

QQ群:948994709

seldom's People

Contributors

986379041 avatar barryybl avatar defnngj avatar dependabot[bot] avatar fnngj avatar klookapptest avatar liutianping0326 avatar luna-cy avatar lyzenghq avatar spycsh avatar wu-clan avatar yongchin0821 avatar yuchou87 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

seldom's Issues

run.py运行case时因浏览器驱动问题case没有执行

run.py:
seldom.main(".\dvm\test_open.py",debug=False)
通过run.py运行无法执行case,但是单独运行test_open.pycase可以执行成功,浏览器驱动的路径已在环境变量中配置,因此main()没有传入此参数,但是查看seldom源码的test_runner.py和driver.py感觉当没有参数传入时并没有像webdriver.py中那样通过Seldom.driver.current_url获取driver path.请各位同学帮我看下这个问题。

run.py目录下执行用例报错

image
命令行生成项目
1)seldom --project g:\mypro
2)打开目录运行run.py报错如图
3)单独执行test_sample.py正常

关于每个用例的日志记录探讨

针对日志记录是否有必要:
A:针对每一个测试用例文件如test1.py 生成对应的专门日志文件吗?
B:还是说日志生成的时候,就会告诉你执行的那一个 *.py文件,执行那个类,执行了那个测试方法?

再引入两个小问题:如何保证以下日志的易用性和整体性!
1、若分布式怎么记录日志?
2、若需要多线程的时候,这个日志又怎么记录?

提供 通过子元素找父元素的方法

页面

        <div>
            <button onclick="ok()">
                <span>确定</span>
            </button>
        </div>
        <div>
            <button onclick="cancel()">
                <span>取消</span>
            </button>
        </div>

实现:

import seldom

class Test(seldom.TestCase):

    def test_case2(self):
        self.get("file:///Users/tech/klpro/github/seldom/test.html")
        elems = self.get_elements(xpath="//div/button/span")

        i = 0
        for elem in elems:
            if elem.text == "确定":
                i = i
                break
            else:
                i = i + 1

        self.click(xpath="//div/button", index=i)

目前使用seldom的实现,比较麻烦,而且也不可靠,提供更加简单可靠的方法。

使用assertText断言报错,提示driver找不到

使用assertText断言报错,提示driver找不到
et1.1.2: Traceback (most recent call last):
File "/Users/sleeli/Desktop/programfile/PycharmProjects/testBSC/CRC2.3/test_dir/test_login.py", line 43, in test_login_case
self.assertText("lizhen")
File "/Users/sleeli/.pyenv/versions/3.7.4/lib/python3.7/site-packages/seldom/case.py", line 102, in assertText
elem = self.driver.find_element_by_tag_name("html")
AttributeError: 'LoginPageTest' object has no attribute 'driver'

seldom api

没有看到检查元素是否存在的api; 使用场景,选择了不同的Type时,之前的页面元素会消失。然后再检查新的元素是否存在

怎么支持多个class

比如:driver.find_element_by_css_selector('.new-tj2.active').click()
写成:type(css=".new-tj2.active")
定位不到

虫师,请教一个关于你的pyse框架代码的问题

def assertTitle(self, title, second=3):
    '''
    Asserts whether the current title is in line with expectations.
    The default is 3 seconds.
    Usage:
    driver.assertTitle("title")
    '''
    if title == None:
        raise NameError("'title' can't be empty.")
    for s in range(second):
        try:
            self.assertIn(title, self.driver.get_title())
            break
        except AssertionError:
            sleep(1)
    else:
        self.assertIn(title, self.driver.get_title())

上面的else语句是否必要,for循环迭代完为空才执行else语句,但是else语句的执行内容跟for语句里的执行内容是一样的,我的理解再次执行还是没找到,是不是多余了?

要是能根据标签分组运行就更好了

一点小建议
1、要是能根据标签分组运行就更好了
2、用例中的业务逻辑和数据如何分离,页面元素发生变化后,如何以最小的代码修改量维护用例

select下拉选不了值,以下三种方法都试了,依然有问题,详细如下

系统demo:
每页显示10条 每页显示20条 每页显示50条

        self.select(css="#nr", value='20')
        self.select(css="#nr", text='每页显示20条')
        self.select(css="#nr", index=2)

笔者实际用例:
请选择 电话 微信 邮箱

       # self.select(css='#ddlSource',text='电话' )   list indices must be integers or slices, not NoneType
       # self.select(css="#ddlSource",value='0')      list indices must be integers or slices, not NoneType
       # self.select(css="#ddlSource",index=2)       list index out of range

跪求大佬看看

image

鼠标click_and_hold再move_to_element 缺少鼠标release的封装操作

实现点击一个button按住不松,移动到另一个位置,这个功能,先调用click_and_hold函数,再调用move_to_element函数,但是这时候鼠标没有释放,查了下seldom的API,貌似没有封装release函数,click_and_hold+move_to_element+release=selinium中的drag_and_drop函数,seldom中没有对这个函数进行封装

selinium中的drag_and_drop函数有release操作,而seldom中无替代方法,最终通过在move_to_element函数中添加self.w3c_actions.pointer_action.release() 问题解决了。

建议seldom中也提供鼠标释放的方法

image

image

image

多语言切换时,用例复用和page构造的问题

class LoginPage(Page):

la="English"    
la_selected=Element(xpath="//span[text()='{}']".format(la),describe=la)

背景:
1.被测页面是有中英韩日四种语言的。。但是测试用例是一样的。只是页面词条不一样
2.同时有些元素定位是需要用到text().所以跟语言有点关系
现在是想在page也面元素够早的时候支持配置这几个参数。英文页面写一个完整的用例
其他中文、日文、韩文,调用英文页面的方法。

要怎么弄比较好?

多个key值json数据,在用例中如何调用?

{
"url": "https://www.baidu.com",
"elemet":[
["id_=’kw‘","id_='su'"]],
"testdata": [
["seldom", "seldom_搜索结果"],
["poium", "poium_搜索结果"] ]
}

上述json格式的参数,在用例中如何调用?
背景:希望一个页面的url和元素定位参数,输入测试数据统一在一个文件维护

运行时出现了“系统找不到指定的文件”的错误

File "c:\program files (x86)\python36-32\Lib\subprocess.py", line 997, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] 系统找不到指定的文件。

我查找了一下,说是webdriver的路径不对,但我已经在项目目录下使用了seldom -install chrome,在lib目录下已经有chromedriver,还需要指定chromedriver 的绝对路径吗? 谢谢

控制test执行顺序的方法

目前一个class中有多个tests时,执行时顺序是按照19,az运行,帮忙提供用户可以自定义顺序方法

在单个用例中执行seldom.main()时会报错,'NoneType' object has no attribute 'get'

import time

import seldom
from seldom import Seldom

from promo_test.PG.login import Login, Reg


class RegTest(seldom.TestCase):
    """Promotion Test"""

    def test_promotion(self):
        """
        promotion test
        """
        page_login = Login(Seldom.driver)
        page_reg = Reg(Seldom.driver)
        page_login.get("https://promo.stratifyd.cn")
        page_reg.register()
        time.sleep(600)

    def test_login(self):
        """
        promotion test
        """
        page_login = Login(Seldom.driver)
        page_login.get("https://promo.stratifyd.cn")


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

如果想只执行test_login, 该怎么执行呀

请增加断言方法,

请增加断言方法,目前相对元素文本的断言方式好像没有,是否可以增加方面的断言

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.