Skip to content

Commit

Permalink
add proper test suite, add elements
Browse files Browse the repository at this point in the history
continuing work on expanding element supported features.
  • Loading branch information
wirepair committed Sep 10, 2015
1 parent 5ac9a96 commit 1c11579
Show file tree
Hide file tree
Showing 14 changed files with 504 additions and 79 deletions.
8 changes: 8 additions & 0 deletions autogcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ func (auto *AutoGcd) Start() error {
return nil
}

func (auto *AutoGcd) Shutdown() {
auto.tabLock.Lock()
for _, tab := range auto.tabs {
auto.debugger.CloseTab(tab.ChromeTarget)
}
auto.tabLock.Unlock()
}

// Returns the first "visual" tab.
func (auto *AutoGcd) GetTab() (*Tab, error) {
auto.tabLock.RLock()
Expand Down
55 changes: 41 additions & 14 deletions autogcd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@ package autogcd

import (
"flag"
"fmt"
"net"
"net/http"
"os"
"testing"
)

var (
testPath string
testDir string
testPort string
testListener net.Listener
testPath string
testDir string
testPort string
testServerAddr string
)

func init() {
Expand All @@ -20,30 +25,35 @@ func init() {

func TestMain(m *testing.M) {
flag.Parse()
testServer()
ret := m.Run()
testCleanUp()
os.Exit(ret)
}

func testCleanUp() {

testListener.Close()
}

func TestStart(t *testing.T) {
s := NewSettings(testPath, testDir, testPort)
s := NewSettings(testPath, testDir)
s.SetStartTimeout(15)
s.SetChromeHost("localhost")
auto := NewAutoGcd(s)
defer auto.Shutdown()

if err := auto.Start(); err != nil {
t.Fatalf("failed to start chrome: %s\n", err)
}
auto.debugger.ExitProcess()

}

func TestGetTab(t *testing.T) {
var err error
var tab *Tab
auto := testDefaultStartup(t)
defer auto.Shutdown()

tab, err = auto.GetTab()
if err != nil {
t.Fatalf("Error getting tab: %s\n", err)
Expand All @@ -52,30 +62,31 @@ func TestGetTab(t *testing.T) {
if tab.Target.Type != "page" {
t.Fatalf("Got tab but wasn't of type Page")
}
auto.debugger.ExitProcess()

}

func TestNewTab(t *testing.T) {
var err error
//var newTab *Tab
auto := testDefaultStartup(t)
defer auto.Shutdown()

tabLen := len(auto.tabs)
_, err = auto.NewTab()
_, err := auto.NewTab()
if err != nil {
t.Fatalf("error creating new tab: %s\n", err)
}

if tabLen+1 != len(auto.tabs) {
t.Fatalf("error created new tab but not reflected in our map")
}

auto.debugger.ExitProcess()
}

func TestCloseTab(t *testing.T) {
var err error
var newTab *Tab
auto := testDefaultStartup(t)
defer auto.Shutdown()

tabLen := len(auto.tabs)

newTab, err = auto.NewTab()
Expand All @@ -95,15 +106,31 @@ func TestCloseTab(t *testing.T) {
if _, err := auto.tabById(newTab.Target.Id); err == nil {
t.Fatalf("error closed tab still in our map")
}

auto.debugger.ExitProcess()
}

func testDefaultStartup(t *testing.T) *AutoGcd {
s := NewSettings(testPath, testDir, testPort)
s := NewSettings(testPath, testDir)
s.SetDebuggerPort(testRandomPort(t))
auto := NewAutoGcd(s)
if err := auto.Start(); err != nil {
t.Fatalf("failed to start chrome: %s\n", err)
}
return auto
}

func testServer() {
testListener, _ = net.Listen("tcp", ":0")
_, testServerPort, _ := net.SplitHostPort(testListener.Addr().String())
testServerAddr = fmt.Sprintf("http://localhost:%s/", testServerPort)
go http.Serve(testListener, http.FileServer(http.Dir("testdata/")))
}

func testRandomPort(t *testing.T) string {
l, err := net.Listen("tcp", ":0")
if err != nil {
t.Fatal(err)
}
_, randPort, _ := net.SplitHostPort(l.Addr().String())
l.Close()
return randPort
}
42 changes: 41 additions & 1 deletion element.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,24 @@ func newElement(tab *Tab, id int) *Element {
return e
}

// Get attributes of the node in sequential name,value pairs in the slice.
func (e *Element) GetAttributes() (map[string]string, error) {
attributes := make(map[string]string)
attr, err := e.tab.DOM().GetAttributes(e.id)
if err != nil {
return nil, err
}
for i := 0; i < len(attr); i += 2 {
attributes[attr[i]] = attr[i+1]
}
return attributes, nil
}

func (e *Element) GetSource() (string, error) {
return e.tab.DOM().GetOuterHTML(e.id)
}

// Clicks the element in the center of the element.
func (e *Element) Click() error {
var x int
var y int
Expand All @@ -42,6 +60,28 @@ func (e *Element) Click() error {
return e.tab.Click(x, y)
}

func (e *Element) SendKeys(text string) error {
//type ( enumerated string [ "char" , "keyDown" , "keyUp" , "rawKeyDown" ] )
err := e.Click()
if err != nil {
return err
}
theType := "char"
modifiers := 0
timestamp := 0.0
unmodifiedText := ""
keyIdentifier := ""
code := ""
key := ""
windowsVirtualKeyCode := 0
nativeVirtualKeyCode := 0
autoRepeat := false
isKeypad := false
isSystemKey := false
_, err = e.tab.Input().DispatchKeyEvent(theType, modifiers, timestamp, text, unmodifiedText, keyIdentifier, code, key, windowsVirtualKeyCode, nativeVirtualKeyCode, autoRepeat, isKeypad, isSystemKey)
return err
}

func (e *Element) Dimensions() ([]float64, error) {
var points []float64
box, err := e.tab.DOM().GetBoxModel(e.id)
Expand All @@ -65,5 +105,5 @@ func centroid(points []float64) (int, int, error) {
x += int(points[i])
y += int(points[i+1])
}
return x / pointLen, y / pointLen, nil
return x / (pointLen / 2), y / (pointLen / 2), nil
}
Loading

0 comments on commit 1c11579

Please sign in to comment.