-
Notifications
You must be signed in to change notification settings - Fork 1
/
excel2spec.groovy
107 lines (98 loc) · 4.31 KB
/
excel2spec.groovy
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
//script to convert an xml-workbook-Spec into a groovy-class-Spec
//helper
def rowToList = { row ->
def list = []
def correction = 0
row.Cell.eachWithIndex { cell, index ->
def numCellsToJump = 0
if (cell."@ss:Index".text()) {
numCellsToJump = (cell."@ss:Index".text().toInteger()) - (index+1+correction)
numCellsToJump.times { list << '' }
correction += numCellsToJump
}
list << cell.text()
}
return list
}
def blockTranslation = [
'gegeben' : 'given: ',
'wenn' : 'when: ',
'und' : 'and: ',
'dann' : 'then: ',
'erwartet' : 'expect: ',
'wobei' : 'where: ',
]
//the main loop
new File('.').listFiles({file,name-> name ==~ /.*Spec.xml$/} as FilenameFilter).each { file ->
println "found $file"
def workbook = new XmlSlurper().parseText(file.text)
//first extract page definitions
def pageDefs = [:]
new File('./pages').mkdirs()
workbook.Worksheet.each { worksheet ->
def name = worksheet."@ss:Name".text()
if (name.endsWith('Seiten')) {
worksheet.Table.Row[1..-1].each { row ->
def (readable,url,technical) = rowToList(row)
pageDefs[readable]=[url:url,name:technical]
def code = """\
package pages
import geb.Page
import spock.lang.Ignore
class $technical extends Page {
static url = "$url"
//TODO:
static at = { title ==~ /some regexp/ }
static content = {
}
}
""".stripIndent()
new File("./pages/${technical}.groovy").write(code)
}
}
}
def withinFeature = 0
workbook.Worksheet.each { worksheet ->
def name = worksheet."@ss:Name".text()
if (name.endsWith('Spec')) {
def pages = []
def code = """\
import geb.spock.GebReportingSpec
%pageImports%
class ${file.name-'.xml'} extends GebReportingSpec {
""".stripIndent()
worksheet.Table.Row[1..-1].each { row ->
def (featureName,block,description,response,screenshot,comment) = rowToList(row)
if (featureName) {
code += "${withinFeature++?" }\n\n":''} @Ignore(\"not implemented\")\ndef '${featureName.replaceAll("'",'\'')}'() {\n"
} else {
block = block?.toLowerCase()?.replaceAll("[^a-z]","")
block = blockTranslation[block]?:block
def matcher = description =~ /^"(.*)"$/
if (matcher) {
description = matcher[0][1].replaceAll('""','"')
}
code += " ${block}\"${description?.replaceAll('"',"'")}\"${comment?.trim()?"\t//$comment":""}\n"
if (screenshot) {
code += " report '$screenshot'\n"
}
if (response) {
if (block.startsWith('given')) {
code += " to $response\n"
} else {
code += " at $response\n"
}
pages << response
}
code += " //TODO: implement test step\n"
}
}
code += " }\n}\n"
code = code.replaceAll('(?ms)%pageImports%',pages.unique().collect{"import pages.${pageDefs[it].name}"}.join("\n"))
println pages.unique().collect{"import pages.${pageDefs[it].name}"}.join("\n")
def targetName = file.name-'.xml'+'.groovy'
new File (targetName).write(code)
println "wrote $targetName"
}
}
}