def test_actionToDict_QuitBrowserAction(self): # Given source = QuitBrowserAction() target = dict sut = DictActionConverter(source, target) # When dataDict = sut.getConverted() # Then self.assertIsInstance(dataDict, dict) self.assertEqual(ActionCmd.QUIT_BROWSER.value, dataDict.get("command"))
def test_actionToDict_PauseAction(self): # Given source = PauseAction(seconds=-123.0) target = dict sut = DictActionConverter(source, target) # When dataDict = sut.getConverted() # Then self.assertIsInstance(dataDict, dict) self.assertEqual(ActionCmd.PAUSE.value, dataDict.get("command")) self.assertEqual(-123.0, dataDict.get("maxWait"))
def test_actionToDict_LoadPageAction(self): # Given source = LoadPageAction(url="Test value of textToSend") target = dict sut = DictActionConverter(source, target) # When dataDict = sut.getConverted() # Then self.assertIsInstance(dataDict, dict) self.assertEqual(ActionCmd.LOAD_PAGE.value, dataDict.get("command")) self.assertEqual("Test value of textToSend", dataDict.get("textToSend"))
def test_dictToAction_shouldRaiseValueIfCommandIsUnknown(self): # Given source: dict = { "command": "This is an invalid test command", "searchStrategy": 0, "searchIdentifier": "", "textToSend": "", "maxWait": 0.0 } target = Action sut = DictActionConverter(source, target) # When / Then with self.assertRaises(ValueError): sut.getConverted()
def __actionBundleToDict(self) -> dict: bundle: ActionBundle = self._source dictActions: List = [] bundleName = bundle.name try: for action in bundle.actions: dictAction: dict = DictActionConverter( source=action, target=dict).getConverted() dictActions.append(dictAction.copy()) dictAction.clear() except KeyError: logger.error("Key of actionDict could not be mapped.", exc_info=True) bundleName = "New" dictActions = [] except Exception as e: logger.error("Unexpected exception.%s", e, exc_info=True) raise # Return a valid dictionary at any case except a previous raise. finalDict: dict = { "version": self.__CURRENT_VERSION, "name": bundleName, "actions": dictActions } return finalDict
def __dictToActionBundle(self) -> ActionBundle: dataDict = self._source # We don't want to stall the app in case of missing/wrong directories/files. # So we just return an empty ActionBundle if for some reason there is no JSON data: if dataDict is None or not dataDict.get("actions") or len( dataDict.get("actions")) == 0: return ActionBundle.createNew() # Convert action: Action actions: [Action] = [] try: for actionDict in dataDict.get("actions"): action = DictActionConverter(source=actionDict, target=Action).getConverted() if isinstance(action, Action): actions.append(action) actionBundle = ActionBundle(name=dataDict['name'], actions=actions) return actionBundle except KeyError: logger.warning("Key of actionDict could not be mapped.", exc_info=True) return ActionBundle.createNew() except Exception as e: logger.error("Unexpected exception. %s", e, exc_info=True) raise
def test_actionToDict_SubmitAction(self): # Given searchConditions = Mock(spec_set=SearchConditions) searchConditions.searchStrategy = Strategy.BY_ID searchConditions.identifier = "Test value of searchIdentifier" source = SubmitAction(searchConditions=searchConditions) target = dict sut = DictActionConverter(source, target) # When dataDict = sut.getConverted() # Then self.assertIsInstance(dataDict, dict) self.assertEqual(ActionCmd.SUBMIT.value, dataDict.get("command")) self.assertEqual(Strategy.BY_ID.value, dataDict.get("searchStrategy")) self.assertEqual("Test value of searchIdentifier", dataDict.get("searchIdentifier"))
def test_dictToAction_QuitBrowserAction(self): # Given source: dict = { "command": ActionCmd.QUIT_BROWSER.value, "searchStrategy": Strategy.BY_TEXT.value, "searchIdentifier": "Test value of searchIdentifier", "textToSend": "Test value of textToSend", "maxWait": 0.0 } target = Action sut = DictActionConverter(source, target) # When action = sut.getConverted() # Then self.assertIsInstance(action, QuitBrowserAction) self.assertEqual(ActionCmd.QUIT_BROWSER, action.command)
def test_dictToAction_LoadPageAction(self): # Given source: dict = { "command": ActionCmd.LOAD_PAGE.value, "searchStrategy": Strategy.IGNORE.value, "searchIdentifier": "Test value of searchIdentifier", "textToSend": "Test value of textToSend", "maxWait": 0.0 } target = Action sut = DictActionConverter(source, target) # When action = sut.getConverted() # Then self.assertIsInstance(action, LoadPageAction) self.assertEqual(ActionCmd.LOAD_PAGE, action.command) self.assertEqual("Test value of textToSend", action.textToSend)
def test_dictToAction_PauseAction(self): # Given source: dict = { "command": ActionCmd.PAUSE.value, "searchStrategy": Strategy.BY_XPATH.value, "searchIdentifier": "Test value of searchIdentifier", "textToSend": "Test value of textToSend", "maxWait": 10 } target = Action sut = DictActionConverter(source, target) # When action = sut.getConverted() # Then self.assertIsInstance(action, PauseAction) self.assertEqual(ActionCmd.PAUSE, action.command) self.assertEqual(10, action.maxWait)
def test_dictToAction_ClickAction(self): # Given source: dict = { "command": ActionCmd.CLICK.value, "searchStrategy": Strategy.BY_ID.value, "searchIdentifier": "Test value of searchIdentifier", "textToSend": "Test value of textToSend", "maxWait": 0.0 } target = Action sut = DictActionConverter(source, target) # When action = sut.getConverted() # Then self.assertIsInstance(action, ClickAction) self.assertEqual(ActionCmd.CLICK, action.command) self.assertEqual(Strategy.BY_ID, action.searchConditions.searchStrategy) self.assertEqual("Test value of searchIdentifier", action.searchConditions.identifier)
def test_actionToDict_WaitUntilVisibleAction(self): # Given searchConditions = Mock(spec_set=SearchConditions) searchConditions.searchStrategy = Strategy.BY_XPATH searchConditions.identifier = "Test value of searchIdentifier" source = WaitUntilVisibleAction(searchConditions=searchConditions, maxWait=44.12) target = dict sut = DictActionConverter(source, target) # When dataDict = sut.getConverted() # Then self.assertIsInstance(dataDict, dict) self.assertEqual(ActionCmd.WAIT_UNTIL_VISIBLE.value, dataDict.get("command")) self.assertEqual(Strategy.BY_XPATH.value, dataDict.get("searchStrategy")) self.assertEqual("Test value of searchIdentifier", dataDict.get("searchIdentifier")) self.assertEqual(44.12, dataDict.get("maxWait"))
def test_init_shouldSetValidProperties(self): # Given source: dict = {"expectedTestKey": "expectedTestValue"} target = Action # When sut = DictActionConverter(source, target) # Then self.assertIsInstance(sut._source, dict) self.assertIs(sut._target, Action) self.assertDictEqual(source, sut._source)
def test_dictToAction_WaitUntilVisibleAction(self): # Given source: dict = { "command": ActionCmd.WAIT_UNTIL_VISIBLE.value, "searchStrategy": Strategy.BY_XPATH.value, "searchIdentifier": "Test value of searchIdentifier", "textToSend": "Test value of textToSend", "maxWait": 2.65 } target = Action sut = DictActionConverter(source, target) # When action = sut.getConverted() # Then self.assertIsInstance(action, WaitUntilVisibleAction) self.assertEqual(ActionCmd.WAIT_UNTIL_VISIBLE, action.command) self.assertEqual(Strategy.BY_XPATH, action.searchConditions.searchStrategy) self.assertEqual("Test value of searchIdentifier", action.searchConditions.identifier) self.assertEqual(2.65, action.maxWait)