Skip to content

Instantly share code, notes, and snippets.

@daevski
Created October 22, 2019 23:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daevski/8c7de4f55a80bc56f408fd0cc0325cef to your computer and use it in GitHub Desktop.
Save daevski/8c7de4f55a80bc56f408fd0cc0325cef to your computer and use it in GitHub Desktop.
Example python-cerberus validation of configuration file, both in yaml syntax
---
enabled:
required: True
type: boolean
empty: False
website_url:
required: True
type: string
empty: False
lt:
required: False
lv:
required: False
ea:
required: False
tc:
required: False
allowed_element_actions:
allowed: &allowed_element_actions
- click
- send keys
- select menu
- inner text
- null
allowed_locator_types:
allowed: &allowed_locator_types
- id
- css selector
- link text
- partial link text
- tag name
- xpath
login_steps:
required: True
type: dict
empty: False
schema:
credential_input_1: &element_rules
required: True
type: dict
empty: False
schema:
locator_value: &locator_value
required: True
type: string
empty: False
locator_type: &locator_type
required: True
type: string
empty: False
allowed: *allowed_locator_types
element_action: &element_action
required: True
type: string
empty: False
nullable: True
allowed: *allowed_element_actions
text_content: &text_content
required: True
type: string
empty: False
nullable: True
credential_input_2:
*element_rules
credential_submit:
*element_rules
security_questions:
required: True
type: dict
allow_unknown: True
nullable: True
valueschema:
type: dict
empty: False
schema:
locator_value:
*locator_value
locator_type:
*locator_type
element_action:
*element_action
text_content:
required: True
type: string
empty: False
nullable: False
multifactor_authentication:
required: True
type: dict
allow_unknown: True
nullable: True
valueschema:
type: dict
empty: False
schema:
locator_value:
*locator_value
locator_type:
*locator_type
element_action:
*element_action
text_content:
required: True
type: string
empty: False
nullable: False
login_verification_steps:
required: True
type: dict
empty: False
schema:
login_success:
*element_rules
login_failure:
*element_rules
payment_steps:
required: True
type: dict
empty: False
allow_unknown: True
schema:
payment_type:
required: True
empty: False
type: dict
anyof:
- payment_card
- account_transfer
schema:
payment_card:
required: False
type: dict
schema:
friendly_name:
required: True
empty: False
type: string
last_four:
required: True
empty: False
type: string
expiration_date:
required: True
empty: False
type: string
fill_card_number:
*element_rules
fill_expiration_month:
*element_rules
fill_expiration_year:
*element_rules
fill_cvc:
*element_rules
account_transfer:
required: False
empty: False
transfer_amount:
required: True
type: dict
empty: False
schema:
locator_value:
*locator_value
locator_type:
*locator_type
element_action:
required: True
type: string
empty: False
nullable: True
allowed: *allowed_element_actions
text_content:
required: True
type: float
empty: False
nullable: True
commit_payment:
*element_rules
valueschema:
type: dict
empty: False
schema:
locator_value:
*locator_value
locator_type:
*locator_type
element_action:
*element_action
text_content:
required: True
empty: False
nullable: True
confirmation_steps:
required: True
type: dict
empty: False
schema:
confirmation_element:
*element_rules
logout_steps:
required: True
type: dict
empty: False
schema:
logout_element:
*element_rules
logout_verification_steps:
required: True
type: dict
empty: False
schema:
logout_verification_element:
*element_rules
enc_pass:
required: False
type: string
empty: False
---
# The payment for this website will be executed if True, and will be skipped if False.
enabled: true
# This URL needs to be the full domain name, including the http(s) schema
website_url: https://www.ncsecu.org/
# These 4 aliases are defined here to simplify the rest of the document.
# They are used like *lt, *lv, etc, to allow for uniform indentation.
# They are not required, but thye do make the document much easier to read.
lt: &lt locator_type
lv: &lv locator_value
ea: &ea element_action
tc: &tc text_content
# List out the required elements on the page that are needed to login.
login_steps:
# This is usually the Username field for a traditional username/password login form.
credential_input_1:
# The value to use to find the element on the page.
*lv: userIDHome
# The type of locator, such as xpath, css selector, or id / class / name, etc
*lt: id
# The action to be taken on the element.
*ea: send_keys
# This is any text that is needed by the 'element_action' (aka *ea), usually text to locate, click or copy.
*tc: user87
# If the second credential is a password, DO NOT include it here.
credential_input_2:
*lv: userPasswordHome
*lt: id
*ea: send_keys
*tc: null # should ALWAYS be null for a password; password is handled internally by the application.
# IMPORTANT: if this *lv is null, the app attempts to use the 'return' key to submit the login credentials.
credential_submit:
*lv: .button[type="submit"]
*lt: css_selector
*ea: click
*tc: null
# How do we know if we logged in ssuccessfully??
login_verification_steps:
# We look for an element on the page to verify!
login_success:
*lv: CheckingSavingsMoneyMarket_0
*lt: id
*ea: null
*tc: null
# We ALSO must look for an element that verifies unsuccessful login...
login_failure:
*lv: ucLogin_tcUserID
*lt: id
*ea: null
*tc: null
security_questions:
# This section (name) must be included, but can be empty if there are no security questions.
# Note: Security answers will be collected by the app at runtime. Each question here will prompt an answer on first run.
multifactor_authentication:
# This section (name) must be included, but can be empty if there is no other authentication factors.
# Note: Some MFA may not be something that can be automated, but it's safer to use it than to turn it off!!
# You can handle it manually if you are watching the automation in a non-headless mode. Default timeout for finding elements is 30 seconds, but you can make this longer if you need to!
# Any and all steps required to 'automate' the rest of the payment steps.
# Include as many clicks and form fills (using anything from the list of supported 'element actions.'
payment_steps:
move_money:
*lv: 'Move Money'
*lt: link_text
*ea: click
*tc: null
from_account:
*lv: ddlFrom
*lt: id
*ea: select_menu
*tc: 12345678
to_account:
*lv: ddlTo
*lt: id
*ea: select_menu
*tc: 11234567
transfer_amount:
*lv: ucAmount_tcCurrency
*lt: id
*ea: send_keys
*tc: 950.0
continue_btn: &continue
*lv: btnContinue
*lt: id
*ea: click
*tc: null
payment_type:
*lv: ddlMortgage
*lt: id
*ea: click
*tc: Regular
# Sometimes you need to hit the 'continue' button a lot... see section following &continue above.
# so feel free to create your own aliases. Created with &name, used with *name.
submit_payment_type: *continue
commit_payment: *continue
# You will most likely want proof of your payment, so here are steps to capture the evidence.
confirmation_steps:
confirmation_element:
*lv: /html/body/table/tbody/tr[1]/td/table[2]/tbody/tr[2]/td[1]/form/table/tbody/tr[5]/td/table[1]/tbody/tr[7]/td[3]
*lt: xpath
# inner text as the action for 'confirmation_element' will capture and save the text that is located with the *lv.
*ea: inner_text
*tc: null
# Element(s) and actions for logging out...
logout_steps:
logout_element:
*lv: ctl00_logoutLink
*lt: id
*ea: click
*tc: null
# And verification that we logged out successfully.
logout_verification_steps:
logout_verification_element:
*lv: formLogout
*lt: id
*ea: null
*tc: null
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment