forked from hbollon/IGopher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
engine.go
271 lines (248 loc) · 8.45 KB
/
engine.go
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
package igopher
import (
"errors"
"fmt"
"os"
"os/signal"
"path/filepath"
"runtime"
"strings"
"syscall"
"time"
"github.com/hbollon/igopher/internal/process"
"github.com/hbollon/igopher/internal/proxy"
"github.com/sirupsen/logrus"
log "github.com/sirupsen/logrus"
"github.com/tebeka/selenium"
"github.com/tebeka/selenium/chrome"
)
const (
locatorID = "ID"
locatorName = "NAME"
locatorXPath = "XPATH"
locatorCSS = "CSS"
)
var (
seleniumPath = filepath.FromSlash("./lib/selenium-server.jar")
chromePath, chromeDriverPath, geckoDriverPath string
)
func init() {
process.Init("./data/pid.txt")
if runtime.GOOS == "windows" {
geckoDriverPath = filepath.FromSlash("./lib/geckodriver.exe")
chromeDriverPath = filepath.FromSlash("./lib/chromedriver.exe")
chromePath = filepath.FromSlash("./lib/chrome-win/chrome.exe")
} else if runtime.GOOS == "darwin" {
geckoDriverPath = filepath.FromSlash("./lib/geckodriver")
chromeDriverPath = filepath.FromSlash("./lib/chromedriver")
chromePath = filepath.FromSlash("./lib/chrome-mac/Chromium.app/Contents/MacOS/Chromium")
} else {
geckoDriverPath = filepath.FromSlash("./lib/geckodriver")
chromeDriverPath = filepath.FromSlash("./lib/chromedriver")
chromePath = filepath.FromSlash("./lib/chrome-linux/chrome")
}
}
// Selenium instance and opts
type Selenium struct {
Instance *selenium.Service
Config *ClientConfig
Opts []selenium.ServiceOption
Proxy proxy.Proxy `yaml:"proxy"`
WebDriver selenium.WebDriver
SigTermRoutineExit chan bool
}
// InitializeSelenium start a Selenium WebDriver server instance
// (if one is not already running).
func (s *Selenium) InitializeSelenium(clientConfig *ClientConfig) {
var err error
s.Config = clientConfig
var output *os.File
if s.Config.Debug {
output = os.Stderr
} else {
output = nil
}
s.Opts = []selenium.ServiceOption{
selenium.GeckoDriver(geckoDriverPath), // Specify the path to GeckoDriver in order to use Firefox.
selenium.ChromeDriver(chromeDriverPath), // Specify the path to ChromeDriver in order to use Chrome.
selenium.Output(output), // Output debug information to stderr.
}
if s.Config.Headless {
s.Opts = append(s.Opts, selenium.StartFrameBuffer())
}
selenium.SetDebug(s.Config.Debug)
s.Instance, err = selenium.NewSeleniumService(seleniumPath, int(s.Config.Port), s.Opts...)
if err != nil {
log.Fatal(err) // Fatal error, exit if webdriver can't be initialize.
}
if s.SigTermRoutineExit == nil {
s.SigTermCleaning()
}
}
// InitFirefoxWebDriver init and launch web driver with Firefox
func (s *Selenium) InitFirefoxWebDriver() {
var err error
caps := selenium.Capabilities{"browserName": "firefox"}
s.WebDriver, err = selenium.NewRemote(caps, fmt.Sprintf("http://localhost:%d/wd/hub", s.Config.Port))
if err != nil {
log.Error(err)
}
}
// InitChromeWebDriver init and launch web driver with Chrome
func (s *Selenium) InitChromeWebDriver() {
var err error
caps := selenium.Capabilities{"browserName": "chrome"}
chromeCaps := chrome.Capabilities{
Path: filepath.FromSlash(chromePath),
Args: []string{
"--incognito",
"--disable-extensions",
"--disable-infobars",
"--disable-dev-shm-usage",
"--no-sandbox",
"--window-size=360,740",
},
MobileEmulation: &chrome.MobileEmulation{
DeviceMetrics: &chrome.DeviceMetrics{
Width: 360,
Height: 740,
PixelRatio: 2.05,
},
UserAgent: "Mozilla/5.0 (Linux; Android 8.0.0; SM-G960F Build/R16NW) " +
"AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.137 Mobile Safari/537.36",
},
}
caps.AddChrome(chromeCaps)
if s.Proxy.Enabled {
logrus.Debug("Proxy activated.")
if s.Proxy.WithAuth {
s.Proxy.LaunchLocalForwarder()
caps.AddProxy(selenium.Proxy{
Type: selenium.Manual,
HTTP: "127.0.0.1:8880",
FTP: "127.0.0.1:8880",
SSL: "127.0.0.1:8880",
NoProxy: nil,
})
} else {
caps.AddProxy(selenium.Proxy{
Type: selenium.Manual,
HTTP: fmt.Sprintf("%s:%d", s.Proxy.RemoteIP, s.Proxy.RemotePort),
FTP: fmt.Sprintf("%s:%d", s.Proxy.RemoteIP, s.Proxy.RemotePort),
SSL: fmt.Sprintf("%s:%d", s.Proxy.RemoteIP, s.Proxy.RemotePort),
NoProxy: nil,
})
}
}
s.WebDriver, err = selenium.NewRemote(caps, fmt.Sprintf("http://localhost:%d/wd/hub", s.Config.Port))
if err != nil {
log.Error(err)
}
}
// CloseSelenium close webdriver and selenium instances
func (s *Selenium) CloseSelenium() {
if s.WebDriver != nil {
s.WebDriver.Close()
s.WebDriver.Quit()
s.WebDriver = nil
logrus.Debug("Closed webdriver")
}
if s.Instance != nil {
s.Instance.Stop()
s.Instance = nil
logrus.Debug("Closed selenium instance")
}
}
// SigTermCleaning launch a gouroutine to handle SigTerm signal and trigger Selenium and Webdriver closing if it raised
func (s *Selenium) SigTermCleaning() {
sig := make(chan os.Signal, 1)
s.SigTermRoutineExit = make(chan bool)
signal.Notify(sig, os.Interrupt, syscall.SIGTERM)
go func() {
for {
select {
case <-sig:
s.CleanUp()
os.Exit(1)
case <-s.SigTermRoutineExit:
s.SigTermRoutineExit = nil
return
default:
break
}
time.Sleep(10 * time.Millisecond)
}
}()
}
// CleanUp clean app ressources including Selenium stuff and proxy-login-automator instance (if exist)
func (s *Selenium) CleanUp() {
s.CloseSelenium()
s.Proxy.StopForwarderProxy()
process.DeletePidFile()
logrus.Info("IGopher's ressources successfully cleared!")
}
/* Browser methods */
// IsElementPresent check if an element is present on the current webpage
func (s *Selenium) IsElementPresent(by, value string) bool {
_, err := s.WebDriver.FindElement(by, value)
if err != nil {
log.Debugf("Element not found by %s: %v", by, err)
return false
}
return true
}
// GetElement wait for element and then return when it's available
func (s *Selenium) GetElement(elementTag, locator string) (selenium.WebElement, error) {
locator = strings.ToUpper(locator)
if locator == locatorID && s.IsElementPresent(selenium.ByID, elementTag) {
return s.WebDriver.FindElement(selenium.ByID, elementTag)
} else if locator == locatorName && s.IsElementPresent(selenium.ByName, elementTag) {
return s.WebDriver.FindElement(selenium.ByName, elementTag)
} else if locator == locatorXPath && s.IsElementPresent(selenium.ByXPATH, elementTag) {
return s.WebDriver.FindElement(selenium.ByXPATH, elementTag)
} else if locator == locatorCSS && s.IsElementPresent(selenium.ByCSSSelector, elementTag) {
return s.WebDriver.FindElement(selenium.ByCSSSelector, elementTag)
} else {
log.Debugf("Incorrect locator '%s'", locator)
return nil, errors.New("Incorrect locator")
}
}
// GetElements wait for elements and then return when they're available
func (s *Selenium) GetElements(elementTag, locator string) ([]selenium.WebElement, error) {
locator = strings.ToUpper(locator)
if locator == locatorID && s.IsElementPresent(selenium.ByID, elementTag) {
return s.WebDriver.FindElements(selenium.ByID, elementTag)
} else if locator == locatorName && s.IsElementPresent(selenium.ByName, elementTag) {
return s.WebDriver.FindElements(selenium.ByName, elementTag)
} else if locator == locatorXPath && s.IsElementPresent(selenium.ByXPATH, elementTag) {
return s.WebDriver.FindElements(selenium.ByXPATH, elementTag)
} else if locator == locatorCSS && s.IsElementPresent(selenium.ByCSSSelector, elementTag) {
return s.WebDriver.FindElements(selenium.ByCSSSelector, elementTag)
} else {
log.Debugf("Incorrect locator '%s'", locator)
return nil, errors.New("Incorrect locator")
}
}
// WaitForElement search and wait until searched element appears.
// Delay argument is in seconds.
func (s *Selenium) WaitForElement(elementTag, locator string, delay int) (bool, error) {
locator = strings.ToUpper(locator)
s.WebDriver.SetImplicitWaitTimeout(0)
defer s.WebDriver.SetImplicitWaitTimeout(30)
timeout := time.After(time.Duration(delay) * time.Second)
tick := time.NewTicker(500 * time.Millisecond)
for {
select {
case <-timeout:
return false, errors.New("Timed out : element not found")
case <-tick.C:
if (locator == locatorID && s.IsElementPresent(selenium.ByID, elementTag)) ||
(locator == locatorName && s.IsElementPresent(selenium.ByName, elementTag)) ||
(locator == locatorXPath && s.IsElementPresent(selenium.ByXPATH, elementTag)) ||
(locator == locatorCSS && s.IsElementPresent(selenium.ByCSSSelector, elementTag)) {
return true, nil
}
}
time.Sleep(10 * time.Millisecond)
}
}