-
Notifications
You must be signed in to change notification settings - Fork 0
/
GOpom.go
80 lines (63 loc) · 1.8 KB
/
GOpom.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
package GOpom
import (
"errors"
"fmt"
"io/ioutil"
"log"
"net/url"
"os"
"path/filepath"
"github.com/DATA-DOG/godog"
"github.com/tebeka/selenium"
)
/*BrowserSteps represents a WebDriver context to run the Scenarios*/
type BrowserSteps struct {
wd selenium.WebDriver
Capabilities selenium.Capabilities
DefaultURL string
URL *url.URL
ScreenshotPath string
}
/*SetBaseURL sets the absolute URL used to complete relative URLs*/
func (b *BrowserSteps) SetBaseURL(url *url.URL) error {
if !url.IsAbs() {
return errors.New("BaseURL must be absolute")
}
b.URL = url
return nil
}
//BeforeScenario is executed before each scenario
func (b *BrowserSteps) BeforeScenario(a interface{}) {
var err error
b.wd, err = selenium.NewRemote(b.Capabilities, b.DefaultURL)
if err != nil {
log.Panic(err)
}
}
//AfterScenario is executed after each scenario
func (b *BrowserSteps) AfterScenario(a interface{}, err error) {
if err != nil && b.ScreenshotPath != "" {
filename := fmt.Sprintf("FAILED STEP - %s.png", err.Error())
buff, err := b.GetWebDriver().Screenshot()
if err != nil {
fmt.Printf("Error %+v\n", err)
}
if _, err := os.Stat(b.ScreenshotPath); os.IsNotExist(err) {
os.MkdirAll(b.ScreenshotPath, 0755)
}
pathname := filepath.Join(b.ScreenshotPath, filename)
ioutil.WriteFile(pathname, buff, 0644)
}
b.GetWebDriver().Quit()
}
func (b *BrowserSteps) buildSteps(s *godog.Suite) {
b.googleSearch(s)
}
//NewBrowserSteps starts a new BrowserSteps instance.
func NewBrowserSteps(s *godog.Suite, cap selenium.Capabilities, defaultURL string) *BrowserSteps {
bs := &BrowserSteps{Capabilities: cap, DefaultURL: defaultURL, ScreenshotPath: os.Getenv("SCREENSHOT_PATH")}
bs.buildSteps(s)
s.BeforeScenario(bs.BeforeScenario)
s.AfterScenario(bs.AfterScenario)
return bs
}