populate_db.py 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. import json
  2. import xlrd
  3. from app import db
  4. from app.models import *
  5. #DATABASE_URL = 'sqlite:///testrun.db'
  6. #engine = create_engine(DATABASE_URL)
  7. # create a db.session
  8. #db.session = db.sessionmaker(bind=engine)
  9. #db.session = db.session()
  10. # recreate db structure
  11. db.drop_all()
  12. db.create_all()
  13. # create users
  14. print('Creating users...')
  15. admin = User(username='admin')
  16. admin.set_password('12345')
  17. user = User(username='simple_user')
  18. user.set_password('12345')
  19. db.session.add(admin)
  20. db.session.add(user)
  21. db.session.commit()
  22. print('Done.')
  23. # create supports
  24. print('Creating supports...')
  25. browsers = {
  26. 'FF': 'Mozilla Firefox',
  27. 'Chrome': 'Google Chrome',
  28. 'IE': 'MS Internet Exploer',
  29. 'Safari': 'Safari',
  30. 'Edge': 'MS Edge',
  31. }
  32. for key, value in browsers.items():
  33. browser = BrowserType(name=key, description=value)
  34. db.session.add(browser)
  35. testcases = {
  36. 'Browser': 'Browser',
  37. 'API-Rest': 'API-Rest',
  38. 'API-SOAP': 'API-SOAP',
  39. 'API-oDataV2': 'API-oDataV2',
  40. 'API-oDataV4': 'API-oDataV4',
  41. }
  42. for key, value in testcases.items():
  43. testcase = TestCaseType(name=key, description=value)
  44. db.session.add(testcase)
  45. activities = {
  46. 'GOTOURL': 'Go to an URL',
  47. 'SETTEXT': 'Set Text of an Element',
  48. 'CLICK': 'Click on an Element',
  49. 'COMMENT': 'A Simple Comment',
  50. }
  51. for key, value in activities.items():
  52. activity = ActivityType(name=key, description=value)
  53. db.session.add(activity)
  54. locators = {
  55. 'xpath': 'Locate via XPATH-Expression',
  56. 'css': 'Locate via CSS-Path of the Element',
  57. 'id': 'Locate via ID of the Element',
  58. }
  59. for key, value in locators.items():
  60. locator = LocatorType(name=key, description=value)
  61. db.session.add(locator)
  62. classnames = {
  63. 'Class A': 'A Simple Class Name',
  64. 'Class B': 'One more Simple Class Name',
  65. 'Class C': 'A Complex Class Name',
  66. }
  67. for key, value in classnames.items():
  68. classname = ClassName(name=key, description=value)
  69. db.session.add(classname)
  70. db.session.commit()
  71. print('Done.')
  72. # create mains
  73. print('Creating mains...')
  74. for i in range(5):
  75. testrun = Testrun(
  76. name=f'Testrun #{i}',
  77. description=f'Testrun #{i} is intended for testing the application UI. There are several features wich are described here.',
  78. creator=admin,
  79. )
  80. db.session.add(testrun)
  81. db.session.commit()
  82. for i in range(5):
  83. if i < 3:
  84. u = admin
  85. else:
  86. u = user
  87. testseq = TestCaseSequence(
  88. name=f'Test Case Sequence #{i}',
  89. description=f'Test Case Sequence #{i} is intended for testing the application UI. There are several features wich are described here.',
  90. creator=u,
  91. classname=classname,
  92. )
  93. testseq.testrun.append(testrun)
  94. if i == 2 or i == 3:
  95. another_testrun = Testrun.query.get(i)
  96. testseq.testrun.append(another_testrun)
  97. db.session.add(testseq)
  98. db.session.commit()
  99. for i in range(5):
  100. if i < 3:
  101. u = admin
  102. else:
  103. u = user
  104. datafile = DataFile(
  105. filename=f'data_file_{i}.xlsx',
  106. creator=u,
  107. )
  108. testseq = TestCaseSequence.query.get(i+1)
  109. datafile.testcase_sequence.append(testseq)
  110. db.session.add(datafile)
  111. db.session.commit()
  112. # get supports
  113. browsers = BrowserType.query.all()
  114. testtypes = TestCaseType.query.all()
  115. activities = ActivityType.query.all()
  116. locators = LocatorType.query.all()
  117. for i in range(12):
  118. if i%3 == 0:
  119. u = admin
  120. else:
  121. u = user
  122. testcase = TestCase(
  123. name=f'Test Case #{i}',
  124. description=f'Test Case #{i} is intended for testing the application UI. There are several features wich are described here.',
  125. creator=u,
  126. classname=classname,
  127. browser_type=browsers[i%len(browsers)],
  128. testcase_type=testtypes[i%len(testtypes)],
  129. )
  130. testseq = TestCaseSequence.query.get(i%5 + 1)
  131. testcase.testcase_sequence.append(testseq)
  132. db.session.add(testcase)
  133. db.session.commit()
  134. for i in range(7):
  135. if i%3 == 0:
  136. u = admin
  137. else:
  138. u = user
  139. teststepseq = TestStepSequence(
  140. name=f'Test Step Sequence #{i}',
  141. description=f'Test Step Sequence #{i} is intended for testing the application UI. There are several features wich are described here.',
  142. creator=u,
  143. classname=classname,
  144. )
  145. teststepseq.testcase.append(testcase)
  146. db.session.add(testcase)
  147. db.session.commit()
  148. '''
  149. for i in range(4):
  150. if i%3 == 0:
  151. u = admin
  152. else:
  153. u = user
  154. teststepex = TestStepExecution(
  155. name=f'Test Step Execution #{i}',
  156. description=f'Test Step Execution #{i} is intended for testing the application UI. There are several features wich are described here.',
  157. creator=u,
  158. activity_type=activities[i%len(activities)],
  159. locator_type=locators[i%len(locators)],
  160. teststep_sequence=TestStepSequence.query.get(i%2 + 1),
  161. )
  162. db.session.add(testcase)
  163. '''
  164. # get teststeps from json
  165. print('Creating from JSON...')
  166. path_to_json = 'teststeps.json'
  167. with open(path_to_json, 'r') as f:
  168. steps = json.load(f)
  169. # populate teststeps
  170. for step in steps:
  171. # get activity type
  172. for a in activities:
  173. if a.name.upper() == step.get('activity_type').upper():
  174. activity = a
  175. break
  176. # get locator type
  177. for l in locators:
  178. if l.name.upper() == step.get('locator_type').upper():
  179. locator = l
  180. break
  181. # create Test Step
  182. teststepex = TestStepExecution(
  183. name=step.get('name'),
  184. description=step.get('description'),
  185. creator=admin,
  186. activity_type=activity,
  187. locator_type=locator,
  188. )
  189. db.session.add(testcase)
  190. db.session.commit()
  191. # set Sample Drop items
  192. print('Creating Sample Drop Items...')
  193. testrun = Testrun(
  194. name=f'Sample Drop Testrun',
  195. description=f'Sample Drop Testrun from "DropsTestRunDefinition.xlsx"',
  196. creator=admin,
  197. )
  198. db.session.add(testrun)
  199. db.session.commit()
  200. classname = ClassName(
  201. name='GC.CLASSES_TESTCASESEQUENCE',
  202. description='Classname for Sample Drop Test Case Sequence')
  203. db.session.add(classname)
  204. datafile = DataFile(
  205. filename=f'DropsTestExample.xlsx',
  206. creator=admin,
  207. )
  208. db.session.add(datafile)
  209. db.session.commit()
  210. testseq = TestCaseSequence(
  211. name=f'Sample Drop Test Case Sequence',
  212. description=f'Sample Drop Test Case Sequence from "DropsTestRunDefinition.xlsx"',
  213. creator=admin,
  214. classname=classname,
  215. )
  216. testseq.testrun.append(testrun)
  217. testseq.datafiles.append(datafile)
  218. db.session.add(testseq)
  219. db.session.commit()
  220. classname = ClassName(
  221. name='GC.CLASSES_TESTCASE',
  222. description='Classname for Sample Drop Test Case')
  223. db.session.add(classname)
  224. db.session.commit()
  225. testcase = TestCase(
  226. name=f'Sample Drop Test Case',
  227. description=f'Sample Drop Test Case from "DropsTestRunDefinition.xlsx"',
  228. creator=admin,
  229. classname=classname,
  230. browser_type=BrowserType.query.get(1),
  231. testcase_type=TestCaseType.query.get(1),
  232. )
  233. testcase.testcase_sequence.append(testseq)
  234. db.session.add(testcase)
  235. db.session.commit()
  236. classname = ClassName(
  237. name='GC.CLASSES_TESTSTEPMASTER',
  238. description='Classname for Sample Drop Test Step')
  239. db.session.add(classname)
  240. db.session.commit()
  241. teststepseq = TestStepSequence(
  242. name=f'Sample Drop Test Step',
  243. description=f'Sample Drop Test Case from "DropsTestRunDefinition.xlsx"',
  244. creator=admin,
  245. classname=classname,
  246. )
  247. teststepseq.testcase.append(testcase)
  248. db.session.add(testcase)
  249. db.session.commit()
  250. # 1
  251. teststepex = TestStepExecution(
  252. name=f'Sample Drop Test Step Execution Number 1',
  253. description=f'Sample Drop Test Step Execution Number 1 from "DropsTestRunDefinition.xlsx"',
  254. creator=admin,
  255. teststep_sequence=teststepseq,
  256. activity_type=ActivityType.query.get(1),
  257. locator_type=LocatorType.query.get(1),
  258. value = 'https://drops.earthsquad.global',
  259. )
  260. db.session.add(teststepex)
  261. # 2
  262. teststepex = TestStepExecution(
  263. name=f'Sample Drop Test Step Execution Number 2',
  264. description=f'Sample Drop Test Step Execution Number 2 from "DropsTestRunDefinition.xlsx"',
  265. creator=admin,
  266. teststep_sequence=teststepseq,
  267. activity_type=ActivityType.query.get(2),
  268. locator_type=LocatorType.query.get(1),
  269. locator = "(//input[@step='any'])[1]",
  270. value = '$(Username)',
  271. timeout = 5,
  272. )
  273. db.session.add(teststepex)
  274. # 3
  275. teststepex = TestStepExecution(
  276. name=f'Sample Drop Test Step Execution Number 3',
  277. description=f'Sample Drop Test Step Execution Number 3 from "DropsTestRunDefinition.xlsx"',
  278. creator=admin,
  279. teststep_sequence=teststepseq,
  280. activity_type=ActivityType.query.get(2),
  281. locator_type=LocatorType.query.get(1),
  282. locator = "(//input[@step='any'])[2]",
  283. value = '$(Password)',
  284. timeout = 0.2,
  285. )
  286. db.session.add(teststepex)
  287. # 4
  288. teststepex = TestStepExecution(
  289. name=f'Sample Drop Test Step Execution Number 4',
  290. description=f'Sample Drop Test Step Execution Number 4 from "DropsTestRunDefinition.xlsx"',
  291. creator=admin,
  292. teststep_sequence=teststepseq,
  293. activity_type=ActivityType.query.get(3),
  294. locator_type=LocatorType.query.get(1),
  295. locator = "//button[contains(.,'Submit')]",
  296. )
  297. db.session.add(teststepex)
  298. db.session.commit()
  299. print('Done.')