Skip to content

Commit

Permalink
Merge pull request #26 from StatCan/feature-read-and-execute-commands-15
Browse files Browse the repository at this point in the history
Feature read and execute commands 15 - v. 0.1.0 Candidate
  • Loading branch information
DominicParent authored Mar 14, 2023
2 parents bec1d7f + 4822fef commit 3309951
Show file tree
Hide file tree
Showing 9 changed files with 229 additions and 1 deletion.
88 changes: 88 additions & 0 deletions demo/tass_sample.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
{
"uuid":"sample-test-run",
"build":"0.1.0Test",
"browser": "chrome",
"test_cases":[
{
"uuid":"sample-test-case",
"name":"Sample Test Case A",

"steps":[
{
"uuid":"sample-test-step-0",
"name":"Launch google.ca",
"action":"load_file",
"parameters":["relative_path", "tests/pages/page1.html"]
},
{
"uuid":"sample-test-step-1",
"name":"Type search query into google",
"action":"type",
"parameters":["locator", {"by":"id", "value":"nameField"}, "text", "do a barrel roll"]
},
{
"uuid":"sample-test-step-2",
"name":"Click the search button",
"action":"click",
"parameters":["locator", {"by": "id", "value": "btnColor"}]
},
{
"uuid":"sample-test-step-3",
"name":"Read search query",
"action":"read_css",
"parameters":["locator", {"by": "id", "value": "btnColor"}, "attribute", "background-color"]
}
]

},
{
"uuid":"sample-test-case",
"name":"Sample Test Case B",
"steps":[
{
"uuid":"sample-test-step-0",
"name":"Launch google.ca",
"action":"load_file",
"parameters":["relative_path", "tests/pages/page1.html"]
},
{
"uuid":"sample-test-step-1",
"name":"Type search query into google",
"action":"type",
"parameters":["locator", {"by":"id", "value":"nameField"}, "text", "do a barrel roll"]
},
{
"uuid":"sample-test-step-2",
"name":"Click the search button",
"action":"click",
"parameters":["locator", {"by": "id", "value": "btnColor"}]
},
{
"uuid":"sample-test-step-3",
"name":"Read search query",
"action":"read_css",
"parameters":["locator", {"by": "id", "value": "btnColor"}, "attribute", "background-color"]
},
{
"uuid":"sample-test-step-1",
"name":"Type search query into google",
"action":"type",
"parameters":["locator", {"by":"id", "value":"nameField"}, "text", "don't do a barrel roll"]
},
{
"uuid":"sample-test-step-2",
"name":"Click the search button",
"action":"click",
"parameters":["locator", {"by": "id", "value": "btnColor"}]
},
{
"uuid":"sample-test-step-3",
"name":"Read search query",
"action":"read_css",
"parameters":["locator", {"by": "id", "value": "btnColor"}, "attribute", "background-color"]
}
]

}
]
}
20 changes: 20 additions & 0 deletions tass/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import argparse
import json
from tass.core.tass_files import TassRun


def main(args):
print(args.file)
with open(args.file) as file:
test = TassRun(args.file, **json.load(file))
for case in test.collect():
print(case)
case.execute_tass()


if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--file',
action='store', required=True)

main(parser.parse_args())
2 changes: 1 addition & 1 deletion tass/actions/selenium.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import os
from selenium.common.exceptions import WebDriverException



def _find_element(driver, locator):
return driver.find_element(**locator)

Expand Down
Empty file added tass/core/__init__.py
Empty file.
32 changes: 32 additions & 0 deletions tass/core/tass_case.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from tass.core.tass_items import TassItem
from tass.drivers.browserdriver import newDriver
import tass.actions.selenium as actions


class TassCase(TassItem):
def execute_tass(self):
# print(self.steps)
for step in self._steps:
print('')
print('* * * * * * * * * *')
print(step)
print('* * * * * * * * * *')
_execute_step(step, self.driver)

def __init__(self, *, steps=[], browser_config={}, **kwargs):
# TODO: Include Pages after confirming data structure
super().__init__(**kwargs)
self._browser_config = browser_config
self._steps = steps
self._driver = None

@property
def driver(self):
if (self._driver is None):
self._driver = newDriver(**self._browser_config)
return self._driver


def _execute_step(step, driver):
params = dict(zip(it := iter(step.get('parameters', None)), it))
getattr(actions, step.get('action'))(driver=driver, **params)
37 changes: 37 additions & 0 deletions tass/core/tass_files.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import json
from tass.core.tass_items import TassFile
from tass.core.tass_case import TassCase
from tass.drivers.supportedbrowsers import Browsers


class TassSuite(TassFile):

def collect(self):
# TODO: Collect all test cases as TassItems and yield
pass


class TassRun(TassFile):

def __init__(self, path, test_cases, browser, **kwargs):
super().__init__(path, **kwargs)
self._test_cases = test_cases
self._browser_name = browser

def collect(self):
# TODO: Collect all test cases as TassItems,
# then collect all TestSuites and yield TassItems
# print(self.file)

try:
browser = Browsers.browser(self._browser_name)
except (KeyError) as ke:
print('')
print('Not a supported browser: ', self._browser_name)
raise ke

for case in self._test_cases:
driver = {'browser': browser,
'config': json.load(open('tass/config/browsers.json'))
.get(browser, {})}
yield TassCase(parent=self, browser_config=driver, **case)
34 changes: 34 additions & 0 deletions tass/core/tass_items.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
class TassItem():

def __init__(self, parent=None, name=None, uuid=None, build=None):
self._parent = parent
self._name = name
self._uuid = uuid
self._build = build if build else parent.build

@property
def name(self):
return self._name

@property
def uuid(self):
return self._uuid

@property
def parent(self):
return self._parent

@property
def build(self):
return self._build


class TassFile(TassItem):

def __init__(self, file_path, **kwargs):
super().__init__(**kwargs)
self._file_path = file_path

@property
def file_path(self):
return self._file_path
4 changes: 4 additions & 0 deletions tass/drivers/browserdriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
from webdriver_manager.microsoft import EdgeChromiumDriverManager


def newDriver(browser, config):
return browser(config)


class WebDriverWaitWrapper():
""" Wrapper class for adding general features to browser driver classes."""
def wait_until(self, until_func, **kwargs):
Expand Down
13 changes: 13 additions & 0 deletions tass/drivers/supportedbrowsers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from enum import Enum
from tass.drivers.browserdriver import ChromeDriver
from tass.drivers.browserdriver import FirefoxDriver
from tass.drivers.browserdriver import EdgeDriver


class Browsers(Enum):
CHROME = ChromeDriver
FIREFOX = FirefoxDriver
EDGE = EdgeDriver

def browser(name):
return Browsers[name.upper()].value

0 comments on commit 3309951

Please sign in to comment.