Advertisement
Guest User

Untitled

a guest
Jul 5th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.64 KB | None | 0 0
  1. 'VBScript Document
  2. Option Explicit
  3.  
  4.  
  5. 'QC Paramters
  6. Dim Server, UserName, Password, QCDomain, QCProject, QCTestSetPath, QCTestSetName,Env, ExecutionBrowser
  7. Dim objWMIService1, objWMIService2, objProcess, colProcess,objNet,objFSO
  8. Dim strComputer, strProcess1, strProcess2
  9. Dim cnt, nDays
  10. Dim WshShell
  11. Dim strLogFile
  12.  
  13. Const DeleteReadOnly = True
  14. cnt = 0
  15. nDays = 6
  16.  
  17. '
  18. strComputer = "."
  19. strProcess1 = "'UFT.exe'"
  20. strProcess2 = "'abc.exe'"
  21. strLogFile = "C:ProjectabcExecutionLog.log"
  22.  
  23. const MAX_POLL_CTR = 45
  24. Const SLEEP_TIME = 60000 '1 min, so a test can run for a max of 10 mins
  25. const FORAPPENDING = 8
  26.  
  27. Server = <url>
  28. UserName = <username>
  29. Password = <password>
  30. QCDomain = <domain>
  31. QCProject = <project>
  32. QCTestSetPath = <path>
  33. QCTestSetName = <TestSetName>
  34. Env = <Environment>
  35. ExecutionBrowser = <Browser>
  36.  
  37.  
  38.  
  39. Call RunTestSet(Server, UserName, Password, QCDomain, QCProject, QCTestSetPath, QCTestSetName)
  40.  
  41. Public Sub RunTestSet(Server, UserName, Password, QCDomain, QCProject, QCTestSetPath, QCTestSetName)
  42. Dim QCTestSetExec, sErr, arrArgs, ix, arg, bExit
  43.  
  44. sErr = "Unable to execute RunTestSet. Please provide the "
  45. arrArgs = Array("Server", "UserName", "Password", "QCDomain", "QCProject", "QCTestSetPath", "QCTestSetName")
  46. bExit = False
  47.  
  48. For ix = LBound(arrArgs) To UBound(arrArgs)
  49. Execute "arg = " & arrArgs(ix)
  50.  
  51. If arg = "" Then
  52. MsgBox sErr & arrArgs(ix) & ".", vbOkOnly, "Error!"
  53.  
  54. bExit = True
  55. End If
  56. Next
  57.  
  58. If bExit Then Exit Sub
  59.  
  60. Set QCTestSetExec = New QCRunTestSet
  61.  
  62. With QCTestSetExec
  63. .Server = Server
  64. .UserName = UserName
  65. .Password = Password
  66. .QCDomain = QCDomain
  67. .QCProject = QCProject
  68. .QCTestSetPath = QCTestSetPath
  69. .QCTestSetName = QCTestSetName
  70.  
  71. .Run
  72. End With
  73.  
  74. Set QCTestSetExec = Nothing
  75. End Sub
  76.  
  77. Class QCRunTestSet
  78.  
  79.  
  80.  
  81. 'Public Variables
  82.  
  83. ''' <summary>
  84. ''' QC Server URL (string)
  85. ''' </summary>
  86. ''' <remarks></remarks>
  87. Public Server
  88.  
  89. ''' <summary>
  90. ''' UserName (string)
  91. ''' </summary>
  92. ''' <remarks></remarks>
  93. Public UserName
  94.  
  95. ''' <summary>
  96. ''' Password (string)
  97. ''' </summary>
  98. ''' <remarks></remarks>
  99. Public Password
  100.  
  101. ''' <summary>
  102. ''' Quality Center Domain (string)
  103. ''' </summary>
  104. ''' <remarks></remarks>
  105. Public QCDomain
  106.  
  107. ''' <summary>
  108. ''' QC Project (string)
  109. ''' </summary>
  110. ''' <remarks></remarks>
  111. Public QCProject
  112.  
  113. ''' <summary>
  114. ''' QC TestSet Folder Path (string)
  115. ''' </summary>
  116. ''' <remarks>RootTestSetFolderTestSetSubFolder</remarks>
  117. Public QCTestSetPath
  118.  
  119. ''' <summary>
  120. ''' Target TestSet Name (string)
  121. ''' </summary>
  122. ''' <remarks></remarks>
  123. Public QCTestSetName
  124.  
  125. ''' <summary>
  126. ''' Recipient list from QC's Automation tab
  127. ''' </summary>
  128. ''' <remarks></remarks>
  129. Public EMailTo
  130.  
  131. ''' <summary>
  132. ''' TSTestFactory manages test instances (TSTest objects) in a test set
  133. ''' </summary>
  134. ''' <remarks></remarks>
  135. Public TSTestFactory
  136.  
  137. ''' <summary>
  138. ''' Number of blocked tests after completion of scheduler (integer)
  139. ''' </summary>
  140. ''' <remarks></remarks>
  141. Public iBlocked
  142.  
  143. ''' <summary>
  144. ''' Number of failed tests after completion of scheduler (integer)
  145. ''' </summary>
  146. ''' <remarks></remarks>
  147. Public iFailed
  148.  
  149. ''' <summary>
  150. ''' Number of N/A tests after completion of scheduler (integer)
  151. ''' </summary>
  152. ''' <remarks></remarks>
  153. Public iNA
  154.  
  155. ''' <summary>
  156. ''' Number of NoRun tests after completion of scheduler (integer)
  157. ''' </summary>
  158. ''' <remarks></remarks>
  159. Public iNoRun
  160.  
  161. ''' <summary>
  162. ''' Number of NotCompleted tests after completion of scheduler (integer)
  163. ''' </summary>
  164. ''' <remarks></remarks>
  165. Public iNotCompleted
  166.  
  167. ''' <summary>
  168. ''' Number of Passed tests after completion of scheduler (integer)
  169. ''' </summary>
  170. ''' <remarks></remarks>
  171. Public iPassed
  172.  
  173. ''' <summary>
  174. ''' DateTime stamp at the start of the Scheduling session (DateTime)
  175. ''' </summary>
  176. ''' <remarks></remarks>
  177. Public dtStartTime
  178.  
  179. 'Private Variables
  180.  
  181. ''' <summary>
  182. ''' QuickTest.Application object
  183. ''' </summary>
  184. ''' <remarks></remarks>
  185. Private qtApp
  186.  
  187. ''' <summary>
  188. ''' TDApiOle object
  189. ''' </summary>
  190. ''' <remarks></remarks>
  191. Private TDConnection
  192.  
  193. ''' <summary>
  194. ''' TSScheduler object returned by the StartExecution method
  195. ''' </summary>
  196. ''' <remarks></remarks>
  197. Private TSScheduler
  198.  
  199. ''' <summary>
  200. ''' TestSet Folder object
  201. ''' </summary>
  202. ''' <remarks></remarks>
  203. Private TSFolder
  204.  
  205. ''' <summary>
  206. ''' Executes the scheduler
  207. ''' </summary>
  208. ''' <remarks></remarks>
  209.  
  210.  
  211.  
  212.  
  213. Public Default Sub Run()
  214. Dim bStatus, dtStartTime
  215.  
  216. '@see isQCConnected()
  217. bStatus = isQCConnected
  218.  
  219. '@see isQTPInstalled()
  220. If bStatus Then bStatus = isQTPInstalled
  221.  
  222. If Not bStatus Then Exit Sub
  223.  
  224. Dim TSTreeManager, QCTestSetPath, TSList, QCTestSetName, TestSet, qtTest, sEnvironment, TSReport, EMailTo, ExecutionStatus
  225.  
  226.  
  227. Dim TestList, TestID, TestRunStatus, ctr, TName, TestName
  228.  
  229. 'TestSetTreeManager manages the test set tree and its related test set folders
  230. Set TSTreeManager = TDConnection.TestSetTreeManager
  231.  
  232. QCTestSetPath = Me.QCTestSetPath
  233. 'Return the test set tree node from the specified tree path
  234. Set TSFolder = TSTreeManager.NodeByPath(QCTestSetPath)
  235.  
  236. QCTestSetName = Me.QCTestSetName
  237. 'Returns the list of test sets contained in the folder that match the specified pattern.
  238. Set TSList = TSFolder.FindTestSets(QCTestSetName)
  239.  
  240. If TSList.Count = 0 Then
  241. MsgBox "The TestSet '" & QCTestSetName & "' was not found.", vbOkOnly, "TSFolder.FindTestSets Exception!"
  242. Exit Sub
  243. End If
  244.  
  245. For Each TestSet in TSList
  246. If LCase(TestSet.Name) = LCase(QCTestSetName) Then
  247. Exit For
  248. End If
  249. Next
  250.  
  251. 'This enables database to update immediately when the field value changes
  252. TestSet.AutoPost = True
  253.  
  254. 'TSTestFactory manages test instances (TSTest objects) in a test set
  255. Set TSTestFactory = TestSet.TSTestFactory
  256. Set Me.TSTestFactory = TSTestFactory
  257. Set TestList = TSTestFactory.NewList("")
  258.  
  259. 'TSTestFactory.NewList("") creates a list of objects according to the specified filter
  260. For Each qtTest in TestList
  261.  
  262. qtTest.Field("TC_STATUS") = "No Run"
  263. qtTest.Post
  264. Next
  265.  
  266.  
  267. TestSet.Refresh : TSFolder.Refresh
  268.  
  269.  
  270. Set TSReport = TestSet.ExecutionReportSettings
  271. TSReport.Enabled = True
  272.  
  273.  
  274. EMailTo = TSReport.EMailTo : Me.EMailTo = EMailTo
  275.  
  276. On Error Resume Next
  277.  
  278.  
  279. dtStartTime = Now : Me.dtStartTime = dtStartTime
  280.  
  281. TestID = ""
  282. TName = ""
  283. ctr = 0
  284. For each qtTest in TestList
  285. TestID = CStr(qtTest.ID)
  286.  
  287. TName = qtTest.Name
  288. TestName = qtTest.TestName
  289. 'Print "Executing Test ID using Field value" & qtTest.Field("TC_TESTCYCL_ID")
  290.  
  291. 'TestSet.StartExecution returns the TSScheduler object and starts the Execution controller
  292. Set TSScheduler = TestSet.StartExecution("")
  293.  
  294. If Err.Number <> 0 Then
  295. MsgBox Err.Description & vbNewLine & vbNewLine & "Unable to create the TSScheduler" & _
  296. "object. Please ensure the ALM Client Registration is complete before " & _
  297. "executing RunTestSet.", vbOkOnly, "RunTestSet.Run->TSScheduler Exception!"
  298.  
  299. On Error Goto 0
  300. Exit Sub
  301. End If
  302. On Error Goto 0
  303.  
  304. 'Run all tests on localhost
  305. TSScheduler.RunAllLocally = True
  306.  
  307.  
  308. 'Logging enabled
  309. TSScheduler.LogEnabled = True
  310.  
  311. 'Start testSet run
  312. TSScheduler.Run(TestID)
  313.  
  314. 'ExecutionStatus represents the execution status of the scheduler
  315. Set ExecutionStatus = TSScheduler.ExecutionStatus
  316.  
  317. 'Wait until all tests are complete running
  318. TestRunStatus = WaitWhileTestRunning(ExecutionStatus,TestID,MAX_POLL_CTR)
  319.  
  320. If not TestRunStatus Then
  321. '
  322.  
  323.  
  324.  
  325. 'Test execution failed or timed out
  326. 'Terminate UFT process
  327. On Error Resume Next
  328. TSScheduler.Stop(TestID)
  329. Set ExecutionStatus = TSScheduler.ExecutionStatus
  330. 'Wait until all tests are complete running
  331. TestRunStatus = WaitWhileTestRunning(ExecutionStatus,TestID,5)
  332.  
  333. If not TestRunStatus Then
  334. If qtTest.IsLocked Then
  335. qtTest.UnLockObject()
  336. WScript.Sleep 100
  337. qtTest.Refresh()
  338. WScript.Sleep 3000 'Atleast 3s are elapse before Refresh completes
  339. End If
  340.  
  341.  
  342. Set objWMIService1 = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\" & strComputer & "rootcimv2")
  343.  
  344. Set colProcess = objWMIService1.ExecQuery _
  345. ("Select * from Win32_Process Where Name = " & strProcess1)
  346.  
  347. For Each objProcess in colProcess
  348. objProcess.Terminate()
  349. WScript.Sleep 100
  350. 'cnt = cnt + 1
  351. Next
  352.  
  353. WScript.Sleep 1000
  354.  
  355. Set objWMIService2 = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\" & strComputer & "rootcimv2")
  356.  
  357. Set colProcess = objWMIService2.ExecQuery _
  358. ("Select * from Win32_Process Where Name = " & strProcess2)
  359.  
  360. For Each objProcess in colProcess
  361. objProcess.Terminate()
  362. WScript.Sleep 100
  363. 'cnt = cnt + 1
  364. Next
  365. WScript.Sleep 10000
  366. End If
  367.  
  368.  
  369. 'To clear temporary Internet files
  370. Set WshShell = CreateObject("WScript.Shell")
  371. WshShell.run "RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 8"
  372.  
  373. 'To clear browsing cookies
  374. WshShell.run "RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 2"
  375.  
  376. 'To Clear Browsing History
  377. WshShell.run "RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 1"
  378. writeLog(TName&" - "&TestName)
  379. 'Exit For 'Test Execution unsuccessful
  380. End If
  381.  
  382. Set ExecutionStatus = Nothing
  383. Set TSScheduler = Nothing
  384.  
  385. Next 'End TestList iteration
  386.  
  387.  
  388. End Sub
  389.  
  390. 'Private Methods
  391.  
  392. Private Function WaitWhileTestRunning(ByVal ExecutionStatus, ByVal TestID, ByVal pollCounter)
  393. Dim RunFinished: RunFinished = False
  394. Dim bTimeout
  395.  
  396. bTimeout = false
  397.  
  398. Do while (RunFinished = False)
  399. ExecutionStatus.RefreshExecStatusInfo TestID, True
  400. RunFinished = ExecutionStatus.Finished
  401.  
  402. WScript.Sleep(SLEEP_TIME)
  403. pollCounter = pollCounter - 1
  404.  
  405. If pollCounter = 0 Then
  406. bTimeout = true
  407. Exit Do
  408. End If
  409. Loop
  410.  
  411. If bTimeout Then
  412. WaitWhileTestRunning = false
  413. Else
  414. WaitWhileTestRunning = RunFinished
  415. End If
  416.  
  417. End Function
  418.  
  419. Private Function writeLog(ByVal strTestName)
  420. Dim obFSO
  421. Dim obFile
  422. Dim strMessage
  423. Set obFSO = CreateObject("Scripting.FileSystemObject")
  424. ' Validate the file exits before attempting to write, create if it does not
  425. If obFSO.FileExists(strLogFile) Then
  426. Set obFile = obFSO.OpenTextFile(strLogFile,FORAPPENDING)
  427. Else
  428. Set obFile = obFSO.CreateTextFile(strLogFile,True)
  429. End If
  430.  
  431. strMessage = "Test Name: " & strTestName & " Executed on " & Now
  432.  
  433. ' write the message
  434.  
  435. obFile.WriteLine strMessage
  436.  
  437. ' Close the log
  438. obFile.Close
  439. End Function
  440.  
  441. Private Function isQCConnected()
  442. isQCConnected = False
  443.  
  444. Dim UserName, Password
  445.  
  446. UserName = Me.UserName
  447. Password = Me.Password
  448.  
  449. On Error Resume Next
  450. Set TDConnection = CreateObject("TDApiOle80.TDConnection")
  451.  
  452. If Err.Number <> 0 Then
  453. MsgBox "Unable to create an instance of the TestDirector API " & _
  454. "OLE (TestDirector Connection) Object.", vbOkOnly, "TDConnection Exception!"
  455. Err.Clear : Exit Function
  456. End If
  457.  
  458. With TDConnection
  459. 'Create a connection with the QC Server
  460. .InitConnectionEx Server
  461.  
  462. If Err.Number <> 0 Then
  463. MsgBox Err.Description, vbOkOnly, "TDConnection.InitConnectionEx Exception!"
  464. Exit Function
  465. End If
  466.  
  467. 'Login to QC
  468. .Login UserName, Password
  469.  
  470. If Err.Number <> 0 Then
  471. MsgBox Err.Description, vbOkOnly, "TDConnection.Login Exception!"
  472. Exit Function
  473. ElseIf Not .LoggedIn Then
  474. MsgBox "Unable to login to Quality Center. Please verify your login " & _
  475. "credentials.", vbOkOnly, "TDConnection.Login Exception!"
  476. Exit Function
  477. End If
  478.  
  479. 'Connect to QC Project
  480. .Connect QCDomain, QCProject
  481.  
  482. If Err.Number <> 0 Then
  483. MsgBox Err.Description, vbOkOnly, "TDConnection.Connect Exception!"
  484. Exit Function
  485. ElseIf Not .ProjectConnected Then
  486. MsgBox "Unable to connect to '" & QCDomain & "/" & QCProject & "'.", vbOkOnly, _
  487. "TDConnection.Connect Exception!"
  488. Exit Function
  489. End If
  490.  
  491. isQCConnected = True
  492. End With
  493.  
  494. On Error Goto 0
  495. End Function
  496.  
  497. Private Function isQTPInstalled()
  498. isQTPInstalled = False
  499.  
  500. On Error Resume Next
  501. Set qtApp = GetObject("", "QuickTest.Application")
  502.  
  503. If Err.Number <> 0 Then
  504. MsgBox Err.Description, vbOkOnly, "QuickTest.Application Exception!"
  505. Exit Function
  506. Else
  507. qtApp.Launch()
  508. qtApp.Visible = True
  509.  
  510. isQTPInstalled = True
  511. End If
  512. On Error Goto 0
  513. End Function
  514.  
  515. Private Function get_TSExecutionLog()
  516. Dim color, style, TSTestFactory, TSList, ix, html, sTest, sStatus, sTester, sActualTester, dtDate, dtExecTime
  517.  
  518. 'color = green;red
  519. color = "46D44B;D41743"
  520.  
  521. 'default html style
  522. style = "font-size: 11px; padding-right: 5px; padding-left: 5px; height: 20px; border-bottom: 1px solid #eee;"
  523.  
  524. Set TSTestFactory = Me.TSTestFactory
  525. Set TSList = TSTestFactory.NewList("")
  526.  
  527. 'Loop through all tests in the TestSet list and retrieve their status
  528. For ix = 1 To TSList.Count
  529. html = html & "<tr>"
  530.  
  531. 'Test Name
  532. html = html & "<td style='" & style & "'>" & TSList.item(ix).field("TSC_NAME") & "</td>"
  533.  
  534. 'Status
  535. sStatus = TSList.item(ix).LastRun.Status
  536. Select Case sStatus
  537. Case "Passed" : html = html & "<td style='color: #" & Trim(Split(color, ";")(0)) & ";" & style & "'>" & sStatus & "</td>"
  538. Case "Failed" : html = html & "<td style='color: #" & Trim(Split(color, ";")(1)) & ";" & style & "'>" & sStatus & "</td>"
  539. Case Else : html = html & "<td style='" & style & "'>" & sStatus & "</td>"
  540. End Select
  541.  
  542. 'Tester
  543. html = html & "<td style='" & style & "'>" & TSList.item(ix).field("TC_TESTER_NAME") & "</td>"
  544.  
  545. 'Actual Tester
  546. html = html & "<td style='" & style & "'>" & TSList.item(ix).field("TC_ACTUAL_TESTER") & "</td>"
  547.  
  548. 'DateTime stamp
  549. html = html & "<td style='" & style & "'>" & Date & "</td>"
  550.  
  551. 'Execution Time
  552. html = html & "<td style='" & style & "'>" & TSList.item(ix).field("TC_EXEC_TIME") & "</td>"
  553. html = html & "</tr>"
  554. Next
  555.  
  556. get_TSExecutionLog = html
  557. End Function
  558.  
  559. ''' <summary>
  560. ''' Returns the number of tests passed, failed and not completed
  561. ''' </summary>
  562. ''' <remarks></remarks>
  563.  
  564. Private Sub load_ExecutionRunStatus()
  565. Dim TSTestFactory, TSList, ix, iBlocked, iFailed, iNA, iNoRun, iNotCompleted, iPassed
  566.  
  567. Set TSTestFactory = Me.TSTestFactory
  568. On Error Resume Next
  569. Set TSList = TSTestFactory.NewList("")
  570.  
  571. 'Loop through all tests in the testSet list and retrieve status
  572. For ix = 1 To TSList.Count
  573. Select Case LCase(TSList.item(ix).LastRun.Status)
  574. Case "blocked" : iBlocked = iBlocked + 1
  575. Case "failed" : iFailed = iFailed + 1
  576. Case "n/a" : iNA = iNA + 1
  577. Case "no run" : iNoRun = iNoRun + 1
  578. Case "not completed" : iNotCompleted = iNotCompleted + 1
  579. Case "passed" : iPassed = iPassed + 1
  580. End Select
  581. Next
  582.  
  583. If iBlocked = "" Then iBlocked = 0
  584. If iFailed = "" Then iFailed = 0
  585. If iNA = "" Then iNA = 0
  586. If iNoRun = "" Then iNoRun = 0
  587. If iNotCompleted = "" Then iNotCompleted = 0
  588. If iPassed = "" Then iPassed = 0
  589.  
  590. Me.iBlocked = iBlocked
  591. Me.iFailed = iFailed
  592. Me.iNA = iNA
  593. Me.iNoRun = iNoRun
  594. Me.iNotCompleted = iNotCompleted
  595. Me.iPassed = iPassed
  596. End Sub
  597.  
  598. ''' <summary>
  599. ''' Sends an email to the distribution list
  600. ''' </summary>
  601. ''' <remarks></remarks>
  602.  
  603. Private Sub TDSendMail()
  604.  
  605. Dim EMailTo : EMailTo = Me.EMailTo
  606. Dim QCTestSetName : QCTestSetName = Me.QCTestSetName
  607.  
  608. If EMailTo = "" Then Exit Sub
  609.  
  610. load_ExecutionRunStatus()
  611.  
  612. TDConnection.SendMail EMailTo, "", "Automation Regression Execution: " & QCTestSetName&"; Environment : " &Env&"; Browser : "&ExecutionBrowser, sHTML
  613. End Sub
  614.  
  615. 'Class Initialize & Terminate
  616.  
  617. ''' <summary>
  618. ''' Releases connections and sends mail after TSScheduler execution
  619. ''' </summary>
  620. ''' <remarks></remarks>
  621.  
  622. Private Sub Class_Terminate()
  623. Dim bStatus
  624. '@see isQCConnected()
  625. bStatus = isQCConnected
  626.  
  627. If bStatus Then
  628. If IsObject(TSFolder) Then
  629. If Not TSFolder Is Nothing Then
  630. TSFolder.Refresh : WScript.Sleep(5000)
  631.  
  632. 'Send an email to the distribution list
  633. TDSendMail()
  634.  
  635. Set TSFolder = Nothing
  636. End If
  637. End If
  638. End If
  639.  
  640. On Error Resume Next
  641. 'Disconnect TD session
  642. TDConnection.Disconnect
  643.  
  644. 'Disconect and quit QTP
  645. If IsObject(qtApp) Then
  646. If qtApp.TDConnection.IsConnected Then qtApp.TDConnection.Disconnect
  647. qtApp.Quit
  648. End If
  649. On Error Goto 0
  650.  
  651. Set qtApp = Nothing
  652. Set TDConnection = Nothing
  653. End Sub
  654.  
  655. End Class ''RunTestSet
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement