项目作者: ModusCreateOrg

项目描述 :
Python automation boilerplate
高级语言: Python
项目地址: git://github.com/ModusCreateOrg/python-automation-boilerplate.git
创建时间: 2018-07-24T12:22:44Z
项目社区:https://github.com/ModusCreateOrg/python-automation-boilerplate

开源协议:MIT License

下载


Selenium & Appium Pytest Web, Mobile and Hybrid App Testing Boilerplate

Description:

This is a boilerplate for testing mobile hybrid apps on Web, iOS & Android.

Dependencies:

Python pip pyenv virtualenv

Installation Steps

In order to get the tests to run locally, you need to install the following pieces of software.

NOTE: All commands shall be executed from Automation Project root directory:

  1. ### MacOS
  2. 1. Install Homebrew with `ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"`
  3. 1.1. Fix commandline `sudo installer -pkg /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg -target /`
  4. 2. Install Pyenv with `brew install pyenv` This is a python version manager.<br />
  5. Add the following to *~/.bash_profile*
  6. ```# Pyenv
  7. export PYENV_ROOT="$HOME/.pyenv"
  8. export PATH="$PYENV_ROOT/bin:$PATH"
  9. export PATH="$PYENV_ROOT/shims:$PATH"
  10. export PATH="$PYENV_ROOT/completions/pyenv.bash:$PATH"
  1. Install python 3.7.5 with pyenv install 3.7.5
  2. Set python version 3.7.5 to be used globally with pyenv global 3.7.5
  3. Install virtualenv with python3 -m pip install --user virtualenv
  4. Create new virtual env with python3 -m virtualenv .venv
  5. Activate new virtual env with source ./.venv/bin/activate
  6. Install all project dependencies with pip install -r requirements.txt
  7. Check python version used with which python.

    Shall be [PROJECT_DIR]/tests/UI/.venv_boilerplate/bin/python

Windows

  1. Install GitBash
  2. Uninstall any previous python version
  3. Install python 3.7.5 using official installation file
  4. Install virtualenv with python -m pip install --user virtualenv
  5. Create new virtual env with python -m virtualenv .venv
  6. Activate new virtual env with source ./.venv/Scripts/activate
  7. Install all project dependencies with pip install -r requirements.txt
  8. Check python version used with which python.

    Shall be [PROJECT_DIR]/tests/UI/.venv_boilerplate/Scripts/python

Test execution

Local Terminal run

  • Chrome example:
    1. python -m pytest -vv --gherkin-terminal-reporter --driver Chrome --driver-path ./selenium_drivers/chromedriver_mac --base-url http://localhost:3001 --variables webdriver/capabilities_web.json --variables i18n.json --variables variables.json --tags=""
  • Appium example:
    1. python -m pytest -vv --gherkin-terminal-reporter --driver Appium --appium-capability app ./[APP_NAME].apk --appium-capability platformName Android --appium-capability platformVersion '7.0' --appium-capability deviceName device --capability env Android --capability os_version 7.0 --tags="" --variables variables.json --variables i18n.json
    2. python -m pytest -vv --gherkin-terminal-reporter --driver Appium --appium-capability browserName Chrome --appium-capability base_url https://beep.modus.app --appium-capability platformName Android --appium-capability platformVersion '7.0' --appium-capability deviceName device --tags="" --variables variables.json --variables i18n.json

Browserstack run

Please read BS documentation for more details on configurations:

Parallel testing

  • Just add the -n=3 --dist=loadscope args and remove --gherkin-terminal-reporter as this reporting type is not compatible with parallel testing
    NOTE:
    n=3 means 3 parallel tests
    dist=loadscope means that parallelism is done at .features file level

Create PyCharm Run Configurations

  1. Edit Configurations > + > Python Tests > pytest
  • Chrome
    1. Script Path = [UI_TESTS_PATH]
    2. Additional Arguments = -vv --gherkin-terminal-reporter --driver Chrome --driver-path ./selenium_drivers/chromedriver_mac --variables variables.json --variables i18n.json
    3. Python Interpreter = 'Previously created virtualenv'
    4. Working Directory = [UI_TESTS_PATH]
  • Firefox

    1. Script Path = [UI_TESTS_PATH]
    2. Additional Arguments = -vv --gherkin-terminal-reporter --driver Firefox --driver-path ./selenium_drivers/geckodriver_mac --variables variables.json --variables i18n.json
    3. Python Interpreter = 'Previously created virtualenv'
    4. Working Directory = [UI_TESTS_PATH]
    • BrowserStack execution arguments based on env (just replace ‘Additional Arguments’ with correct value:
      ```
      Android App: -vv —gherkin-terminal-reporter —driver Appium —host ‘[BS_USERNAME]:[BS_KEY]@hub-cloud.browserstack.com’ —port 80 —variables webdriver/capabilities_android_app.json —variables i18n.json —variables variables.json —tags=””
      Android Web: -vv —gherkin-terminal-reporter —driver Browserstack —capability build ‘[NAME_OF_BUILD_APP_OR_FEATURE]’ —base-url [BASE_URL] —variables webdriver/capabilities_android_web.json —variables i18n.json —variables variables.json —tags=””

    iOS App: -vv —gherkin-terminal-reporter —driver Appium —host ‘[BS_USERNAME]:[BS_KEY]@hub-cloud.browserstack.com’ —port 80 —variables webdriver/capabilities_ios_app.json —variables i18n.json —variables variables.json —tags=””
    iOS Web: -vv —gherkin-terminal-reporter —driver BrowserStack —capability device ‘iPad Pro 12.9 2018’ —capability os_version ‘12.0’ —base-url [BASE_URL] —variables variables.json —variables i18n.json

IE: -vv —gherkin-terminal-reporter —driver BrowserStack —capability browser ‘IE’ —capability browser_version ‘11’ —base-url http://localhost:3001 —variables webdriver/capabilities_web.json —variables i18n.json —variables variables.json —tags=””
Edge: -vv —gherkin-terminal-reporter —driver BrowserStack —capability browser ‘Edge’ —capability browser_version ‘18.0’ —base-url http://localhost:3001 —variables webdriver/capabilities_web.json —variables i18n.json —variables variables.json —tags=””
Chrome: -vv —gherkin-terminal-reporter —driver Browserstack —capability build ‘[NAME_OF_BUILD_APP_OR_FEATURE]’ —base-url [BASE_URL] —variables webdriver/capabilities_web.json —variables webdriver/capabilities_web.json —variables i18n.json —variables variables.json —tags=””
Safari: -vv —gherkin-terminal-reporter —driver BrowserStack —capability browser ‘Safari’ —capability browser_version ‘12.0’ —base-url [BASE_URL] —variables webdriver/capabilities_web.json —variables i18n.json —variables variables.json —tags=””

  1. 2. Run or Debug with the above configurations
  2. ## Code Quality
  3. Linting = the process of analyzing the source code to flag programming errors, bugs, stylistic errors, and suspicious constructs.
  4. **IMPORTANT:** Lint your code before any commit
  5. - Go to _tests_root_ folder
  6. - Run `pylint ./**/**.py`
  7. - There should be only one Error: `E: 4, 0: invalid syntax (<string>, line 4) (syntax-error)`
  8. - This is due to a _pylint_ issue: root files or folders cannot be ignored from linting. Will follow the fix
  9. - A rating above 9.00 should be kept for the code
  10. ## Package tests for AWS CI
  11. TODO
  12. # Browserstack Configuration:
  13. Add BrowserStack API credentials to `./.browserstack` file

[credentials]
username=TODO
key=TODO

  1. # TestRail Integration
  2. This is HOW TO guide for TestRail integration of this project
  3. ## Configuration:
  4. Add TestRail API credentials to `./.testrailapi` file

[credentials]
email=TODO
key=TODO
url=https://moduscreateinc.testrail.io
verify_ssl=True

  1. Note: Please follow instructions for generating user_key: http://docs.gurock.com/testrail-api2/accessing
  2. **!DO NOT PUSH your credentials into git repo**
  3. ## Export test cases to TestRail
  4. - From project root directory
  5. - Run:
  6. **To import/update test Scenarios for ALL feature files**
  7. - ```python -m pytest -vv --export_tests_path "features" --variables variables.json --variables i18n.json

To import/update test Scenarios for INDIVIDUAL .feature file

  • python -m pytest -vv --export_tests_path "features/[DIR_NAME]/[FILE_NAME].feature" --variables variables.json --variables i18n.json

Implementation details

  • Each .feature file is a product functionality
    • Unique key is the pair of Feature Name - Functionality + feature description
      1. Feature: Create User - Email registration
      2. As an anonymous user
      3. I open the app for the first time
      4. I want to be able to register with email
    • It will create a new Test Suite for each unique Feature Name file published
    • It will create a new Section within the Test Suite for each unique Functionality
    • If test suite was previously imported it will update all the tests within
  • Each Scenario is a TestRail Case
    • Unique key is pair of scenario name + data set (The Examples line in json format)
      1. Scenario: Add two numbers
      2. Given I have powered calculator on
      3. When I enter <50> into the calculator
      4. When I enter <70> into the calculator
      5. When I press add
      6. Then The result should be <120> on the screen
      7. Examples:
      8. | number_1 | number_2 | result |
      9. | 10 | 20 | 30 |
      10. | 50 | 60 | 120 |
    • It will create a new case for each Scenario published
    • If case was imported it will update it with latest changes
    • Scenario tags:
    • Steps are imported as separate ones with empty Expected Results
    • Do NOT use And and But keys as it will fail the match of test cases during results publishing

Publish test results to TestRail

Prerequisites:

1. Create Test Plan in TestRail

  • You have to manually create the test plan in TestRail
    • Naming convention: [JIRAPROJECT_NAME][SPRINTNAME][MARKET] - MARKET only if applied
      • eg: JIRA_Sprint-1_us or JIRA_Regression_us
    • Test Plan shall contain all Cases that you want to execute within the session
    • The correct configuration shall be present. This is described by variables.json in env
    • Test results are published to TestRail at the end of testing
      • The reason of failure is also added to the test step

2. Test run details in project

  • Go to variables.json

    • Edit project with corresponding data. eg:
    • ```
      “project”: {
      “id”: 1,
      “name”: “JIRA”,
      “language”: “en”,
      “tags”:””,
      “test_plan”: “JIRA_Sprint_1”,
      “market”: “us”
      }

    • Info

      • id = mandatory, taken from TestRail, is the id of the project. Can be picked up from url in TestRail. Make sure id is correct.
      • name = mandatory, name of the project you will publish to.
      • tags = optional, filtering scenarios by required parameters
      • test_plan = mandatory, title of the test plan created manually in TestRail
      • language = mandatory, taking string from i18n.json for selected language
      • market = optional, in order to know for which market to trigger tests

Run tests and publish results to TestRail

  • Add the following argument to CLI
    • --export_results - this will run and publish tests results

Notes

Tips and Tricks

To benefit from autocomplete please set UI folder as Sources Root

  • Right click on UI_
  • Click on Mark Directory As
  • Click on Sources Root