test_ServiceTest.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. import os
  2. import glob
  3. import xlrd3 as xlrd
  4. import subprocess
  5. from pathlib import Path
  6. from baangt.base.DownloadFolderMonitoring import DownloadFolderMonitoring
  7. from baangt.base.BrowserFactory import BrowserDriver
  8. from baangt.base import GlobalConstants as GC
  9. from baangt.base.PathManagement import ManagedPaths
  10. from baangt.base.TestRun.TestRun import TestRun
  11. from uuid import uuid4
  12. import psutil
  13. import pytest
  14. import platform
  15. from sqlalchemy import create_engine
  16. from sqlalchemy.orm import sessionmaker
  17. import uuid
  18. from baangt.base.DataBaseORM import TestrunLog
  19. import json
  20. # Will Check for the current directory and change it to baangt root dir
  21. if not os.path.basename(os.getcwd()) == "baangt":
  22. if os.path.basename(os.path.dirname(os.getcwd())) == "baangt":
  23. os.chdir('..')
  24. else:
  25. assert 0, "Please run the test from baangt/ or baangt/tests/ directory."
  26. # Paths
  27. current_dir = os.getcwd()
  28. managed_path = ManagedPaths()
  29. output_dir = Path(managed_path.getOrSetExportPath())
  30. input_dir = Path(current_dir).joinpath("tests/0TestInput/ServiceTestInput")
  31. input_file = str(Path(current_dir).joinpath("tests/0TestInput/ServiceTestInput/simpleAutomationpractice_small.xlsx"))
  32. input_file_parallel = str(Path(input_dir).joinpath("simpleAutomationpractice.xlsx"))
  33. drivers_folder = Path(managed_path.getOrSetDriverPath())
  34. isLinux = True if platform.system().upper() == "LINUX" else False
  35. # Creates a copy of your environment
  36. my_env = os.environ.copy()
  37. # Use if you get error 'java' command not found even after it is installed in your system
  38. # Replace the path inside "" to the bin folder of java.
  39. my_env["PATH"] = r"C:\Program Files (x86)\Java\jre1.8.0_251\bin;" + my_env["PATH"]
  40. # To monitor new download files
  41. folder_monitor = DownloadFolderMonitoring(str(output_dir))
  42. # Program execution functions
  43. def execute_from_main(run_file, globals_file):
  44. # Execute the main baangt program with TestRunFile and globals file
  45. subprocess.call(
  46. "python baangt.py --run "+run_file+" --globals "+globals_file,
  47. shell=True, env=my_env
  48. )
  49. def execute(run_file, globals_file):
  50. # Execute the program using TestRun
  51. lUUID = uuid4()
  52. lTestRun = TestRun(run_file, globalSettingsFileNameAndPath=globals_file, uuid=lUUID)
  53. return lTestRun
  54. # Testing Output Functions
  55. def check_output(xlsx_file):
  56. workbook = xlrd.open_workbook(xlsx_file)
  57. book = workbook.sheet_by_name("Summary")
  58. test_records = book.row(2)[1].value or 0
  59. success = book.row(3)[1].value or 0
  60. error = book.row(5)[1].value or 0
  61. test_records = int(test_records)
  62. success = int(success)
  63. error = int(error)
  64. assert success >= test_records/2
  65. def check_browsermob_output(xlsx_file):
  66. workbook = xlrd.open_workbook(xlsx_file)
  67. book = workbook.sheet_by_name("Network")
  68. assert book.nrows > 25
  69. # 20.5.2020: This is covered in test_browserHandling.
  70. # def test_download_browser_drivers():
  71. # # Will delete the pre-existing browsers and download new.
  72. # file_list = glob.glob(str(drivers_folder.joinpath('*')))
  73. # for file in file_list:
  74. # print(file)
  75. # os.remove(file)
  76. # driver_folder_monitor = DownloadFolderMonitoring(str(drivers_folder))
  77. # BrowserDriver.downloadDriver(GC.BROWSER_FIREFOX)
  78. # BrowserDriver.downloadDriver(GC.BROWSER_CHROME)
  79. # new_drivers = driver_folder_monitor.getNewFiles()
  80. # assert driver_folder_monitor
  81. # for drivers in new_drivers:
  82. # print(drivers[0], "downloaded")
  83. # return "Downloading drivers test succeeded"
  84. #
  85. # Firefox testing section
  86. def test_regular_firefox():
  87. # Will run the main program with normal regular globals settings
  88. run_file = input_file
  89. globals_file = Path(input_dir).joinpath("globals_ff.json").as_posix()
  90. execute(run_file, globals_file)
  91. new_file = folder_monitor.getNewFiles()
  92. assert new_file
  93. output_file = output_dir.joinpath(new_file[0][0]).as_posix()
  94. check_output(output_file)
  95. return "Firefox regular test succeed output file =", new_file[0][0]
  96. def test_parellel_firefox():
  97. # Will run the main program with 2 browsers running parallel
  98. run_file = input_file_parallel
  99. globals_file = Path(input_dir).joinpath("globals_parellel_ff.json").as_posix()
  100. execute(run_file, globals_file)
  101. new_file = folder_monitor.getNewFiles()
  102. assert new_file
  103. output_file = output_dir.joinpath(new_file[0][0]).as_posix()
  104. check_output(output_file)
  105. return "Firefox parellel test succeed output file =", new_file[0][0]
  106. def test_browsermob_proxy_firefox():
  107. # Will run the main program with browsermob proxy mode
  108. run_file = input_file
  109. globals_file = Path(input_dir).joinpath("globals_browsermob_proxy_ff.json").as_posix()
  110. execute(run_file, globals_file)
  111. new_file = folder_monitor.getNewFiles()
  112. assert new_file
  113. output_file = output_dir.joinpath(new_file[0][0]).as_posix()
  114. check_output(output_file)
  115. check_browsermob_output(output_file)
  116. return "Firefox Browsermob test succeed output file =", new_file[0][0]
  117. def test_headless_firefox():
  118. # Will run the main program with headless browser
  119. run_file = input_file
  120. globals_file = Path(input_dir).joinpath("globals_headless_ff.json").as_posix()
  121. execute(run_file, globals_file)
  122. new_file = folder_monitor.getNewFiles()
  123. assert new_file
  124. output_file = output_dir.joinpath(new_file[0][0]).as_posix()
  125. check_output(output_file)
  126. return "Firefox headless test succeed output file =", new_file[0][0]
  127. def test_csv_firefox():
  128. # Will run the main program for csv output
  129. run_file = input_file
  130. globals_file = Path(input_dir).joinpath("globals_csv_ff.json").as_posix()
  131. execute(run_file, globals_file)
  132. new_file = folder_monitor.getNewFiles()
  133. assert new_file
  134. assert ".csv" in new_file[0][0]
  135. return "Firefox Output Format test succeed output file =", new_file[0][0]
  136. # Chrome Testing Section
  137. @pytest.mark.skipif(isLinux, reason="Chrome not stable on Linux in Docker")
  138. def test_regular_chrome():
  139. # Will run the main program with normal regular globals settings
  140. run_file = input_file
  141. globals_file = Path(input_dir).joinpath("globals_chrome.json").as_posix()
  142. execute(run_file, globals_file)
  143. new_file = folder_monitor.getNewFiles()
  144. assert new_file
  145. output_file = output_dir.joinpath(new_file[0][0]).as_posix()
  146. check_output(output_file)
  147. return "Chrome regular test succeed output file =", new_file[0][0]
  148. @pytest.mark.skipif(isLinux, reason="Chrome not stable on Linux in Docker")
  149. def test_parellel_chrome():
  150. # Will run the main program with 2 browsers running parallel
  151. run_file = input_file_parallel
  152. globals_file = Path(input_dir).joinpath("globals_parellel_chrome.json").as_posix()
  153. execute(run_file, globals_file)
  154. new_file = folder_monitor.getNewFiles()
  155. assert new_file
  156. output_file = output_dir.joinpath(new_file[0][0]).as_posix()
  157. check_output(output_file)
  158. return "Chrome parellel test succeed output file =", new_file[0][0]
  159. @pytest.mark.skipif(isLinux, reason="Chrome not stable on Linux in Docker")
  160. def test_browsermob_proxy_chrome():
  161. # Will run the main program with browsermob proxy mode
  162. run_file = input_file
  163. globals_file = Path(input_dir).joinpath("globals_browsermob_proxy_chrome.json").as_posix()
  164. execute(run_file, globals_file)
  165. new_file = folder_monitor.getNewFiles()
  166. assert new_file
  167. output_file = output_dir.joinpath(new_file[0][0]).as_posix()
  168. check_output(output_file)
  169. check_browsermob_output(output_file)
  170. return "Chrome Browsermob test succeed output file =", new_file[0][0]
  171. @pytest.mark.skipif(isLinux, reason="Chrome not stable on Linux in Docker")
  172. def test_headless_chrome():
  173. # Will run the main program with headless browser
  174. for proc in psutil.process_iter():
  175. if proc.name() == "browsermob-proxy":
  176. proc.kill()
  177. run_file = input_file
  178. globals_file = Path(input_dir).joinpath("globals_headless_chrome.json").as_posix()
  179. execute(run_file, globals_file)
  180. new_file = folder_monitor.getNewFiles()
  181. assert new_file
  182. output_file = output_dir.joinpath(new_file[0][0]).as_posix()
  183. check_output(output_file)
  184. return "Chrome headless test succeed output file =", new_file[0][0]
  185. @pytest.mark.skipif(isLinux, reason="Chrome not stable on Linux in Docker")
  186. def test_csv_chrome():
  187. # Will run the main program for csv output
  188. run_file = input_file
  189. globals_file = Path(input_dir).joinpath("globals_csv_chrome.json").as_posix()
  190. execute(run_file, globals_file)
  191. new_file = folder_monitor.getNewFiles()
  192. assert new_file
  193. assert ".csv" in new_file[0][0]
  194. return "Chrome Output Format test succeed output file =", new_file[0][0]
  195. def test_full_BaangtWebDemo():
  196. run_file = str(input_dir.joinpath("CompleteBaangtWebdemo.xlsx"))
  197. execute(run_file, globals_file=Path(input_dir).joinpath("globals_ff.json"))
  198. new_file = folder_monitor.getNewFiles()
  199. assert new_file
  200. output_file = output_dir.joinpath(new_file[0][0]).as_posix()
  201. check_output(output_file)
  202. def test_NestedIfElse_with_NoBrowser():
  203. run_file = str(input_dir.joinpath("CompleteBaangtWebdemo_else.xlsx"))
  204. testRun = execute(run_file, globals_file=Path(input_dir).joinpath("globalsNoBrowser.json"))
  205. assert testRun.statistics.teststep_executed == 18, "If/Else/Endif are not working as expected"
  206. new_file = folder_monitor.getNewFiles()
  207. assert new_file
  208. output_file = output_dir.joinpath(new_file[0][0]).as_posix()
  209. check_output(output_file)
  210. def test_NestedIfElse_with_greater_endif():
  211. run_file = str(input_dir.joinpath("CompleteBaangtWebdemo_else_error.xlsx"))
  212. try:
  213. execute(run_file, globals_file=Path(input_dir).joinpath("globalsNoBrowser.json"))
  214. assert 1 == 0
  215. except BaseException:
  216. assert 1 == 1
  217. def test_NestedLoops_and_repeat():
  218. run_file = str(input_dir.joinpath("CompleteBaangtWebdemo_nested.xlsx"))
  219. execute(run_file, globals_file=Path(input_dir).joinpath("globalsNoBrowser.json"))
  220. managedPaths = ManagedPaths()
  221. DATABASE_URL = os.getenv('DATABASE_URL') or 'sqlite:///' + str(
  222. managedPaths.derivePathForOSAndInstallationOption().joinpath('testrun.db'))
  223. new_file = folder_monitor.getNewFiles()
  224. assert new_file
  225. output_file = output_dir.joinpath(new_file[0][0]).as_posix()
  226. wb = xlrd.open_workbook(output_file)
  227. sheet1 = wb.sheet_by_name("Test_textarea2")
  228. sheet2 = wb.sheet_by_name("Test_textarea2.nested")
  229. assert sheet1.nrows == 3
  230. assert sheet2.nrows == 3
  231. TestRunSheet = wb.sheet_by_name("Summary")
  232. TestRunUUID = TestRunSheet.cell_value(8, 1)
  233. engine = create_engine(DATABASE_URL)
  234. Session = sessionmaker(bind=engine)
  235. s = Session()
  236. data = s.query(TestrunLog).get(uuid.UUID(TestRunUUID).bytes)
  237. assert "textarea2" in json.loads(data.RLPJson)
  238. def test_JsonTestRun():
  239. run_file = str(input_dir.joinpath("example_googleImages.json"))
  240. execute(run_file, globals_file=Path(input_dir).joinpath("globals.json"))
  241. new_file = folder_monitor.getNewFiles()
  242. assert new_file