项目作者: dcfranca

项目描述 :
PHAT: Pluggable HTTP Auto Testing
高级语言: Python
项目地址: git://github.com/dcfranca/phat.git
创建时间: 2017-05-05T08:33:51Z
项目社区:https://github.com/dcfranca/phat

开源协议:MIT License

下载


ACKNOWLEDGMENT

This software was originally developed at Booking.com. With approval from Booking.com, this software was released as open source, for which the authors would like to express their gratitude.

PHAT: Pluggable HTTP Auto Testing

Tool for simple HTTP test automation.

About

PHAT was originally conceived in a Booking.com Hackathon.
This tool main goal is to a simple and easy way to test HTTP requests.
The core of the application just make HTTP requests and send the response to the plugins available.

The strength of the tool is in its plugins.
I’ll detail each plugin below and how to use it.

A plugin is able to:

  1. Add command line options
  2. Change the way to handle and generate URLs from relative paths
  3. Analyse an HTTP request/response and infer whether it’s failing or not.

The documentation for creating new plugins is coming soon :)

Installation instructions

Go to the directory you downloaded it and run:

  1. python setup.py install

If you have any aditional plugins, you can add the plugins directory setting the environment variable PHAT_EXTRA_PLUGINS_DIR
For ex: if you have more plugins under /plugins_dir/plugins’ you should set PHAT_EXTRA_PLUGINS_DIR to /plugins_dir’

pip:

Just run pip install phat-tool

Basic Usage

  1. Usage: phat [options] -f <filename>
  2. Options:
  3. --version show program's version number and exit
  4. -h, --help show this help message and exit
  5. -f FILENAME, --file=FILENAME
  6. The json file that contains your test cases
  7. -u USERNAME, --username=USERNAME
  8. The username for a http basic authentication
  9. -p PASSWORD, --password=PASSWORD
  10. The password for a http basic authentication or a file
  11. with the password
  12. -l, --use-https Indicates wether it should open the links using https
  13. -x, --no-proxy Indicates wether it should use proxy or not
  14. -o OUTPUT_FILENAME, --output-file=OUTPUT_FILENAME
  15. The json file to output with your results
  16. -d DEBUG, --debug=DEBUG
  17. The debug messages level (1-4)
  18. --tests-sequential If true, tests will be run sequentially for easier
  19. troubleshooting
  20. -J JUNIT_FILENAME, --junit-file=JUNIT_FILENAME
  21. The JUnit format file to output with your results
  22. -z SELENIUM_BROWSER, --selenium-browser=SELENIUM_BROWSER
  23. The selenium driver to use, one of ('chrome',
  24. 'firefox', 'phantomjs')
  25. -Z SELENIUM_BROWSER_PATH, --selenium-browser-path=SELENIUM_BROWSER_PATH
  26. Path to the browser or driver executable to use with
  27. Selenium
  28. -G SELENIUM_GRID_HUB, --selenium-grid=SELENIUM_GRID_HUB
  29. URL to connect on a Selenium Grid Hub
  30. --selenium-tests-sequential
  31. If true, selenium tests will be run sequentially for
  32. easier troubleshooting
  33. -H HTML_FILENAME, --html-file=HTML_FILENAME
  34. The HTML format file to output with your results

Additional switches depend on the set of plugins currently available.

The file required to run is a json file follows this structure:

  1. [
  2. {
  3. "url": "/path/"
  4. },
  5. {
  6. "url": "/relativepath/",
  7. "run_browser": true,
  8. "regex": ["[-+]?[0-9]*\\.[0-9]+"]
  9. },
  10. {
  11. "url": "/relativepath/",
  12. "compare_url": "https://mydomain.com/absolutepath/",
  13. "fuzzy": 1.0,
  14. "run_browser": true
  15. }
  16. ]

Each item of the array of the json is required to contain a url
property, this url can use a relative or absolute path, if it’s a
relative the domain will be mounted based on the server, your username
and role.
Every other attribute is optional and depends on the plugins available.

Default Plugins

CompareURL

Plugin for testing if the response content of a http request is the similar to other one

How it works

  1. It looks for the property compare_url on the json item, this property is dictionary containing the url to compare and the fuzzy ratio you expect.
  2. This test only run in the case it finds this property
  3. You can define how similar the response contents might be using the property fuzzy in a float from 0-1
  4. 0 = They can be completely different
  5. 1 = They must be exactly the same

Example

  1. {
  2. "url": "/relativepath/",
  3. "compare_url": {
  4. "url": "https://mydomain.com/absolutepath/",
  5. "fuzzy": 0.9
  6. }
  7. }

StatusCode

Plugin for testing the response status code of a HTTP request.

How it works

  1. It looks for the property status_codes on the json item, this property is an array of expected status codes.
  2. If nothing is assigned the default value is defined as:
  3. [200, 301, 302]
  4. This test always run

Example

  1. {
  2. "url": "http://mydomain.com",
  3. "status_codes": [404, 200]
  4. }

RunBrowser

Plugin that opens the url in the default browser

How it works

  1. It looks for the property run_browser on the json item, this property is a boolean, if the property is true it opens the url in a new tab in your default browser

Example

  1. {
  2. "url": "/checkme/",
  3. "run_browser": true
  4. }

Regex

Plugin that checks if a list of regex expressions are in the response content of the http request.

How it works

  1. It looks for the property regex or not_regex on the json item, this property is a list of strings containing the regex expressions you want to find or not to find.

Example

  1. {
  2. "url": "/showmeanumber",
  3. "regex": ["[-+]?[0-9]*\\.[0-9]+"]
  4. }

Plugin that follow the links on the page, looking for a 404 or 500 error

How it works

  1. It looks for the property follow_links or follow_links_fast on the json item
  2. The property follow_links is a simple boolean indicating wether the tests should follow the links or not, in case it's true it'll follow all the links on the page and report any 404 or 500 error.
  3. The property follow_links_fast is a float similar to the fuzzy property of the CompareURLs plugin indicating how different the urls must be to go through them, in this case the tests can run much faster as it's not going through all the links, but only the ~different~ ones.

Example

  1. {
  2. "url": "/givemesomelinks",
  3. "follow_links_fast": 0.60
  4. }

Selenium

Plugin that allows to add Selenium tests to your test case

How it works

  1. It looks for the property selenium_steps on the json item, this property is a list of steps that can be performed by Selenium.
  2. You also can pass the --selenium-browser to the command line to choose the browser you want to run the tests (the default is phantomjs, but it works nicer with Firefox)
  3. If you want to run against a Selenium grid you have to add the parameter --selenium-grid, i.e: http://localhost:4445/wd/hub
  4. Each test can have one or more of those properties:
  5. element: If you want to check if some element exists, check its value or interact with it.
  6. Each element can have one or more of this properties:
  7. class: Select the element based on a css class
  8. id: Select the element based on an id
  9. css_selector: Select the element given a css_selector expression
  10. must_be_displayed: Boolean indicating wether the element must be displayed or not (default is false)
  11. exists: Boolean indicating wether you want to check if an elements exists or the opposite (default is true)
  12. action: Property indicating the action to be made in the selected element
  13. Possible actions are click or type(to type in an input field):
  14. current_url: Check if the current url is ==, contains, starstswith or the specific string
  15. wait: An integer indicating the number of seconds to wait until the next step
  16. contains_regex: Indicating if the selected element must contains a specific regex expression

Example

  1. [
  2. {
  3. "url": "/",
  4. "selenium_steps": [
  5. {
  6. "element": {
  7. "id": "logo_no_globe_new_logo"
  8. }
  9. },
  10. {
  11. "element": {
  12. "id": "non-existent-item",
  13. "exists": false
  14. },
  15. "current_url": {
  16. "==": "/"
  17. }
  18. },
  19. {
  20. "element": {
  21. "id": "ss",
  22. "action": {
  23. "type": "amsterdam"
  24. }
  25. }
  26. },
  27. {
  28. "wait": 3
  29. },
  30. {
  31. "element": {
  32. "css_selector": ".autocomplete-list",
  33. "action": {
  34. "click": true
  35. }
  36. }
  37. },
  38. {
  39. "element": {
  40. "css_selector": ".searchbox-button",
  41. "action": {
  42. "click": true
  43. }
  44. },
  45. "current_url": {
  46. "contains": "/myurl"
  47. }
  48. }
  49. ]
  50. },
  51. {
  52. "url": "/components/",
  53. "selenium_steps": [
  54. {
  55. "element": {
  56. "class": "currency",
  57. "action": {
  58. "click": true
  59. }
  60. }
  61. },
  62. {
  63. "wait": 2
  64. },
  65. {
  66. "element": {
  67. "id": "selectme",
  68. "contains": "USD"
  69. }
  70. },
  71. {
  72. "element": {
  73. "id": "converter",
  74. "contains_regex": "[0-9]+\\.[0-9]{2}"
  75. }
  76. }
  77. ]
  78. }
  79. ]

JSONApi

Plugin that allows to add json api tests to your test case

How it works

  1. It looks for the property json_path on the json item, this property is a dictionary with json paths to match and what to do with those matches
  2. Each match can be evaluated using one of those operators:
  3. Regular operators
  4. ==, !=, >, >=, <, <=, startswith, endswith, contains, match_regex
  5. Store operators
  6. store_var (store single value), store_length (store the lenght of the results), store_array (store the array of results)
  7. Suppose you need the output from a previous call to do the next request, or even you need to check some values in the future, you can store values in variables using one of the store operators
  8. Later you can access the variable value using "<<name>>" syntax.
  9. Array operators (those operators apply over an array of results from the json path)
  10. length_==, length_!=, length_>, length_<, length_>=, length_<=, any_==, any_!=, any_>, any_>=, any_<, any_<=
  11. By the default the regular operators apply to all elements found that match the json path, and will report any error found, except if you use one of the any_ operators, in that case it'll fail only if none of the items comparison match.

Example

  1. [
  2. {
  3. "url": "/browser/log",
  4. "data": {
  5. "message": "test"
  6. },
  7. "json_path": {
  8. "$.status": {
  9. "==": "ok"
  10. },
  11. "$.channels[0]": {
  12. "==": "error"
  13. }
  14. }
  15. },
  16. {
  17. "url": "/browser/settings",
  18. "data": { "id":13903, "extension_id": "123" },
  19. "json_path": {
  20. "$.*": {
  21. "!=": null
  22. }
  23. }
  24. },
  25. {
  26. "url": "/browser/store",
  27. "data": { "caller_id": "72074579", "id": 13903, "url": "http://mydomain.com/test"},
  28. "json_path": {
  29. "$.context_id": {
  30. ">": 0,
  31. "store_var": "context_id"
  32. }
  33. }
  34. },
  35. {"wait": 8},
  36. {
  37. "url": "/browser/retrieve",
  38. "data": {"id":"72074579", "recipient_staff_id" : 13903},
  39. "json_path": {
  40. "$._context_id": {"==": "<<context_id>>"},
  41. "$.urls[0]":{"==": "http://mydomain.com/test"}
  42. }
  43. },
  44. {
  45. "url": "/browser/mark",
  46. "data": {"context_id":"<<context_id>>"},
  47. "json_path": {
  48. "$.status":{"==": "OK"}
  49. }
  50. },
  51. {
  52. "url": "/iso",
  53. "json_path": {
  54. "$.LON": {"==": "gb"},
  55. "$.DUB": {"==": "ie"},
  56. "$.SEA": {"==": "us"},
  57. "$.CBG": {"==": "gb"},
  58. "$.BCN": {"==": "es"}
  59. }
  60. }
  61. ]

TimeMe

Plugin that checks if a request is taking longer than expected

How it works

  1. It looks for the property timeme on the json item, this property is an object with the those possible properties:
  2. requests: Number of requests to perform (default to 5)
  3. limit_max: Limit for the maximum request time (in seconds)
  4. limit_avg: Limit for the average request time (in seconds)

Example

  1. {
  2. "url": "/myslowpage",
  3. "timeme": {
  4. "requests": 10,
  5. "limit_max": 0.6,
  6. "limit_avg": 0.45
  7. }
  8. }

JUnit

Plugin to generate a JUnit format file output, so it can be integrated with JUnit tools, i.e: Jenkins

How it works

  1. To use it you just need to pass the argument -J <filename> to the command line
  2. -J JUNIT_FILENAME, --junit-file=JUNIT_FILENAME
  3. The JUnit format file to output with your results

HTMLoutput

Plugin to generate a HTML format file output, and open it in a browser for a nicer visualization of the results

How it works

  1. To use it you just need to pass the argument -H <filename> to the command line
  2. -H HTML_FILENAME, --html-file=HTML_FILENAME
  3. The HTML format file to output with your results