/
ScreenshotBlock.groovy
99 lines (91 loc) · 4.03 KB
/
ScreenshotBlock.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
package org.asciidoctor.extension
import org.asciidoctor.extension.BlockProcessor
import org.asciidoctor.ast.AbstractBlock
import org.asciidoctor.extension.Reader
import java.awt.image.BufferedImage
import javax.imageio.ImageIO
import geb.Browser
import groovy.lang.Binding
import org.openqa.selenium.Dimension
class ScreenshotBlock extends BlockProcessor {
ScreenshotBlock(String name, Map<String, Object> config) {
super(name, [contexts: [':paragraph'], content_model: ':simple'])
}
String generateId(){
def alphabet=(('A'..'Z')+('0'..'9')).join()
def stringId
new Random().with {
stringId=(1..32).collect{alphabet[nextInt(36)]}.join()
}
stringId
}
def process(AbstractBlock parent, Reader reader, Map<String, Object> attributes) {
def maxHeight=600
def maxWidth=800
if(attributes.dimension){
def dimension=attributes.dimension
def driver=Browser.drive({}).driver
def window=driver.manage().window()
if(dimension=="FRAME_IPHONE4") dimension="336x504"
else if(dimension=="FRAME_IPHONE5") dimension="336x596"
else if(dimension=="FRAME_IPHONE6") dimension="336x596"
else if(dimension=="FRAME_IPHONE6PLUS") dimension="360x640"
else if(dimension=="FRAME_SAMSUNG_S4") dimension="370x657"
else if(dimension=="FRAME_IMAC") dimension="1200x675"
else if(dimension=="FRAME_BROWSER") dimension="800x500"
dimension=dimension.split("x")
def width=dimension[0] as int
def height=dimension[1] as int
maxHeight=height
maxWidth=width
def size=window.size
int viewportdeltax = driver.executeScript("return document.documentElement.clientWidth")-size.width
int viewportdeltay = 80//driver.executeScript("return document.documentElement.clientHeight")-size.height
window.size=new Dimension(width-viewportdeltax, height+viewportdeltay)
}
if(attributes.action=="browse"){
def binding = new Binding()
binding.setVariable("Browser", Browser)
binding.setVariable("Dimension", Dimension)
def shell = new GroovyShell(binding)
shell.evaluate("Browser.drive{"+reader.lines().join("\n")+"}")
createBlock(parent,"paragraph","",[:],[:])
}
else {
def cutElement
if(!attributes.name) attributes.name=generateId()
Browser.drive {
if(attributes.url){
go attributes.url
waitFor(1){true}
}
report attributes.name
if(attributes.selector){
cutElement=$(attributes.selector)
}
}
if(cutElement) cropScreenshot(new File("build/asciidoc/screenshots/"+attributes.name+".png"),cutElement, maxWidth, maxHeight)
else cropScreenshot(new File("build/asciidoc/screenshots/"+attributes.name+".png"), maxWidth, maxHeight)
def alt=attributes.dimension
createBlock(parent,"image","",[target:"screenshots/"+attributes.name+".png",title:reader.lines().join(" // "),alt:alt],[:])
}
}
def cropScreenshot(screen, element, int maxWidth=800, int maxHeight=1200){
int width = element.width
int height = element.height
if(height>maxHeight) height=maxHeight
if(width>maxWidth) width=maxWidth
BufferedImage img = ImageIO.read(screen)
BufferedImage dest = img.getSubimage(element.x, element.y, width, height)
ImageIO.write(dest, "png", screen)
}
def cropScreenshot(screen, int maxWidth=800, int maxHeight=600){
BufferedImage img = ImageIO.read(screen)
int width = img.width
int height = img.height
if(height>maxHeight) height=maxHeight
if(width>maxWidth) width=maxWidth
BufferedImage dest = img.getSubimage(0, 0, width, height)
ImageIO.write(dest, "png", screen)
}
}