oef/regression_test/regression_test.go
2020-02-10 09:41:09 +01:00

251 lines
4.5 KiB
Go

package regression_test
import (
"fmt"
"testing"
"github.com/remogatto/prettytest"
"github.com/tebeka/selenium"
)
var (
wd selenium.WebDriver
)
// Start of setup
type testSuite struct {
prettytest.Suite
}
func TestRunner(t *testing.T) {
prettytest.Run(
t,
new(testSuite),
)
}
func (t *testSuite) BeforeAll() {
var err error
// Connect to the WebDriver instance running locally.
caps := selenium.Capabilities{
"browserName": "firefox",
}
wd, err = selenium.NewRemote(caps, fmt.Sprintf("http://localhost:%d/wd/hub", 4444))
if err != nil {
panic(err)
}
}
func (t *testSuite) AfterAll() {
wd.Quit()
}
func (t *testSuite) TestLogin() {
if err := wd.Get("http://oef_regression_test:3000"); err != nil {
panic(err)
}
err := findElement("#username").SendKeys("admin")
if err != nil {
panic(err)
}
err = findElement("#password").SendKeys("admin")
if err != nil {
panic(err)
}
err = findElement("button").Click()
if err != nil {
panic(err)
}
expected := []string{"JUNIOR Contest", "SENIOR Contest"}
elements := findElements(".list-group a")
for i, el := range elements {
text, err := el.Text()
if err != nil {
panic(err)
}
t.Contain(expected[i], text)
}
logout()
}
func (t *testSuite) TestSchoolSubscription() {
if err := wd.Get("http://oef_regression_test:3000"); err != nil {
panic(err)
}
err := findElement("#school_subscription_link").Click()
if err != nil {
panic(err)
}
err = findElement("#password").SendKeys("subscribe")
if err != nil {
panic(err)
}
err = findElement("button").Click()
if err != nil {
panic(err)
}
err = findElement("#school_name").SendKeys("Liceo GALILEI")
if err != nil {
panic(err)
}
err = findElement("#school_code").SendKeys("FI12346789")
if err != nil {
panic(err)
}
err = findElement("#school_address").SendKeys("via Trieste 1, Milano")
if err != nil {
panic(err)
}
err = findElement("button.dropdown-toggle").Click()
if err != nil {
panic(err)
}
err = findElement(".bs-searchbox input").SendKeys("Friuli")
if err != nil {
panic(err)
}
err = findElement("a.dropdown-item.active").Click()
if err != nil {
panic(err)
}
err = findElement("#school_email").SendKeys("foo.bar@school.org")
if err != nil {
panic(err)
}
err = findElement("#school_contact_person_firstname").SendKeys("Mario")
if err != nil {
panic(err)
}
err = findElement("#school_contact_person_lastname").SendKeys("BROS")
if err != nil {
panic(err)
}
err = findElement("#school_contest_director_firstname").SendKeys("Luigi")
if err != nil {
panic(err)
}
err = findElement("#school_contest_director_lastname").SendKeys("BROS")
if err != nil {
panic(err)
}
err = findElement("button.btn.btn-primary").Click()
if err != nil {
panic(err)
}
expected := []string{"Liceo GALILEI", "foo.bar@school.org"}
elements := findElements("p strong")
t.True(len(elements) > 0)
for i, el := range elements {
text, err := el.Text()
if err != nil {
panic(err)
}
t.Equal(expected[i], text)
}
credentials := findElements("dd")
if len(credentials) == 0 {
panic("Can't find credentials in the renderered HTML page")
}
username, err := credentials[0].Text()
if err != nil {
panic(err)
}
password, err := credentials[1].Text()
if err != nil {
panic(err)
}
logout()
login(username, password)
logout()
login("admin", "admin")
err = findElement("#schools_navbar_link").Click()
if err != nil {
panic(err)
}
err = findElement("#myInput").SendKeys("Liceo GALILEI")
if err != nil {
panic(err)
}
err = findElement("a.oef-selected-by-search").Click()
if err != nil {
panic(err)
}
err = findElement("button.karmen-ajax-delete").Click()
if err != nil {
panic(err)
}
err = findElement("#karmen-modal-btn-confirm").Click()
if err != nil {
panic(err)
}
}
func findElement(selector string) selenium.WebElement {
element, err := wd.FindElement(selenium.ByCSSSelector, selector)
if err != nil {
panic(err)
}
return element
}
func findElements(selector string) []selenium.WebElement {
elements, err := wd.FindElements(selenium.ByCSSSelector, selector)
if err != nil {
panic(err)
}
return elements
}
func login(username, password string) {
err := findElement("#username").SendKeys(username)
if err != nil {
panic(err)
}
err = findElement("#password").SendKeys(password)
if err != nil {
panic(err)
}
err = findElement("button").Click()
if err != nil {
panic(err)
}
}
func logout() {
err := findElement("a#logout").Click()
if err != nil {
panic(err)
}
}