TestStepMaster.py 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  1. import baangt.base.GlobalConstants as GC
  2. from baangt.base.Timing.Timing import Timing
  3. from baangt.base.BrowserHandling.BrowserHandling import BrowserDriver
  4. from baangt.base.ApiHandling import ApiHandling
  5. import sys
  6. from pkg_resources import parse_version
  7. import logging
  8. from baangt.TestSteps.Exceptions import baangtTestStepException
  9. from baangt.TestSteps.AddressCreation import AddressCreate
  10. from baangt.base.PDFCompare import PDFCompare, PDFCompareDetails
  11. from baangt.base.Faker import Faker as baangtFaker
  12. from baangt.base.Utils import utils
  13. from baangt.base.RuntimeStatistics import Statistic
  14. import random
  15. import itertools
  16. logger = logging.getLogger("pyC")
  17. class TestStepMaster:
  18. def __init__(self, executeDirect=True, **kwargs):
  19. self.anchor = None
  20. self.anchorLocator = None
  21. self.anchorLocatorType = None
  22. self.testCaseStatus = None
  23. self.ifActive = False
  24. self.ifIsTrue = True
  25. self.elseIsTrue = False
  26. self.ifLis = [self.ifIsTrue]
  27. self.elseLis = [self.elseIsTrue]
  28. self.ifConditions = 0
  29. self.repeatIsTrue = [False]
  30. self.repeatDict = [] # used to store steps command of repeat data
  31. self.repeatData = [] # used to store RLP_ data to be looped in repeat
  32. self.repeatCount = [] # Used to store count of randomdata in loop
  33. self.repeatActive = 0 # to sync active repeat counts with repeat done and will be execute when both are equal
  34. self.repeatDone = 0
  35. self.baangtFaker = None
  36. self.statistics = Statistic()
  37. self.kwargs = kwargs
  38. self.testRunInstance = kwargs.get(GC.KWARGS_TESTRUNINSTANCE)
  39. self.testcaseDataDict = kwargs.get(GC.KWARGS_DATA)
  40. self.timing: Timing = kwargs.get(GC.KWARGS_TIMING)
  41. self.timingName = self.timing.takeTime(self.__class__.__name__, forceNew=True)
  42. self.browserSession: BrowserDriver = kwargs.get(GC.KWARGS_BROWSER)
  43. self.apiSession: ApiHandling = kwargs.get(GC.KWARGS_API_SESSION)
  44. self.testStepNumber = kwargs.get(GC.STRUCTURE_TESTSTEP) # Set in TestRunData by TestCaseMaster
  45. self.testRunUtil = self.testRunInstance.testRunUtils
  46. # Get the release from testrun-customizing if set: (will influence, whether or not elements will be executed)
  47. self.globalRelease = self.testRunInstance.globalSettings.get("Release", "")
  48. # check, if this TestStep has additional Parameters and if so, execute
  49. lSequence = self.testRunUtil.getSequenceByNumber(testRunName=self.testRunInstance.testRunName,
  50. sequence=kwargs.get(GC.STRUCTURE_TESTCASESEQUENCE))
  51. self.testCase = self.testRunUtil.getTestCaseByNumber(lSequence, kwargs.get(GC.STRUCTURE_TESTCASE))
  52. self.testStep = self.testRunUtil.getTestStepByNumber(self.testCase, self.testStepNumber)
  53. if self.testStep and len(self.testStep) > 1:
  54. if not isinstance(self.testStep[1], str) and executeDirect:
  55. # This TestStepMaster-Instance should actually do something - activitites are described
  56. # in the TestExecutionSteps.
  57. # Otherwise there's only a classname in TestStep[0]
  58. self.executeDirect(self.testStep[1][GC.STRUCTURE_TESTSTEPEXECUTION])
  59. # Teardown makes only sense, when we actually executed something directly in here
  60. # Otherwise (if it was 1 or 2 Tab-stops more to the left) we'd take execution time without
  61. # having done anything
  62. self.teardown()
  63. self.statistics.update_teststep_sequence()
  64. def executeDirect(self, executionCommands):
  65. """
  66. Executes a sequence of Commands. Will be subclassed in other modules.
  67. :param executionCommands:
  68. :return:
  69. """
  70. for index, (key, command) in enumerate(executionCommands.items()):
  71. try:
  72. self.executeDirectSingle(index, command)
  73. except Exception as ex:
  74. # 2020-07-16: This Exception is cought, printed and then nothing. That's not good. The test run
  75. # continues forever, despite this exception. Correct way would be to set test case to error and stop
  76. # this test case
  77. # Before we change something here we should check, why the calling function raises an error.
  78. logger.critical(ex)
  79. self.testcaseDataDict[GC.TESTCASESTATUS] = GC.TESTCASESTATUS_ERROR
  80. return
  81. def manageNestedCondition(self, condition="", ifis=False):
  82. if condition.upper() == "IF":
  83. self.ifConditions += 1
  84. self.ifLis.append(ifis)
  85. self.elseLis.append(False)
  86. elif condition.upper() == "ELSE":
  87. self.elseLis[-1] = True
  88. elif condition.upper() == "ENDIF":
  89. self.ifConditions -= 1
  90. if self.ifConditions < 0:
  91. raise BaseException("Numbers of ENDIF are greater than IF.")
  92. self.ifLis.pop()
  93. self.elseLis.pop()
  94. assert len(self.ifLis) == self.ifConditions + 1 and len(self.elseLis) == self.ifConditions + 1
  95. self.ifIsTrue = self.ifLis[-1]
  96. self.elseIsTrue = self.elseLis[-1]
  97. def executeDirectSingle(self, commandNumber, command, replaceFromDict=None):
  98. """
  99. This will execute a single instruction
  100. """
  101. # when we have an IF-condition and it's condition was not TRUE, then skip whatever comes here until we
  102. # reach Endif
  103. if not self.ifIsTrue and not self.elseIsTrue:
  104. if command["Activity"].upper() == "IF":
  105. self.manageNestedCondition(condition=command["Activity"].upper(), ifis=self.ifIsTrue)
  106. return
  107. if command["Activity"].upper() != "ELSE" and command["Activity"].upper() != "ENDIF":
  108. return
  109. if self.repeatIsTrue[-1]: # If repeat statement is active then execute this
  110. if command["Activity"].upper() == "REPEAT": # To sync active repeat with repeat done
  111. self.repeatActive += 1
  112. if command["Activity"].upper() != "REPEAT-DONE": # store command in repeatDict
  113. self.repeatDict[-1][commandNumber] = command
  114. return
  115. else:
  116. self.repeatDone += 1 # to sync repeat done with active repeat
  117. if self.repeatDone < self.repeatActive: # if all repeat-done are not synced with repeat store the data
  118. self.repeatDict[-1][commandNumber] = command
  119. logger.info(command)
  120. return
  121. self.repeatIsTrue[-1] = False
  122. if self.repeatData[-1]:
  123. data_list = []
  124. if type(self.repeatData[-1]) is list:
  125. for data_dic in self.repeatData[-1]:
  126. keys, values = zip(*data_dic.items())
  127. final_values = []
  128. for value in values: # coverting none list values to list. Useful in make all possible data using itertools
  129. if type(value) is not list:
  130. final_values.append([value])
  131. else:
  132. final_values.append(value)
  133. data_l = [dict(zip(keys, v)) for v in itertools.product(*final_values)] # itertools to make all possible combinations
  134. data_list.extend(data_l)
  135. else:
  136. data_list = [self.repeatData[-1]]
  137. if len(self.repeatCount) > 0 and self.repeatCount[-1]: # get random data from list of data
  138. try:
  139. data_list = random.sample(data_list, int(self.repeatCount[-1]))
  140. except:
  141. pass
  142. if data_list:
  143. for data in data_list:
  144. temp_dic = dict(self.repeatDict[-1])
  145. processed_data = {}
  146. for key in temp_dic:
  147. try:
  148. processed_data = dict(temp_dic[key])
  149. except Exception as ex:
  150. logger.debug(ex)
  151. try:
  152. self.executeDirectSingle(key, processed_data, replaceFromDict=data)
  153. except Exception as ex:
  154. logger.info(ex)
  155. else:
  156. temp_dic = dict(self.repeatDict[-1])
  157. for key in temp_dic:
  158. try:
  159. processed_data = dict(temp_dic[key])
  160. except Exception as ex:
  161. logger.debug(ex)
  162. for ky in processed_data:
  163. try:
  164. processed_data[ky] = self.replaceVariables(processed_data[ky])
  165. except Exception as ex:
  166. logger.info(ex)
  167. try:
  168. self.executeDirectSingle(key, processed_data)
  169. except Exception as ex:
  170. logger.info(ex)
  171. del self.repeatIsTrue[-1]
  172. del self.repeatDict[-1]
  173. del self.repeatData[-1]
  174. del self.repeatCount[-1]
  175. self.repeatDone -= 1
  176. self.repeatActive -= 1
  177. return
  178. css, id, lActivity, lLocator, lLocatorType, xpath = self._extractAllSingleValues(command)
  179. if lActivity == "COMMENT":
  180. return # Comment's are ignored
  181. if self.anchor and xpath:
  182. if xpath[0:3] == '///': # Xpath doesn't want to use Anchor
  183. xpath = xpath[1:]
  184. logger.debug(f"Anchor active, but escaped. Using pure xpath: {xpath}")
  185. else:
  186. logger.debug(f"Anchor active. combining {self.anchorLocator} with {xpath}")
  187. xpath = self.anchorLocator + xpath
  188. lValue = str(command["Value"])
  189. lValue2 = str(command["Value2"])
  190. lComparison = command["Comparison"]
  191. lOptional = utils.anything2Boolean(command["Optional"])
  192. # check release line
  193. lRelease = command["Release"]
  194. # Timeout defaults to 20 seconds, if not set otherwise.
  195. lTimeout = TestStepMaster._setTimeout(command["Timeout"])
  196. lTimingString = f"TS {commandNumber} {lActivity.lower()}"
  197. self.timing.takeTime(lTimingString)
  198. logger.info(
  199. f"Executing TestStepDetail {commandNumber} with parameters: act={lActivity}, lType={lLocatorType}, loc={lLocator}, "
  200. f"Val1={lValue}, comp={lComparison}, Val2={lValue2}, Optional={lOptional}, timeout={lTimeout}")
  201. original_value = lValue # used as key to make rlp json dict and will be used further to make sheet name
  202. lValue, lValue2 = self.replaceAllVariables(lValue, lValue2, replaceFromDict=replaceFromDict)
  203. if not TestStepMaster.ifQualifyForExecution(self.globalRelease, lRelease):
  204. logger.debug(f"we skipped this line due to {lRelease} disqualifies according to {self.globalRelease} ")
  205. return
  206. if lActivity == "GOTOURL":
  207. self.browserSession.goToUrl(lValue)
  208. elif lActivity == "SETTEXT":
  209. self.browserSession.findByAndSetText(xpath=xpath, css=css, id=id, value=lValue, timeout=lTimeout)
  210. elif lActivity == "SETTEXTIF":
  211. self.browserSession.findByAndSetTextIf(xpath=xpath, css=css, id=id, value=lValue, timeout=lTimeout,
  212. optional=lOptional)
  213. elif lActivity == "FORCETEXT":
  214. self.browserSession.findByAndForceText(xpath=xpath, css=css, id=id, value=lValue, timeout=lTimeout,
  215. optional=lOptional)
  216. elif lActivity == "FORCETEXTIF":
  217. if lValue:
  218. self.browserSession.findByAndForceText(xpath=xpath, css=css, id=id, value=lValue, timeout=lTimeout,
  219. optional=lOptional)
  220. elif lActivity == "SETANCHOR":
  221. if not lLocator:
  222. self.anchor = None
  223. self.anchorLocator = None
  224. self.anchorLocatorType = None
  225. else:
  226. found = self.browserSession.findBy(xpath=xpath, css=css, id=id, timeout=lTimeout)
  227. if found:
  228. self.anchor = self.browserSession.element
  229. self.anchorLocator = lLocator
  230. self.anchorLocatorType = lLocatorType
  231. logger.debug(f"Anchor set: {lLocatorType} {lLocator}")
  232. else:
  233. logger.error(f"Anchor should be set, but can't be found in the current page: {lLocatorType}, {lLocator}")
  234. raise ValueError(f"Anchor should be set, but can't be found in the current page: {lLocatorType}, {lLocator}")
  235. elif lActivity == 'HANDLEIFRAME':
  236. self.browserSession.handleIframe(lLocator)
  237. elif lActivity == 'SWITCHWINDOW':
  238. lWindow = lValue
  239. if lWindow.isnumeric():
  240. lWindow = int(lWindow)
  241. self.browserSession.handleWindow(windowNumber=lWindow, timeout=lTimeout)
  242. elif lActivity == "CLICK":
  243. self.browserSession.findByAndClick(xpath=xpath, css=css, id=id, timeout=lTimeout)
  244. elif lActivity == "CLICKIF":
  245. self.browserSession.findByAndClickIf(xpath=xpath, css=css, id=id, timeout=lTimeout, value=lValue)
  246. elif lActivity == "PAUSE":
  247. self.browserSession.sleep(seconds=float(lValue))
  248. elif lActivity == "IF":
  249. # Originally we had only Comparisons. Now we also want to check for existance of Field
  250. if not lValue and lLocatorType and lLocator:
  251. lValue = self.browserSession.findBy(xpath=xpath, css=css, id=id, optional=lOptional,
  252. timeout=lTimeout)
  253. self.__doComparisons(lComparison=lComparison, value1=lValue, value2=lValue2)
  254. logger.debug(f"IF-condition {lValue} {lComparison} {lValue2} evaluated to: {self.ifIsTrue} ")
  255. elif lActivity == "ELSE":
  256. if not self.ifIsTrue:
  257. self.manageNestedCondition(condition=lActivity)
  258. logger.debug("Executing ELSE-condition")
  259. elif lActivity == "ENDIF":
  260. self.manageNestedCondition(condition=lActivity)
  261. elif lActivity == "REPEAT":
  262. self.repeatActive += 1
  263. self.repeatIsTrue.append(True)
  264. self.repeatDict.append({})
  265. if original_value not in self.testRunInstance.json_dict:
  266. self.testRunInstance.json_dict[original_value] = []
  267. self.testRunInstance.json_dict[original_value].append(lValue)
  268. self.repeatData.append(lValue)
  269. self.repeatCount.append(lValue2)
  270. elif lActivity == 'GOBACK':
  271. self.browserSession.goBack()
  272. elif lActivity == 'APIURL':
  273. self.apiSession.setBaseURL(lValue)
  274. elif lActivity == 'ENDPOINT':
  275. self.apiSession.setEndPoint(lValue)
  276. elif lActivity == 'POST':
  277. self.apiSession.postURL(content=lValue)
  278. elif lActivity == 'GET':
  279. self.apiSession.getURL()
  280. elif lActivity == 'HEADER':
  281. self.apiSession.setHeaders(setHeaderData=lValue)
  282. elif lActivity == 'SAVE':
  283. self.doSaveData(lValue, lValue2, lLocatorType, lLocator)
  284. elif lActivity == 'CLEAR':
  285. # Clear a variable:
  286. if self.testcaseDataDict.get(lValue):
  287. del self.testcaseDataDict[lValue]
  288. elif lActivity == 'SAVETO':
  289. # In this case, we need to parse the real field, not the representation of the replaced field value
  290. self.doSaveData(command['Value'], lValue2, lLocatorType, lLocator)
  291. elif lActivity == 'SUBMIT':
  292. self.browserSession.submit()
  293. elif lActivity == "ADDRESS_CREATE":
  294. # Create Address with option lValue and lValue2
  295. AddressCreate(lValue, lValue2)
  296. # Update testcaseDataDict with addressDict returned from
  297. AddressCreate.returnAddress()
  298. self.testcaseDataDict.update(AddressCreate.returnAddress())
  299. elif lActivity == 'ASSERT':
  300. value_found = self.browserSession.findByAndWaitForValue(xpath=xpath, css=css, id=id, optional=lOptional,
  301. timeout=lTimeout)
  302. if not self.__doComparisons(lComparison=lComparison, value1=value_found, value2=lValue):
  303. raise baangtTestStepException(f"Expected Value: {lValue}, Value found :{value_found} ")
  304. elif lActivity == 'IBAN':
  305. # Create Random IBAN. Value1 = Input-Parameter for IBAN-Function. Value2=Fieldname
  306. self.__getIBAN(lValue, lValue2)
  307. elif lActivity == 'PDFCOMPARE':
  308. self.doPDFComparison(lValue)
  309. elif lActivity == 'CHECKLINKS':
  310. self.checkLinks()
  311. elif lActivity == 'ALERTIF':
  312. self.browserSession.confirmAlertIfAny()
  313. elif lActivity == GC.TESTCASESTATUS_STOP.upper():
  314. self.testcaseDataDict[GC.TESTCASESTATUS_STOP] = "X" # will stop the test case
  315. elif lActivity == GC.TESTCASESTATUS_STOPERROR.upper():
  316. self.testcaseDataDict[GC.TESTCASESTATUS_STOPERROR] = "X" # will stop the test case and set error
  317. elif lActivity[0:3] == "ZZ_":
  318. # custom command. Do nothing and return
  319. return
  320. else:
  321. raise BaseException(f"Unknown command in TestStep {lActivity}")
  322. self.timing.takeTime(lTimingString)
  323. def _extractAllSingleValues(self, command):
  324. lActivity = command["Activity"].upper()
  325. lLocatorType = command["LocatorType"].upper()
  326. try:
  327. lLocator = self.replaceVariables(command["Locator"])
  328. except Exception as ex:
  329. logger.info(ex)
  330. if lLocator and not lLocatorType: # If locatorType is empty, default it to XPATH
  331. lLocatorType = 'XPATH'
  332. xpath, css, id = self.__setLocator(lLocatorType, lLocator)
  333. return css, id, lActivity, lLocator, lLocatorType, xpath
  334. def doPDFComparison(self, lValue, lFieldnameForResults="DOC_Compare"):
  335. lFiles = self.browserSession.findNewFiles()
  336. if len(lFiles) > 1:
  337. # fixme: Do something! There were more than 1 files since last check. Damn
  338. logger.critical(f"There were {len(lFiles)} files new since last check. Can't handle that. ")
  339. raise Exception
  340. elif len(lFiles) == 1:
  341. # Wonderful. Let's do the PDF-Comparison
  342. lPDFDataClass = PDFCompareDetails()
  343. lPDFDataClass.fileName = lFiles[0][0]
  344. lPDFDataClass.referenceID = lValue
  345. lDict = {"": lPDFDataClass}
  346. lPDFCompare = PDFCompare()
  347. lDict = lPDFCompare.compare_multiple(lDict)
  348. self.testcaseDataDict[lFieldnameForResults + "_Status"] = lDict[""].Status
  349. self.testcaseDataDict[lFieldnameForResults + "_Results"] = lDict[""].StatusText
  350. def replaceAllVariables(self, lValue, lValue2, replaceFromDict=None):
  351. # Replace variables from data file
  352. try:
  353. if len(lValue) > 0:
  354. lValue = self.replaceVariables(lValue, replaceFromDict=replaceFromDict)
  355. if len(lValue2) > 0:
  356. lValue2 = self.replaceVariables(lValue2, replaceFromDict=replaceFromDict)
  357. except Exception as ex:
  358. logger.info(ex)
  359. return lValue, lValue2
  360. def __getIBAN(self, lValue, lValue2):
  361. from baangt.base.IBAN import IBAN
  362. if not lValue2:
  363. logger.critical("IBAN-Method called without destination field name in column 'Value 2'")
  364. return
  365. lIBAN = IBAN()
  366. self.testcaseDataDict[lValue2] = lIBAN.getRandomIBAN()
  367. pass
  368. def checkLinks(self):
  369. """
  370. Will check all links on the current webpage
  371. Result will be written into "CheckedLinks" in TestDataDict
  372. If theres a returncode >= 400 in the list, we'll mark the testcase as failed
  373. """
  374. if self.testcaseDataDict.get("CheckedLinks"):
  375. self.testcaseDataDict["Result_CheckedLinks"] += self.browserSession.checkLinks()
  376. else:
  377. self.testcaseDataDict["Result_CheckedLinks"] = self.browserSession.checkLinks()
  378. for entry in self.testcaseDataDict["Result_CheckedLinks"]:
  379. if entry[0] > 400:
  380. self.testcaseDataDict[GC.TESTCASESTATUS] = GC.TESTCASESTATUS_ERROR
  381. self.testcaseDataDict[GC.TESTCASEERRORLOG] = self.testcaseDataDict.get(GC.TESTCASEERRORLOG, "") \
  382. + "URL-Checker error"
  383. break
  384. @staticmethod
  385. def ifQualifyForExecution(version_global, version_line):
  386. """ This function will test version_global and version_line
  387. @return True or False
  388. """
  389. if not version_global.strip():
  390. # No value is defined in Release, return True
  391. return True
  392. if not version_line.strip():
  393. # we skipped this line
  394. return True
  395. # split the version line
  396. if not len(version_line.strip().split(" ")) == 2:
  397. logger.debug(f"Invalid release format {version_line} ")
  398. return True
  399. comp_operator, version = version_line.strip().split(" ")
  400. if comp_operator == "<":
  401. return parse_version(version_global) < parse_version(version)
  402. elif comp_operator == ">":
  403. return parse_version(version_global) > parse_version(version)
  404. elif comp_operator == ">=":
  405. return parse_version(version_global) >= parse_version(version)
  406. elif comp_operator == "<=":
  407. return parse_version(version_global) <= parse_version(version)
  408. elif comp_operator == "=" or comp_operator == "==":
  409. return parse_version(version_global) == parse_version(version)
  410. else:
  411. logger.debug(f"Global version {version_global}, line version {version_line} ")
  412. return False
  413. def doSaveData(self, toField, valueForField, lLocatorType, lLocator):
  414. """
  415. Save fields. Either from an existing DICT (usually in API-Mode) or from a Webelement (in Browser-Mode)
  416. :param toField:
  417. :param valueForField:
  418. :param lLocatorType:
  419. :param lLocator:
  420. :return: no return parameter. The implicit return is a value in a field.
  421. """
  422. if self.testCase[1][GC.KWARGS_TESTCASETYPE] == GC.KWARGS_BROWSER:
  423. xpath, css, id = TestStepMaster.__setLocator(lLocatorType, lLocator)
  424. self.testcaseDataDict[toField] = self.browserSession.findByAndWaitForValue(xpath=xpath, css=css, id=id)
  425. logger.debug(f"Saved {self.testcaseDataDict[toField]} to {toField}")
  426. elif self.testCase[1][GC.KWARGS_TESTCASETYPE] == GC.KWARGS_API_SESSION:
  427. self.testcaseDataDict[toField] = valueForField
  428. else:
  429. sys.exit("Testcase Type not supported")
  430. @staticmethod
  431. def __setLocator(lLocatorType, lLocator):
  432. return utils.setLocatorFromLocatorType(lLocatorType, lLocator)
  433. @staticmethod
  434. def _setTimeout(lTimeout):
  435. return 20 if not lTimeout else float(lTimeout)
  436. def __doComparisons(self, lComparison, value1, value2):
  437. if isinstance(value1, bool) or isinstance(value2, bool):
  438. value1 = utils.anything2Boolean(value1)
  439. value2 = utils.anything2Boolean(value2)
  440. if value2 == 'None':
  441. value2 = None
  442. if lComparison == "=":
  443. if value1 == value2:
  444. self.manageNestedCondition(condition="IF", ifis=True)
  445. else:
  446. self.manageNestedCondition(condition="IF", ifis=False)
  447. elif lComparison == "!=":
  448. self.ifIsTrue = False if value1 == value2 else True
  449. elif lComparison == ">":
  450. if value1 > value2:
  451. self.manageNestedCondition(condition="IF", ifis=True)
  452. else:
  453. self.manageNestedCondition(condition="IF", ifis=False)
  454. elif lComparison == "<":
  455. if value1 < value2:
  456. self.manageNestedCondition(condition="IF", ifis=True)
  457. else:
  458. self.manageNestedCondition(condition="IF", ifis=False)
  459. elif not lComparison: # Check only, if Value1 has a value.
  460. val = True if value1 else False
  461. self.manageNestedCondition(condition="IF", ifis=val)
  462. else:
  463. raise BaseException(f"Comparison Operator not supported/unknown {lComparison}")
  464. return self.ifIsTrue
  465. def execute(self):
  466. """Method is overwritten in all children/subclasses"""
  467. pass
  468. def teardown(self):
  469. if self.testCaseStatus:
  470. if not self.testcaseDataDict[GC.TESTCASESTATUS]:
  471. self.testcaseDataDict[GC.TESTCASESTATUS] = self.testCaseStatus
  472. elif self.testCaseStatus == GC.TESTCASESTATUS_SUCCESS:
  473. # We'll not overwrite a potential Error Status of the testcase
  474. pass
  475. elif self.testCaseStatus == GC.TESTCASESTATUS_ERROR:
  476. self.testcaseDataDict = GC.TESTCASESTATUS_ERROR
  477. else:
  478. sys.exit("No idea, what happened here. Unknown condition appeared")
  479. self.timing.takeTime(self.timingName)
  480. def replaceVariables(self, expression, replaceFromDict=None):
  481. """
  482. The syntax for variables is currently $(<column_name_from_data_file>). Multiple variables can be assigned
  483. in one cell, for instance perfectly fine: "http://$(BASEURL)/$(ENDPOINT)"
  484. There's a special syntax for the faker module: $(FAKER.<fakermethod>).
  485. Also a special syntax for API-Handling: $(APIHandling.<DictElementName>).
  486. @param expression: the current cell, either as fixed value, e.g. "Franzi" or with a varible $(DATE)
  487. @return: the replaced value, e.g. if expression was $(DATE) and the value in column "DATE" of data-file was
  488. "01.01.2020" then return will be "01.01.2020"
  489. """
  490. if "$(" not in expression:
  491. return expression
  492. while "$(" in expression:
  493. if expression[0:2] == "$(":
  494. left_part = ""
  495. else:
  496. left_part = expression.split("$(")[0]
  497. center = expression[len(left_part) + 2:]
  498. center = center.split(")")[0]
  499. right_part = expression[len(left_part) + len(center) + 3:]
  500. centerValue = ""
  501. if replaceFromDict: # json is supplied with repeat tag, that json is used here to get main data
  502. dic = replaceFromDict
  503. for key in center.split('.')[-1:]:
  504. dic = self.iterate_json(dic, key)
  505. centerValue = dic
  506. if centerValue: # if we got value from the passed json then bypass this if else conditions
  507. pass
  508. elif "." not in center:
  509. # Replace the variable with the value from data structure
  510. centerValue = self.testcaseDataDict.get(center)
  511. else:
  512. # This is a reference to a DICT with ".": for instance APIHandling.AnswerContent("<bla>")
  513. dictVariable = center.split(".")[0]
  514. dictValue = center.split(".")[1]
  515. if dictVariable == 'ANSWER_CONTENT':
  516. centerValue = self.apiSession.session[1].answerJSON.get(dictValue, "Empty")
  517. elif dictVariable == 'FAKER':
  518. # This is to call Faker Module with the Method, that is given after the .
  519. centerValue = self._getFakerData(dictValue)
  520. elif self.testcaseDataDict.get(dictVariable):
  521. dic = self.testcaseDataDict.get(dictVariable)
  522. for key in center.split('.')[1:]:
  523. dic = self.iterate_json(dic, key)
  524. centerValue = dic
  525. else:
  526. raise BaseException(f"Missing code to replace value for: {center}")
  527. if not centerValue:
  528. if center in self.testcaseDataDict.keys():
  529. # The variable exists, but has no value.
  530. centerValue = ""
  531. else:
  532. raise BaseException(f"Variable not found: {center}, input parameter was: {expression}")
  533. expression = "".join([left_part, str(centerValue), right_part])
  534. return expression
  535. def iterate_json(self, data, key):
  536. # itereate through list of json and create list of data from dictionary inside those list
  537. if type(data) is list:
  538. lis = []
  539. for dt in data:
  540. dt = self.iterate_json(dt, key)
  541. lis.append(dt)
  542. return lis
  543. elif type(data) is dict:
  544. return data.get(key)
  545. def _getFakerData(self, fakerMethod):
  546. if not self.baangtFaker:
  547. self.baangtFaker = baangtFaker()
  548. logger.debug(f"Calling faker with method: {fakerMethod}")
  549. return self.baangtFaker.fakerProxy(fakerMethod=fakerMethod)