TestStepMaster.py 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  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. lActivity = command["Activity"].upper()
  179. if lActivity == "COMMENT":
  180. return # Comment's are ignored
  181. lLocatorType = command["LocatorType"].upper()
  182. try:
  183. lLocator = self.replaceVariables(command["Locator"])
  184. except Exception as ex:
  185. logger.info(ex)
  186. if lLocator and not lLocatorType: # If locatorType is empty, default it to XPATH
  187. lLocatorType = 'XPATH'
  188. xpath, css, id = self.__setLocator(lLocatorType, lLocator)
  189. if self.anchor and xpath:
  190. if xpath[0:3] == '///': # Xpath doesn't want to use Anchor
  191. xpath = xpath[1:]
  192. logger.debug(f"Anchor active, but escaped. Using pure xpath: {xpath}")
  193. else:
  194. logger.debug(f"Anchor active. combining {self.anchorLocator} with {xpath}")
  195. xpath = self.anchorLocator + xpath
  196. lValue = str(command["Value"])
  197. lValue2 = str(command["Value2"])
  198. lComparison = command["Comparison"]
  199. lOptional = utils.anything2Boolean(command["Optional"])
  200. # check release line
  201. lRelease = command["Release"]
  202. # Timeout defaults to 20 seconds, if not set otherwise.
  203. lTimeout = TestStepMaster._setTimeout(command["Timeout"])
  204. lTimingString = f"TS {commandNumber} {lActivity.lower()}"
  205. self.timing.takeTime(lTimingString)
  206. logger.info(
  207. f"Executing TestStepDetail {commandNumber} with parameters: act={lActivity}, lType={lLocatorType}, loc={lLocator}, "
  208. f"Val1={lValue}, comp={lComparison}, Val2={lValue2}, Optional={lOptional}, timeout={lTimeout}")
  209. original_value = lValue # used as key to make rlp json dict and will be used further to make sheet name
  210. lValue, lValue2 = self.replaceAllVariables(lValue, lValue2, replaceFromDict=replaceFromDict)
  211. if not TestStepMaster.ifQualifyForExecution(self.globalRelease, lRelease):
  212. logger.debug(f"we skipped this line due to {lRelease} disqualifies according to {self.globalRelease} ")
  213. return
  214. if lActivity == "GOTOURL":
  215. self.browserSession.goToUrl(lValue)
  216. elif lActivity == "SETTEXT":
  217. self.browserSession.findByAndSetText(xpath=xpath, css=css, id=id, value=lValue, timeout=lTimeout)
  218. elif lActivity == "SETTEXTIF":
  219. self.browserSession.findByAndSetTextIf(xpath=xpath, css=css, id=id, value=lValue, timeout=lTimeout,
  220. optional=lOptional)
  221. elif lActivity == "FORCETEXT":
  222. self.browserSession.findByAndForceText(xpath=xpath, css=css, id=id, value=lValue, timeout=lTimeout)
  223. elif lActivity == "SETANCHOR":
  224. if not lLocator:
  225. self.anchor = None
  226. self.anchorLocator = None
  227. self.anchorLocatorType = None
  228. else:
  229. found = self.browserSession.findBy(xpath=xpath, css=css, id=id, timeout=lTimeout)
  230. if found:
  231. self.anchor = self.browserSession.element
  232. self.anchorLocator = lLocator
  233. self.anchorLocatorType = lLocatorType
  234. logger.debug(f"Anchor set: {lLocatorType} {lLocator}")
  235. else:
  236. logger.error(f"Anchor should be set, but can't be found in the current page: {lLocatorType}, {lLocator}")
  237. raise ValueError(f"Anchor should be set, but can't be found in the current page: {lLocatorType}, {lLocator}")
  238. elif lActivity == 'HANDLEIFRAME':
  239. self.browserSession.handleIframe(lLocator)
  240. elif lActivity == 'SWITCHWINDOW':
  241. lWindow = lValue
  242. if lWindow.isnumeric():
  243. lWindow = int(lWindow)
  244. self.browserSession.handleWindow(windowNumber=lWindow, timeout=lTimeout)
  245. elif lActivity == "CLICK":
  246. self.browserSession.findByAndClick(xpath=xpath, css=css, id=id, timeout=lTimeout)
  247. elif lActivity == "CLICKIF":
  248. self.browserSession.findByAndClickIf(xpath=xpath, css=css, id=id, timeout=lTimeout, value=lValue)
  249. elif lActivity == "PAUSE":
  250. self.browserSession.sleep(seconds=float(lValue))
  251. elif lActivity == "IF":
  252. # Originally we had only Comparisons. Now we also want to check for existance of Field
  253. if not lValue and lLocatorType and lLocator:
  254. lValue = self.browserSession.findBy(xpath=xpath, css=css, id=id, optional=lOptional,
  255. timeout=lTimeout)
  256. self.__doComparisons(lComparison=lComparison, value1=lValue, value2=lValue2)
  257. logger.debug(f"IF-condition {lValue} {lComparison} {lValue2} evaluated to: {self.ifIsTrue} ")
  258. elif lActivity == "ELSE":
  259. if not self.ifIsTrue:
  260. self.manageNestedCondition(condition=lActivity)
  261. logger.debug("Executing ELSE-condition")
  262. elif lActivity == "ENDIF":
  263. self.manageNestedCondition(condition=lActivity)
  264. elif lActivity == "REPEAT":
  265. self.repeatActive += 1
  266. self.repeatIsTrue.append(True)
  267. self.repeatDict.append({})
  268. if original_value not in self.testRunInstance.json_dict:
  269. self.testRunInstance.json_dict[original_value] = []
  270. self.testRunInstance.json_dict[original_value].append(lValue)
  271. self.repeatData.append(lValue)
  272. self.repeatCount.append(lValue2)
  273. elif lActivity == 'GOBACK':
  274. self.browserSession.goBack()
  275. elif lActivity == 'APIURL':
  276. self.apiSession.setBaseURL(lValue)
  277. elif lActivity == 'ENDPOINT':
  278. self.apiSession.setEndPoint(lValue)
  279. elif lActivity == 'POST':
  280. self.apiSession.postURL(content=lValue)
  281. elif lActivity == 'GET':
  282. self.apiSession.getURL()
  283. elif lActivity == 'HEADER':
  284. self.apiSession.setHeaders(setHeaderData=lValue)
  285. elif lActivity == 'SAVE':
  286. self.doSaveData(lValue, lValue2, lLocatorType, lLocator)
  287. elif lActivity == 'CLEAR':
  288. # Clear a variable:
  289. if self.testcaseDataDict.get(lValue):
  290. del self.testcaseDataDict[lValue]
  291. elif lActivity == 'SAVETO':
  292. # In this case, we need to parse the real field, not the representation of the replaced field value
  293. self.doSaveData(command['Value'], lValue2, lLocatorType, lLocator)
  294. elif lActivity == 'SUBMIT':
  295. self.browserSession.submit()
  296. elif lActivity == "ADDRESS_CREATE":
  297. # Create Address with option lValue and lValue2
  298. AddressCreate(lValue, lValue2)
  299. # Update testcaseDataDict with addressDict returned from
  300. AddressCreate.returnAddress()
  301. self.testcaseDataDict.update(AddressCreate.returnAddress())
  302. elif lActivity == 'ASSERT':
  303. value_found = self.browserSession.findByAndWaitForValue(xpath=xpath, css=css, id=id, optional=lOptional,
  304. timeout=lTimeout)
  305. if not self.__doComparisons(lComparison=lComparison, value1=value_found, value2=lValue):
  306. raise baangtTestStepException(f"Expected Value: {lValue}, Value found :{value_found} ")
  307. elif lActivity == 'IBAN':
  308. # Create Random IBAN. Value1 = Input-Parameter for IBAN-Function. Value2=Fieldname
  309. self.__getIBAN(lValue, lValue2)
  310. elif lActivity == 'PDFCOMPARE':
  311. self.doPDFComparison(lValue)
  312. elif lActivity == 'CHECKLINKS':
  313. self.checkLinks()
  314. elif lActivity == 'ALERTIF':
  315. self.browserSession.confirmAlertIfAny()
  316. elif lActivity == GC.TESTCASESTATUS_STOP.upper():
  317. self.testcaseDataDict[GC.TESTCASESTATUS_STOP] = "X" # will stop the test case
  318. elif lActivity == GC.TESTCASESTATUS_STOPERROR.upper():
  319. self.testcaseDataDict[GC.TESTCASESTATUS_STOPERROR] = "X" # will stop the test case and set error
  320. elif lActivity[0:3] == "ZZ_":
  321. # custom command. Do nothing and return
  322. return
  323. else:
  324. raise BaseException(f"Unknown command in TestStep {lActivity}")
  325. self.timing.takeTime(lTimingString)
  326. def doPDFComparison(self, lValue, lFieldnameForResults="DOC_Compare"):
  327. lFiles = self.browserSession.findNewFiles()
  328. if len(lFiles) > 1:
  329. # fixme: Do something! There were more than 1 files since last check. Damn
  330. logger.critical(f"There were {len(lFiles)} files new since last check. Can't handle that. ")
  331. raise Exception
  332. elif len(lFiles) == 1:
  333. # Wonderful. Let's do the PDF-Comparison
  334. lPDFDataClass = PDFCompareDetails()
  335. lPDFDataClass.fileName = lFiles[0][0]
  336. lPDFDataClass.referenceID = lValue
  337. lDict = {"": lPDFDataClass}
  338. lPDFCompare = PDFCompare()
  339. lDict = lPDFCompare.compare_multiple(lDict)
  340. self.testcaseDataDict[lFieldnameForResults + "_Status"] = lDict[""].Status
  341. self.testcaseDataDict[lFieldnameForResults + "_Results"] = lDict[""].StatusText
  342. def replaceAllVariables(self, lValue, lValue2, replaceFromDict=None):
  343. # Replace variables from data file
  344. try:
  345. if len(lValue) > 0:
  346. lValue = self.replaceVariables(lValue, replaceFromDict=replaceFromDict)
  347. if len(lValue2) > 0:
  348. lValue2 = self.replaceVariables(lValue2, replaceFromDict=replaceFromDict)
  349. except Exception as ex:
  350. logger.info(ex)
  351. return lValue, lValue2
  352. def __getIBAN(self, lValue, lValue2):
  353. from baangt.base.IBAN import IBAN
  354. if not lValue2:
  355. logger.critical("IBAN-Method called without destination field name in column 'Value 2'")
  356. return
  357. lIBAN = IBAN()
  358. self.testcaseDataDict[lValue2] = lIBAN.getRandomIBAN()
  359. pass
  360. def checkLinks(self):
  361. """
  362. Will check all links on the current webpage
  363. Result will be written into "CheckedLinks" in TestDataDict
  364. If theres a returncode >= 400 in the list, we'll mark the testcase as failed
  365. """
  366. if self.testcaseDataDict.get("CheckedLinks"):
  367. self.testcaseDataDict["Result_CheckedLinks"] += self.browserSession.checkLinks()
  368. else:
  369. self.testcaseDataDict["Result_CheckedLinks"] = self.browserSession.checkLinks()
  370. for entry in self.testcaseDataDict["Result_CheckedLinks"]:
  371. if entry[0] > 400:
  372. self.testcaseDataDict[GC.TESTCASESTATUS] = GC.TESTCASESTATUS_ERROR
  373. self.testcaseDataDict[GC.TESTCASEERRORLOG] = self.testcaseDataDict.get(GC.TESTCASEERRORLOG, "") \
  374. + "URL-Checker error"
  375. break
  376. @staticmethod
  377. def ifQualifyForExecution(version_global, version_line):
  378. """ This function will test version_global and version_line
  379. @return True or False
  380. """
  381. if not version_global.strip():
  382. # No value is defined in Release, return True
  383. return True
  384. if not version_line.strip():
  385. # we skipped this line
  386. return True
  387. # split the version line
  388. if not len(version_line.strip().split(" ")) == 2:
  389. logger.debug(f"Invalid release format {version_line} ")
  390. return True
  391. comp_operator, version = version_line.strip().split(" ")
  392. if comp_operator == "<":
  393. return parse_version(version_global) < parse_version(version)
  394. elif comp_operator == ">":
  395. return parse_version(version_global) > parse_version(version)
  396. elif comp_operator == ">=":
  397. return parse_version(version_global) >= parse_version(version)
  398. elif comp_operator == "<=":
  399. return parse_version(version_global) <= parse_version(version)
  400. elif comp_operator == "=" or comp_operator == "==":
  401. return parse_version(version_global) == parse_version(version)
  402. else:
  403. logger.debug(f"Global version {version_global}, line version {version_line} ")
  404. return False
  405. def doSaveData(self, toField, valueForField, lLocatorType, lLocator):
  406. """
  407. Save fields. Either from an existing DICT (usually in API-Mode) or from a Webelement (in Browser-Mode)
  408. :param toField:
  409. :param valueForField:
  410. :param lLocatorType:
  411. :param lLocator:
  412. :return: no return parameter. The implicit return is a value in a field.
  413. """
  414. if self.testCase[1][GC.KWARGS_TESTCASETYPE] == GC.KWARGS_BROWSER:
  415. xpath, css, id = TestStepMaster.__setLocator(lLocatorType, lLocator)
  416. self.testcaseDataDict[toField] = self.browserSession.findByAndWaitForValue(xpath=xpath, css=css, id=id)
  417. logger.debug(f"Saved {self.testcaseDataDict[toField]} to {toField}")
  418. elif self.testCase[1][GC.KWARGS_TESTCASETYPE] == GC.KWARGS_API_SESSION:
  419. self.testcaseDataDict[toField] = valueForField
  420. else:
  421. sys.exit("Testcase Type not supported")
  422. @staticmethod
  423. def __setLocator(lLocatorType, lLocator):
  424. return utils.setLocatorFromLocatorType(lLocatorType, lLocator)
  425. @staticmethod
  426. def _setTimeout(lTimeout):
  427. return 20 if not lTimeout else float(lTimeout)
  428. def __doComparisons(self, lComparison, value1, value2):
  429. if isinstance(value1, bool) or isinstance(value2, bool):
  430. value1 = utils.anything2Boolean(value1)
  431. value2 = utils.anything2Boolean(value2)
  432. if value2 == 'None':
  433. value2 = None
  434. if lComparison == "=":
  435. if value1 == value2:
  436. self.manageNestedCondition(condition="IF", ifis=True)
  437. else:
  438. self.manageNestedCondition(condition="IF", ifis=False)
  439. elif lComparison == "!=":
  440. self.ifIsTrue = False if value1 == value2 else True
  441. elif lComparison == ">":
  442. if value1 > value2:
  443. self.manageNestedCondition(condition="IF", ifis=True)
  444. else:
  445. self.manageNestedCondition(condition="IF", ifis=False)
  446. elif lComparison == "<":
  447. if value1 < value2:
  448. self.manageNestedCondition(condition="IF", ifis=True)
  449. else:
  450. self.manageNestedCondition(condition="IF", ifis=False)
  451. elif not lComparison: # Check only, if Value1 has a value.
  452. val = True if value1 else False
  453. self.manageNestedCondition(condition="IF", ifis=val)
  454. else:
  455. raise BaseException(f"Comparison Operator not supported/unknown {lComparison}")
  456. return self.ifIsTrue
  457. def execute(self):
  458. """Method is overwritten in all children/subclasses"""
  459. pass
  460. def teardown(self):
  461. if self.testCaseStatus:
  462. if not self.testcaseDataDict[GC.TESTCASESTATUS]:
  463. self.testcaseDataDict[GC.TESTCASESTATUS] = self.testCaseStatus
  464. elif self.testCaseStatus == GC.TESTCASESTATUS_SUCCESS:
  465. # We'll not overwrite a potential Error Status of the testcase
  466. pass
  467. elif self.testCaseStatus == GC.TESTCASESTATUS_ERROR:
  468. self.testcaseDataDict = GC.TESTCASESTATUS_ERROR
  469. else:
  470. sys.exit("No idea, what happened here. Unknown condition appeared")
  471. self.timing.takeTime(self.timingName)
  472. def replaceVariables(self, expression, replaceFromDict=None):
  473. """
  474. The syntax for variables is currently $(<column_name_from_data_file>). Multiple variables can be assigned
  475. in one cell, for instance perfectly fine: "http://$(BASEURL)/$(ENDPOINT)"
  476. There's a special syntax for the faker module: $(FAKER.<fakermethod>).
  477. Also a special syntax for API-Handling: $(APIHandling.<DictElementName>).
  478. @param expression: the current cell, either as fixed value, e.g. "Franzi" or with a varible $(DATE)
  479. @return: the replaced value, e.g. if expression was $(DATE) and the value in column "DATE" of data-file was
  480. "01.01.2020" then return will be "01.01.2020"
  481. """
  482. if "$(" not in expression:
  483. return expression
  484. while "$(" in expression:
  485. if expression[0:2] == "$(":
  486. left_part = ""
  487. else:
  488. left_part = expression.split("$(")[0]
  489. center = expression[len(left_part) + 2:]
  490. center = center.split(")")[0]
  491. right_part = expression[len(left_part) + len(center) + 3:]
  492. centerValue = ""
  493. if replaceFromDict: # json is supplied with repeat tag, that json is used here to get main data
  494. dic = replaceFromDict
  495. for key in center.split('.')[-1:]:
  496. dic = self.iterate_json(dic, key)
  497. centerValue = dic
  498. if centerValue: # if we got value from the passed json then bypass this if else conditions
  499. pass
  500. elif "." not in center:
  501. # Replace the variable with the value from data structure
  502. centerValue = self.testcaseDataDict.get(center)
  503. else:
  504. # This is a reference to a DICT with ".": for instance APIHandling.AnswerContent("<bla>")
  505. dictVariable = center.split(".")[0]
  506. dictValue = center.split(".")[1]
  507. if dictVariable == 'ANSWER_CONTENT':
  508. centerValue = self.apiSession.session[1].answerJSON.get(dictValue, "Empty")
  509. elif dictVariable == 'FAKER':
  510. # This is to call Faker Module with the Method, that is given after the .
  511. centerValue = self._getFakerData(dictValue)
  512. elif self.testcaseDataDict.get(dictVariable):
  513. dic = self.testcaseDataDict.get(dictVariable)
  514. for key in center.split('.')[1:]:
  515. dic = self.iterate_json(dic, key)
  516. centerValue = dic
  517. else:
  518. raise BaseException(f"Missing code to replace value for: {center}")
  519. if not centerValue:
  520. if center in self.testcaseDataDict.keys():
  521. # The variable exists, but has no value.
  522. centerValue = ""
  523. else:
  524. raise BaseException(f"Variable not found: {center}, input parameter was: {expression}")
  525. expression = "".join([left_part, str(centerValue), right_part])
  526. return expression
  527. def iterate_json(self, data, key):
  528. # itereate through list of json and create list of data from dictionary inside those list
  529. if type(data) is list:
  530. lis = []
  531. for dt in data:
  532. dt = self.iterate_json(dt, key)
  533. lis.append(dt)
  534. return lis
  535. elif type(data) is dict:
  536. return data.get(key)
  537. def _getFakerData(self, fakerMethod):
  538. if not self.baangtFaker:
  539. self.baangtFaker = baangtFaker()
  540. logger.debug(f"Calling faker with method: {fakerMethod}")
  541. return self.baangtFaker.fakerProxy(fakerMethod=fakerMethod)