matchers.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package test
  2. import (
  3. "fmt"
  4. "time"
  5. gomegaTypes "github.com/onsi/gomega/types"
  6. )
  7. // BeInFrameAt is a custom matcher that looks for the expected text at the given
  8. // coordinates.
  9. func BeInFrameAt(x, y int) gomegaTypes.GomegaMatcher {
  10. return &textInFrameMatcher{
  11. x: x,
  12. y: y,
  13. found: "",
  14. }
  15. }
  16. type textInFrameMatcher struct {
  17. x int
  18. y int
  19. found string
  20. }
  21. func (matcher *textInFrameMatcher) Match(actual interface{}) (success bool, err error) {
  22. text, _ := actual.(string)
  23. start := time.Now()
  24. for time.Since(start) < perTestTimeout {
  25. matcher.found = GetText(matcher.x, matcher.y, runeCount(text))
  26. if matcher.found == text {
  27. return true, nil
  28. }
  29. time.Sleep(100 * time.Millisecond)
  30. }
  31. return false, fmt.Errorf("Timeout. Expected\n\t%#v\nto be in the Browsh frame, but found\n\t%#v", text, matcher.found)
  32. }
  33. func (matcher *textInFrameMatcher) FailureMessage(text interface{}) (message string) {
  34. return fmt.Sprintf("Expected\n\t%#v\nto equal\n\t%#v", text, matcher.found)
  35. }
  36. func (matcher *textInFrameMatcher) NegatedFailureMessage(text interface{}) (message string) {
  37. return fmt.Sprintf("Expected\n\t%#v\nnot to equal of\n\t%#v", text, matcher.found)
  38. }