oef/regression_test/regression_test.go

438 lines
8.7 KiB
Go

package regression_test
import (
"fmt"
"testing"
"git.andreafazzi.eu/andrea/oef/orm"
"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)
}
addSchoolAsSubscriber(&orm.School{
Name: "Liceo GALILEI",
Code: "Fi12346789",
Address: "via Trieste 1, Milano",
Region: &orm.Region{Name: "Friuli"},
Email: "foo.bar@school.org",
SchoolContactPersonFirstname: "Mario",
SchoolContactPersonLastname: "BROS",
ContestDirectorFirstname: "Luigi",
ContestDirectorLastname: "BROS",
})
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)
// Subscribe a participant
addParticipantAsSchool(&orm.Participant{
Firstname: "Mario",
Lastname: "ROSSI",
FiscalCode: "RSSMR1234567",
Category: &orm.Category{Name: "Junior"},
})
// Subscribe another participant
addParticipantAsSchool(&orm.Participant{
Firstname: "Serena",
Lastname: "BIANCA",
FiscalCode: "BNCSR1234567",
Category: &orm.Category{Name: "Senior"},
})
alert, err := findElement(".alert").Text()
t.Nil(err)
t.Contain("Iscrizione completa", alert)
findElement("#school_navbar_link").Click()
expected = []string{"Mario ROSSI", "Serena BIANCA"}
elements = findElements("#partecipanti_list_group a")
t.True(len(elements) > 0)
for i, el := range elements {
text, err := el.Text()
if err != nil {
panic(err)
}
t.Contain(expected[i], text)
}
// Open the participant view
first := findElements("#partecipanti_list_group a")[0]
err = first.Click()
if err != nil {
panic(err)
}
// Remove the participant
err = findElement("button.karmen-ajax-delete").Click()
if err != nil {
panic(err)
}
err = findElement("#karmen-modal-btn-confirm").Click()
if err != nil {
panic(err)
}
// Subscribe a participant of the same existing category
addParticipantAsSchool(&orm.Participant{
Firstname: "Luigi",
Lastname: "VERDI",
FiscalCode: "VRDLGSR1234567",
Category: &orm.Category{Name: "Senior"},
})
alert, err = findElement(".alert").Text()
t.Nil(err)
t.Contain("partecipante di questa categoria", alert)
err = findElement(".alert a").Click()
if err != nil {
panic(err)
}
// Subscribe a participant with the same fiscal code
populateParticipantForm(&orm.Participant{
Firstname: "Luigi",
Lastname: "VERDI",
FiscalCode: "BNCSR1234567",
Category: &orm.Category{Name: "Senior"},
})
clickPrimaryButton()
alert, err = findElement(".alert").Text()
if err != nil {
panic(err)
}
t.Contain("codice fiscale è già presente", alert)
removeSchoolAsAdmin("Liceo GALILEI")
removeParticipantAsAdmin("BIANCA")
}
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 := wd.Get("http://oef_regression_test:3000")
if err != nil {
panic(err)
}
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)
}
}
func removeSchoolAsAdmin(name string) {
logout()
login("admin", "admin")
err := findElement("#schools_navbar_link").Click()
if err != nil {
panic(err)
}
err = findElement("#myInput").SendKeys(name)
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 removeParticipantAsAdmin(name string) {
logout()
login("admin", "admin")
err := findElement("#participants_navbar_link").Click()
if err != nil {
panic(err)
}
err = findElement("#myInput").SendKeys(name)
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 addSchoolAsSubscriber(school *orm.School) {
err := findElement("#school_name").SendKeys(school.Name)
if err != nil {
panic(err)
}
err = findElement("#school_code").SendKeys(school.Code)
if err != nil {
panic(err)
}
err = findElement("#school_address").SendKeys(school.Address)
if err != nil {
panic(err)
}
err = findElement("button.dropdown-toggle").Click()
if err != nil {
panic(err)
}
err = findElement(".bs-searchbox input").SendKeys(school.Region.Name)
if err != nil {
panic(err)
}
err = findElement("a.dropdown-item.active").Click()
if err != nil {
panic(err)
}
err = findElement("#school_email").SendKeys(school.Email)
if err != nil {
panic(err)
}
err = findElement("#school_contact_person_firstname").SendKeys(school.SchoolContactPersonFirstname)
if err != nil {
panic(err)
}
err = findElement("#school_contact_person_lastname").SendKeys(school.SchoolContactPersonLastname)
if err != nil {
panic(err)
}
err = findElement("#school_contest_director_firstname").SendKeys(school.ContestDirectorFirstname)
if err != nil {
panic(err)
}
err = findElement("#school_contest_director_lastname").SendKeys(school.ContestDirectorLastname)
if err != nil {
panic(err)
}
err = findElement("button.btn.btn-primary").Click()
if err != nil {
panic(err)
}
}
func addParticipantAsSchool(participant *orm.Participant) {
err := findElement("#new_participant_subscribe").Click()
if err != nil {
panic(err)
}
populateParticipantForm(participant)
clickPrimaryButton()
}
func populateParticipantForm(participant *orm.Participant) {
err := findElement("#participant_firstname").SendKeys(participant.Firstname)
if err != nil {
panic(err)
}
err = findElement("#participant_lastname").SendKeys(participant.Lastname)
if err != nil {
panic(err)
}
err = findElement("#participant_fiscalcode").SendKeys(participant.FiscalCode)
if err != nil {
panic(err)
}
err = findElement("button.dropdown-toggle").Click()
if err != nil {
panic(err)
}
err = findElement(".bs-searchbox input").SendKeys(participant.Category.Name)
if err != nil {
panic(err)
}
err = findElement("a.dropdown-item.active").Click()
if err != nil {
panic(err)
}
}
func clickPrimaryButton() {
err := findElement("button.btn.btn-primary").Click()
if err != nil {
panic(err)
}
}