Skip to content

Commit

Permalink
initial support for js timeout (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
llonchj committed Aug 2, 2017
1 parent 711a655 commit b7956b1
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 11 deletions.
33 changes: 22 additions & 11 deletions assertion.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package browsersteps

import (
"context"
"fmt"
"time"

"github.com/DATA-DOG/godog"
)
Expand Down Expand Up @@ -38,18 +40,27 @@ func (b *BrowserSteps) iShouldSeePageTitleAs(expectedTitle string) error {
}

func (b *BrowserSteps) iShouldSeeIn(expectedText, selector, by string) error {
element, err := b.GetWebDriver().FindElement(by, selector)
if err != nil {
return err
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
for {
select {
case <-time.After(50 * time.Millisecond):
element, err := b.GetWebDriver().FindElement(by, selector)
if err != nil {
break
}
gotText, err := element.Text()
if err != nil {
break
}
if expectedText != gotText {
break
}
return nil
case <-ctx.Done():
return ctx.Err()
}
}
gotText, err := element.Text()
if err != nil {
return err
}
if expectedText != gotText {
return fmt.Errorf("Title Mismatch. Got '%s', Expected '%s'", gotText, expectedText)
}
return nil
}

func (b *BrowserSteps) iShouldSee(selector, by string) error {
Expand Down
9 changes: 9 additions & 0 deletions features/assertion/shouldsee.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Feature: Should See Assertion

This feature supports visible/invisible assertion steps

Scenario: Should See Element
Given I am a anonymous user
When I navigate to "/js.html"
And I submit "submit" id
Then I should see "Done!" in "p" id
40 changes: 40 additions & 0 deletions public/js.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<!DOCTYPE html>
<html>

<head>
<title>Test server</title>
<style>
h1 {
text-align: center;
}

body {
font-family: Tahoma, Geneva, Verdana, sans-serif;
}
</style>
</head>

<body>
<form id="myform" onsubmit="javascript: return formSubmit();">
<label for="timeout">Timeout(ms)</label>
<input id="timeout" name="timeout" value="300">
<input type="submit" name="submit" id="submit" value="Submit">
</form>
<p id="p">Hello!</p>
<script>
function doIt(t) {
var ele = document.getElementById("p");
ele.innerHTML = t === undefined ? "Done!" : t;
}

function formSubmit() {
doIt("Hello!");
var ele = document.getElementById("timeout");
var timeout = parseInt(ele.value);
setTimeout(doIt, timeout);
return false;
}
</script>
</body>

</html>

0 comments on commit b7956b1

Please sign in to comment.