Skip to content

Commit

Permalink
Lots of new integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tombh committed Apr 7, 2018
1 parent 3d0b2ec commit 1acb178
Show file tree
Hide file tree
Showing 16 changed files with 344 additions and 76 deletions.
20 changes: 10 additions & 10 deletions interfacer/Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion interfacer/contrib/run_tests.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/bash
set -e

go test test/browsh_test.go
go test test/*.go
2 changes: 2 additions & 0 deletions interfacer/src/browsh/browsh.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ var (
// (See #1114 for rationale)
"datareporting.policy.firstRunURL": "''",
}
// TestServerPort ... Port for the test server
TestServerPort = "4444"
)

func setupLogging() {
Expand Down
2 changes: 1 addition & 1 deletion interfacer/src/browsh/firefox.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func startWERFirefox() {
"--firefox=" + rootDir + "/webext/contrib/firefoxheadless.sh",
"--verbose",
"--no-reload",
"--url=http://www.something.com/",
"--url=http://localhost:" + TestServerPort + "/smorgasbord",
}
firefoxProcess := exec.Command(rootDir+"/webext/node_modules/.bin/web-ext", args...)
firefoxProcess.Dir = rootDir + "/webext/dist/"
Expand Down
101 changes: 55 additions & 46 deletions interfacer/test/browsh_test.go
Original file line number Diff line number Diff line change
@@ -1,67 +1,76 @@
package main
package test

import (
"strings"
"testing"
"time"
"strconv"
"fmt"

"github.com/gdamore/tcell"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

"browsh"
)

var simScreen tcell.SimulationScreen
var startupWait = 10
var browserFingerprint = " ← | x | "
var rootDir = browsh.Shell("git rev-parse --show-toplevel")

func TestIntegration(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Integration tests")
}

func GetFrameText() string {
var text string
cells, _, _ := simScreen.GetContents()
for _, element := range cells {
text += string(element.Runes)
}
fmt.Println(text)
return text
}

var _ = Describe("Integration", func() {
var _ = Describe("Showing a basic webpage", func() {
BeforeEach(func() {
var count = 0
simScreen = tcell.NewSimulationScreen("UTF-8")
go browsh.Start(simScreen)
for {
if count > startupWait {
var message = "Couldn't find browsh " +
"startup signature within " +
strconv.Itoa(startupWait) +
" seconds"
panic(message)
}
time.Sleep(time.Second)
if (strings.Contains(GetFrameText(), browserFingerprint)) {
break
}
count++
}
})
GotoURL(testSiteURL + "/smorgasbord/")
})

Describe("Browser UI", func() {
It("should have the page title, buttons and the current URL", func() {
Expect("Smörgåsbord").To(BeInFrameAt(0, 0))
Expect(" ← |").To(BeInFrameAt(0, 1))
Expect(" x |").To(BeInFrameAt(4, 1))
URL := testSiteURL + "/smorgasbord/"
Expect(URL).To(BeInFrameAt(9, 1))
})

Describe("Interaction", func() {
It("should navigate to a new page by using the URL bar", func() {
SpecialKey(tcell.KeyCtrlL)
Keyboard(testSiteURL + "/smorgasbord/another.html")
SpecialKey(tcell.KeyEnter)
Expect("Another").To(BeInFrameAt(0, 0))
})

AfterEach(func() {
browsh.Shell(rootDir + "/webext/contrib/firefoxheadless.sh kill")
It("should navigate to a new page by clicking a link", func() {
simScreen.InjectMouse(12, 21, tcell.Button1, tcell.ModNone)
Expect("Another").To(BeInFrameAt(0, 0))
})

It("should scroll the page by one line", func() {
SpecialKey(tcell.KeyDown)
Expect("meal,▄originating▄in▄").To(BeInFrameAt(12, 12))
})

It("should scroll the page by one page", func() {
SpecialKey(tcell.KeyPgDn)
Expect("continuing▄with▄a▄variety▄of▄fish").To(BeInFrameAt(12, 10))
})
})
})

Describe("Showing a basic webpage", func() {
It("have the right text", func() {
Expect(GetFrameText()).To(ContainSubstring("Something"))
Describe("Rendering", func() {
It("should render dynamic content", func() {
Expect([3]int32{0, 255, 255}).To(Equal(GetFgColour(39, 3)))
waitForNextFrame()
Expect([3]int32{255, 0, 255}).To(Equal(GetFgColour(39, 3)))
})

It("should switch to monochrome mode", func() {
simScreen.InjectKey(tcell.KeyRune, 'M', tcell.ModAlt)
waitForNextFrame()
Expect([3]int32{0, 0, 0}).To(Equal(GetBgColour(0, 2)))
Expect([3]int32{255, 255, 255}).To(Equal(GetFgColour(12, 11)))
})

Describe("Text positioning", func() {
It("should position the left/right-aligned coloumns", func() {
Expect("Smörgåsbord▄(Swedish:").To(BeInFrameAt(12, 11))
Expect("The▄Swedish▄word").To(BeInFrameAt(42, 11))
})
})
})
})
44 changes: 44 additions & 0 deletions interfacer/test/matchers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package test

import (
"fmt"
"time"

gomegaTypes "github.com/onsi/gomega/types"
)

// BeInFrameAt is a custom matcher that looks for the expected text at the given
// coordinates.
func BeInFrameAt(x, y int) gomegaTypes.GomegaMatcher {
return &textInFrameMatcher{
x: x,
y: y,
found: "",
}
}

type textInFrameMatcher struct {
x int
y int
found string
}

func (matcher *textInFrameMatcher) Match(actual interface{}) (success bool, err error) {
text, _ := actual.(string)
start := time.Now()
for time.Since(start) < perTestTimeout {
matcher.found = GetText(matcher.x, matcher.y, runeCount(text))
if matcher.found == text { return true, nil }
time.Sleep(100 * time.Millisecond)
}
return false, fmt.Errorf("Timeout. Expected\n\t%#v\nto be in the Browsh frame, but found\n\t%#v", text, matcher.found)
}

func (matcher *textInFrameMatcher) FailureMessage(text interface{}) (message string) {
return fmt.Sprintf("Expected\n\t%#v\nto equal\n\t%#v", text, matcher.found)
}

func (matcher *textInFrameMatcher) NegatedFailureMessage(text interface{}) (message string) {
return fmt.Sprintf("Expected\n\t%#v\nnot to equal of\n\t%#v", text, matcher.found)
}

Loading

0 comments on commit 1acb178

Please sign in to comment.