Control a Selenium Driver using INI files.
This was created to make running tests even easier than before. With the SeleniumINI CLI we can run template files directly from the terminal. Also I added functionality for dynamic templates, batch template exectuion and concurrent tests.
usage: SeleniumINICLI.py [-h] [-b] [-c] [-d DYNAMIC] [-e] [-i INPUT_FILE] [-k]
[-o OUTPUT_FILE] [-p]
optional arguments:
-h, --help show this help message and exit
-b, --batch Creating report from a batch amount of templates
-c, --concurrent Only used when the --batch flag has been used, speficy
the amount of concurrent browsers should be running at
once.
-d DYNAMIC, --dynamic DYNAMIC
If you incorporated dynamic variables in you template
ini. Declare them here in a string in python diction
form. e.g. '{"url": "something.com"}'
-e, --exclude Will exclude element actions that were not performed.
-i INPUT_FILE, --input-file INPUT_FILE
The file of the template file
-k, --keep-temps Keep temporary files that were created by
SeleniumINIDriver
-o OUTPUT_FILE, --output-file OUTPUT_FILE
The report outputted to a file
-p, --pprint The report pretty printed to terminal
SeleniumINICLI.py -i my_github.ini
Excluding actions not performed, pretty printing and outputing log file
SeleniumINICLI.py -i my_github.ini --exclude -o output.log --pprint
Dynamic Templates are templates that were created however small changes may happen between templates. Dynamic Templates are the same as Static Templates except they contain value holders in the ini file. The value holders appear as <var_name> in the template file. When running dynamic template make sure to specify the --dynamic flag and pass in the correct variables. In this example my_github.ini contains two variables 'url' and 'user'. I filled in these place holders with 'google.com' and 'rascoro1'
SeleniumINICLI.py -i my_github.ini --dynamic "{'url': 'google.com', 'user':'rascoro1'}"
This will run one after another. Templates can either be dynamic or static.
Take a look at the github_batch.ini file to create batch files.
SeleniumINICLI.py -i github_batch.ini --batch
Concurrent can only be run with a batch file Templates can either be dynamic or static.
SeleniumINICLI.py -i github_batch.ini --concurrent
Each template file represents a flow within the browser.
General section is where we can modify general attributes.
Below are the currently supported attributes
url:The url to the webpage
start_wait: The time from start to first action
between_wait: The time to wait before moving onto the next element/section
between_action_wait: The time to wait before each action is preformed on a specific element/section
Example
url=https://www.google.com/
start_wait=
between_wait=1
between_action_wait=.5
Each other section can have any type of name.
For instance the submit button on a webpage could be the section: [submit_button]
And the search text could be name: [search_input_text]
Each one of these sections have specific attribute that are allowed.
Below are the attribute that can be placed within these sections:
attr_xpath: The xpath to the element you are looking for
send_keys: The keys to be sent to this element
click: Boolean either click or do not click element (True/False)
text: If you would like to obtain the element type (True/False)
clear: If you would like to clear the value in the text field (True/False)
submit: Submit the form (True/False)
get_attribute: Get a specific attribute from the selenium.Element e.g "outerHTML"
is_displayed: Is the element displayed (True/False)
is_enabled: Is the element enabled (True/False)
is_selected: Is the element selected (True/False)
Example:
[general]
url=https://www.google.com/
start_wait=1
between_wait=1
between_action_wait=.5
[search_input]
attr_xpath=//div/input[contains(@title, "Search")]
send_keys=rascoro1
submit=True
This is the easest part since creating the .ini is most of the work.
Initilize the Template then point the template object to the ini file.
Load the sections and set the execute order, This can be set by you if you would like to mess around with this.
Start the driver and then run the driver.
After a report will be returned as a dictionary.
If you would like to show the report nicely and with color you can us pprint()
import Template
t = Template.Template()
# the template we are choosing
t.file_path = "my_github.ini"
# load the sections into the template
t.load_sections()
# This is setting the order according to the 'my_github.ini'
t.set_execute_order()
# start the webdriver
t.start_driver()
# run the scraper
report = t.run()
# Exclude attributes that have value None since they were not searched for
# And were not specificed in the template.
t.pprint(exclude_none=True)