-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestPackageTestRun.kt
120 lines (103 loc) · 4.75 KB
/
TestPackageTestRun.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package de.scandio.e4.testpackages
import ch.qos.logback.classic.Level
import ch.qos.logback.classic.LoggerContext
import de.scandio.e4.E4Env
import de.scandio.e4.worker.client.NoopWebClient
import de.scandio.e4.worker.collections.ActionCollection
import de.scandio.e4.worker.interfaces.Action
import de.scandio.e4.worker.interfaces.TestPackage
import de.scandio.e4.worker.interfaces.WebClient
import de.scandio.e4.worker.util.Util
import org.slf4j.LoggerFactory
import org.slf4j.LoggerFactory.getILoggerFactory
abstract class TestPackageTestRun {
val loggerContext = getILoggerFactory() as LoggerContext
private val log = LoggerFactory.getLogger(javaClass)
protected var screenshotCount = 0
protected val util: Util
init {
this.util = Util()
setLogLevel("org.apache", Level.ERROR)
}
protected fun setLogLevel(packagePath: String, level: Level) {
loggerContext.getLogger(packagePath).level = level
}
protected fun executeTestPackage(testPackage: TestPackage, howManyTimes: Int = 1) {
log.info("==============================================================")
log.info("START executing ${testPackage.virtualUsers.size} virtual users")
repeat(howManyTimes) {
log.info("==> NEXT RUN: $it")
for (virtualUserClass in testPackage.virtualUsers) {
val virtualUser = virtualUserClass.newInstance()
log.info("Executing virtual user ${virtualUser.javaClass.simpleName}")
val measurement = executeActions(virtualUser.actions)
log.info("[MEASURE] Total time taken for VirtualUser ${virtualUser.javaClass.simpleName}: ${measurement.totalTimeTaken} (Total actions run: ${measurement.numActionsRun} - Actions excluded from measurement: ${measurement.numExcludedActions})")
}
}
log.info("DONE executing ${testPackage.virtualUsers.size} virtual users")
log.info("==============================================================")
}
protected fun executeTestPackagePrepare(testPackage: TestPackage) {
log.info("==============================================================")
log.info("START executing ${testPackage.setupActions.size} setup actions")
executeActions(testPackage.setupActions)
log.info("DONE executing setup actions")
log.info("==============================================================")
}
protected fun executeAction(action: Action) {
var webClient: WebClient = NoopWebClient()
if (!action.isRestOnly()) {
webClient = E4Env.newAdminTestWebClient()
}
val restConfluence = E4Env.newAdminTestRestClient()
log.info("Executing action ${action.javaClass.simpleName}")
val runtimeName = "afteraction-${action.javaClass.simpleName}"
try {
action.execute(webClient, restConfluence)
log.info("Time taken: ${action.timeTaken}")
} finally {
if (!action.isRestOnly) {
webClient.takeScreenshot(runtimeName)
webClient.dumpHtml(runtimeName)
webClient.quit()
}
}
}
protected fun executeActions(actions: ActionCollection): Measurement {
var totalTimeTaken: Long = 0
var numExcludedActions = 0
var numActionsRun = 0
for (action in actions) {
var webClient: WebClient
if (action.isRestOnly) {
webClient = NoopWebClient()
} else {
webClient = E4Env.newAdminTestWebClient()
}
val restClient = E4Env.newAdminTestRestClient()
try {
log.info("Executing action ${action.javaClass.simpleName}")
action.execute(webClient, restClient)
if (!actions.isExcludedFromMeasurement(action)) {
totalTimeTaken += action.timeTaken
numActionsRun += 1
log.info("Time taken for action ${action.javaClass.simpleName}: ${action.timeTaken}")
} else {
numExcludedActions += 1
}
} finally {
val runtimeName = "afteraction-${action.javaClass.simpleName}"
webClient.takeScreenshot(runtimeName)
webClient.dumpHtml(runtimeName)
webClient.quit()
}
}
return Measurement(totalTimeTaken, numExcludedActions, numActionsRun)
}
open fun shot(webClient: WebClient) {
this.screenshotCount += 1
val path = this.util.takeScreenshot(webClient.webDriver, "${E4Env.OUT_DIR}/$screenshotCount-test-run.png")
print(path)
}
class Measurement(val totalTimeTaken: Long, val numExcludedActions: Int, val numActionsRun: Int)
}