package regression_test import ( "fmt" "strconv" "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", SchoolContactPersonEmail: "mario.bros@nintendo.com", SchoolContactPersonTelephoneNumber: "123 456 789", ContestDirectorFirstname: "Luigi", ContestDirectorLastname: "BROS", PreviousParticipantsSelection: 30, }) 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", Year: 2, StudyField: "Human Science", Category: &orm.Category{Name: "Junior"}, }) // Subscribe another participant addParticipantAsSchool(&orm.Participant{ Firstname: "Serena", Lastname: "BIANCA", FiscalCode: "BNCSR1234567", Year: 4, StudyField: "Economics", 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", Year: 4, StudyField: "Economics", 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", Year: 4, StudyField: "Economics", 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 (t *testSuite) TestParticipantResponse() { login("WINBOXGUF", "GUf3wIP0DF7") err := findElement("a.btn-primary").Click() if err != nil { panic(err) } err = findElement("#update-response").Click() if err != nil { panic(err) } correct := []string{ "97", "129", "105", "113", "93", "89", "109", "141", "157", "85", "133", "153", "121", "117", "101", "149", "81", "145", "125", "137", } for _, answerId := range correct { err = findElement("#answer_" + answerId).Click() if err != nil { panic(err) } } err = findElement("button.btn-primary").Click() if err != nil { panic(err) } logout() login("admin", "admin") err = findElement("#responses_navbar_link").Click() if err != nil { panic(err) } err = findElement("#myInput").SendKeys("BOXILL") if err != nil { panic(err) } err = findElement("a.oef-selected-by-search").Click() if err != nil { panic(err) } scoreEl := findElement("#score") score, err := scoreEl.Text() t.Nil(err) t.Equal("20", score) err = findElement("a.btn-primary").Click() if err != nil { panic(err) } err = findElement("#reset_responses").Click() if err != nil { panic(err) } err = findElement("button.btn-primary").Click() if err != nil { panic(err) } logout() } func (t *testSuite) TestContestActiveWindow() { login("admin", "admin") err := searchAndClick("junior") if err != nil { panic(err) } err = findElement("a.btn-primary").Click() if err != nil { panic(err) } err = findElement("#contest_description").SendKeys("Gara per la categoria JUNIOR") err = findElement("#contest_date").SendKeys("2020-03-04") err = findElement("#contest_start_time").SendKeys("14:00") err = findElement("#contest_end_time").SendKeys("15:00") err = findElement("#contest_duration").SendKeys("45") err = findElement(".btn-primary").Click() t.True(true) logout() } 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_contact_person_email").SendKeys(school.SchoolContactPersonEmail) if err != nil { panic(err) } err = findElement("#school_contact_person_telephone_number").SendKeys(school.SchoolContactPersonTelephoneNumber) 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("#school_previous_participants_selection").SendKeys(strconv.Itoa(int(school.PreviousParticipantsSelection))) 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("#participant_year").SendKeys(strconv.Itoa(participant.Year)) if err != nil { panic(err) } err = findElement("#participant_study_field").SendKeys(participant.StudyField) 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) } } func searchAndClick(text string) error { err := findElement("#myInput").SendKeys(text) if err != nil { return err } err = findElement("a.oef-selected-by-search").Click() if err != nil { return err } return nil }