Skip to content

Commit af35367

Browse files
committed
feat: support to take screenshots
Signed-off-by: Adrian Riobo Lorenzo <[email protected]>
1 parent 4fc2a33 commit af35367

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

99 files changed

+36575
-9
lines changed

cmd/app/app.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,15 @@ import (
55
)
66

77
const (
8-
appPath = "app-path"
8+
appPath = "app-path"
9+
record = "record"
10+
recordDesc = "capture screenshot"
11+
recordsPath = "records-path"
12+
recordsPathDesc = "path to store the screeshots"
13+
)
14+
15+
var (
16+
recordsPathDefault = "."
917
)
1018

1119
func GetCmd() *cobra.Command {

cmd/app/click.go

+25-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
package app
22

33
import (
4+
"fmt"
5+
46
"github.com/spf13/cobra"
57
"github.com/spf13/pflag"
68
"github.com/spf13/viper"
79

810
"github.com/adrianriobo/goax/pkg/goax/app"
11+
"github.com/adrianriobo/goax/pkg/util/delay"
12+
"github.com/adrianriobo/goax/pkg/util/logging"
13+
"github.com/adrianriobo/goax/pkg/util/screenshot"
914
)
1015

1116
func getClickCmd() *cobra.Command {
@@ -27,6 +32,8 @@ func getClickCmd() *cobra.Command {
2732
flagSet.StringP("element-type", "t", "", "element type to be clicked")
2833
flagSet.Bool("strict", false, "to force id match exactly with the element id")
2934
flagSet.Int("order", 0, "in case multiple elements with same id, we can specify the order of the element within the list of elements")
35+
flagSet.Bool(record, false, recordDesc)
36+
flagSet.StringP(recordsPath, "", recordsPathDefault, recordsPathDesc)
3037
c.Flags().AddFlagSet(flagSet)
3138
c.MarkFlagRequired("element")
3239
return c
@@ -37,9 +44,25 @@ func click() error {
3744
if err != nil {
3845
return err
3946
}
40-
return a.ClickWithOrder(
47+
delay.Delay(delay.LONG)
48+
if viper.IsSet(record) {
49+
if err := screenshot.CaptureScreen(viper.GetString(recordsPath), "clickLoadForefrontApp"); err != nil {
50+
logging.Errorf("error capturing the screenshot: %v", err)
51+
}
52+
}
53+
if err := a.ClickWithOrder(
4154
viper.GetString("element"),
4255
viper.GetString("element-type"),
4356
viper.IsSet("strict"),
44-
viper.GetInt("order"))
57+
viper.GetInt("order")); err != nil {
58+
return err
59+
}
60+
delay.Delay(delay.LONG)
61+
if viper.IsSet(record) {
62+
sfn := fmt.Sprintf("click-%s", viper.GetString("element"))
63+
if err := screenshot.CaptureScreen(viper.GetString(recordsPath), sfn); err != nil {
64+
logging.Errorf("error capturing the screenshot: %v", err)
65+
}
66+
}
67+
return nil
4568
}

cmd/app/open.go

+17-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ import (
66
"github.com/spf13/viper"
77

88
"github.com/adrianriobo/goax/pkg/goax/app"
9+
"github.com/adrianriobo/goax/pkg/util/delay"
10+
"github.com/adrianriobo/goax/pkg/util/logging"
11+
"github.com/adrianriobo/goax/pkg/util/screenshot"
912
)
1013

1114
func getOpenCmd() *cobra.Command {
@@ -23,11 +26,23 @@ func getOpenCmd() *cobra.Command {
2326

2427
flagSet := pflag.NewFlagSet("app", pflag.ExitOnError)
2528
flagSet.StringP(appPath, "p", "", "path for the application to be handle")
29+
flagSet.Bool(record, false, recordDesc)
30+
flagSet.StringP(recordsPath, "", recordsPathDefault, recordsPathDesc)
2631
c.Flags().AddFlagSet(flagSet)
27-
2832
return c
2933
}
3034

3135
func open() error {
32-
return app.Open(viper.GetString(appPath))
36+
if err := app.Open(
37+
viper.GetString(appPath)); err != nil {
38+
return err
39+
}
40+
// We open remotely so we wait for a bit
41+
delay.Delay(delay.LONG)
42+
if viper.IsSet(record) {
43+
if err := screenshot.CaptureScreen(viper.GetString(recordsPath), "openApp"); err != nil {
44+
logging.Errorf("error capturing the screenshot: %v", err)
45+
}
46+
}
47+
return nil
3348
}

cmd/app/setvalue.go

+25-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
package app
22

33
import (
4+
"fmt"
5+
46
"github.com/spf13/cobra"
57
"github.com/spf13/pflag"
68
"github.com/spf13/viper"
79

810
"github.com/adrianriobo/goax/pkg/goax/app"
11+
"github.com/adrianriobo/goax/pkg/util/delay"
12+
"github.com/adrianriobo/goax/pkg/util/logging"
13+
"github.com/adrianriobo/goax/pkg/util/screenshot"
914
)
1015

1116
func getSetValueCmd() *cobra.Command {
@@ -29,6 +34,8 @@ func getSetValueCmd() *cobra.Command {
2934
flagSet.Int("order", 0, "in case multiple elements with same id, we can specify the order of the element within the list of elements")
3035
flagSet.Bool("focus", false, "if focus flag is added it will add the value to the current focused textbox on the screen (if any)")
3136
flagSet.StringP("value", "v", "", "value to be set on the element")
37+
flagSet.Bool(record, false, recordDesc)
38+
flagSet.StringP(recordsPath, "", recordsPathDefault, recordsPathDesc)
3239
c.Flags().AddFlagSet(flagSet)
3340
c.MarkFlagRequired("value")
3441
c.MarkFlagsMutuallyExclusive("focus", "element")
@@ -40,13 +47,29 @@ func setValue() error {
4047
if err != nil {
4148
return err
4249
}
50+
delay.Delay(delay.LONG)
51+
if viper.IsSet(record) {
52+
if err := screenshot.CaptureScreen(viper.GetString(recordsPath), "setValueLoadForefrontApp"); err != nil {
53+
logging.Errorf("error capturing the screenshot: %v", err)
54+
}
55+
}
4356
if viper.IsSet("focus") {
4457
return a.SetValueOnFocus(viper.GetString("value"))
4558
}
46-
return a.SetValueWithOrder(
59+
if err := a.SetValueWithOrder(
4760
viper.GetString("element"),
4861
viper.GetString("element-type"),
4962
viper.IsSet("strict"),
5063
viper.GetInt("order"),
51-
viper.GetString("value"))
64+
viper.GetString("value")); err != nil {
65+
return err
66+
}
67+
delay.Delay(delay.LONG)
68+
if viper.IsSet(record) {
69+
sfn := fmt.Sprintf("setValue-%s", viper.GetString("element"))
70+
if err := screenshot.CaptureScreen(viper.GetString(recordsPath), sfn); err != nil {
71+
logging.Errorf("error capturing the screenshot: %v", err)
72+
}
73+
}
74+
return nil
5275
}

go.mod

+4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ go 1.19
44

55
require (
66
github.com/go-ole/go-ole v1.2.6
7+
github.com/kbinani/screenshot v0.0.0-20210720154843-7d3a670d8329
78
github.com/openstandia/w32uiautomation v0.0.0-20190416091226-c987b5b65d6b
89
github.com/sirupsen/logrus v1.9.2
910
github.com/spf13/cobra v1.7.0
@@ -15,8 +16,11 @@ require (
1516

1617
require (
1718
github.com/fsnotify/fsnotify v1.6.0 // indirect
19+
github.com/gen2brain/shm v0.0.0-20200228170931-49f9650110c5 // indirect
1820
github.com/hashicorp/hcl v1.0.0 // indirect
1921
github.com/inconshreveable/mousetrap v1.1.0 // indirect
22+
github.com/jezek/xgb v0.0.0-20210312150743-0e0f116e1240 // indirect
23+
github.com/lxn/win v0.0.0-20210218163916-a377121e959e // indirect
2024
github.com/magiconair/properties v1.8.7 // indirect
2125
github.com/mitchellh/mapstructure v1.5.0 // indirect
2226
github.com/pelletier/go-toml/v2 v2.0.8 // indirect

go.sum

+9
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7
5959
github.com/frankban/quicktest v1.14.4 h1:g2rn0vABPOOXmZUj+vbmUp0lPoXEMuhTpIluN0XL9UY=
6060
github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4HY=
6161
github.com/fsnotify/fsnotify v1.6.0/go.mod h1:sl3t1tCWJFWoRz9R8WJCbQihKKwmorjAbSClcnxKAGw=
62+
github.com/gen2brain/shm v0.0.0-20200228170931-49f9650110c5 h1:Y5Q2mEwfzjMt5+3u70Gtw93ZOu2UuPeeeTBDntF7FoY=
63+
github.com/gen2brain/shm v0.0.0-20200228170931-49f9650110c5/go.mod h1:uF6rMu/1nvu+5DpiRLwusA6xB8zlkNoGzKn8lmYONUo=
6264
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
6365
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
6466
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
@@ -127,15 +129,21 @@ github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:
127129
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
128130
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
129131
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
132+
github.com/jezek/xgb v0.0.0-20210312150743-0e0f116e1240 h1:dy+DS31tGEGCsZzB45HmJJNHjur8GDgtRNX9U7HnSX4=
133+
github.com/jezek/xgb v0.0.0-20210312150743-0e0f116e1240/go.mod h1:3P4UH/k22rXyHIJD2w4h2XMqPX4Of/eySEZq9L6wqc4=
130134
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
131135
github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk=
136+
github.com/kbinani/screenshot v0.0.0-20210720154843-7d3a670d8329 h1:qq2nCpSrXrmvDGRxW0ruW9BVEV1CN2a9YDOExdt+U0o=
137+
github.com/kbinani/screenshot v0.0.0-20210720154843-7d3a670d8329/go.mod h1:2VPVQDR4wO7KXHwP+DAypEy67rXf+okUx2zjgpCxZw4=
132138
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
133139
github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg=
134140
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
135141
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
136142
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
137143
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
138144
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
145+
github.com/lxn/win v0.0.0-20210218163916-a377121e959e h1:H+t6A/QJMbhCSEH5rAuRxh+CtW96g0Or0Fxa9IKr4uc=
146+
github.com/lxn/win v0.0.0-20210218163916-a377121e959e/go.mod h1:KxxjdtRkfNoYDCUP5ryK7XJJNTnpC8atvtmTheChOtk=
139147
github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY=
140148
github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0=
141149
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
@@ -310,6 +318,7 @@ golang.org/x/sys v0.0.0-20200523222454-059865788121/go.mod h1:h1NjWce9XRLGQEsW7w
310318
golang.org/x/sys v0.0.0-20200803210538-64077c9b5642/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
311319
golang.org/x/sys v0.0.0-20200905004654-be1d3432aa8f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
312320
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
321+
golang.org/x/sys v0.0.0-20201018230417-eeed37f84f13/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
313322
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
314323
golang.org/x/sys v0.0.0-20201201145000-ef89a241ccb3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
315324
golang.org/x/sys v0.0.0-20210104204734-6f8348627aad/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=

pkg/os/windows/util/toolbar/toolbar.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import (
1111

1212
win32ss "github.com/adrianriobo/goax/pkg/os/windows/api/system-services"
1313
win32wam "github.com/adrianriobo/goax/pkg/os/windows/api/user-interface/windows-and-messages"
14-
win32windows "github.com/adrianriobo/goax/pkg/os/windows/app/ux/windows"
15-
win32process "github.com/adrianriobo/goax/pkg/os/windows/services/process"
14+
win32process "github.com/adrianriobo/goax/pkg/os/windows/util/services/process"
15+
win32windows "github.com/adrianriobo/goax/pkg/os/windows/util/windows"
1616
"github.com/adrianriobo/goax/pkg/util/logging"
1717
)
1818

pkg/util/screenshot/screenshot.go

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package screenshot
2+
3+
import (
4+
"fmt"
5+
"image"
6+
"image/png"
7+
"os"
8+
"path/filepath"
9+
10+
"github.com/kbinani/screenshot"
11+
)
12+
13+
var seq int = 0
14+
15+
func CaptureScreen(outputPath, outputFilename string) error {
16+
return CaptureBounds(screenshot.GetDisplayBounds(0), outputPath, outputFilename)
17+
}
18+
19+
func CaptureBounds(rect image.Rectangle, outputPath, outputFilename string) error {
20+
bounds := screenshot.GetDisplayBounds(0)
21+
img, err := screenshot.CaptureRect(bounds)
22+
if err != nil {
23+
return fmt.Errorf("error capturing the screen: %v", err)
24+
}
25+
fileName := fmt.Sprintf("%s-%s.png", outputFilename, screenshotSequece())
26+
file, err := os.Create(filepath.Join(outputPath, fileName))
27+
if err != nil {
28+
return fmt.Errorf("error creating the file: %v", err)
29+
}
30+
defer file.Close()
31+
err = png.Encode(file, img)
32+
if err != nil {
33+
return fmt.Errorf("error encoding the capture: %v", err)
34+
}
35+
return nil
36+
}
37+
38+
func screenshotSequece() string {
39+
s := fmt.Sprintf("%d", seq)
40+
seq++
41+
return s
42+
}

vendor/github.com/gen2brain/shm/.travis.yml

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/gen2brain/shm/AUTHORS

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/gen2brain/shm/LICENSE

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/gen2brain/shm/README.md

+14
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)