func Test_Register_AlreadyRegistered(t *testing.T) { SubmitRegistration(userOne, t) webdriver.WaitFor(5*time.Second, webdriver.ElementToVanish, "div[class='selenium-flag']") // Case 1: non-unique email const emailErr = "Already associated with a user" msg, err := webdriver.FetchText("span[name='EmailAddrErrorMsg']") if err != nil { t.Fatalf("Failed to fetch the email error message: %s", err) } if msg != emailErr { t.Fatalf("Expected error \"%s\" was in fact, \"%s\"", emailErr, msg) } // Case 2: non-unique user id const userErr = "Not available" origEmailAddr := userOne.EmailAddr userOne.EmailAddr = "[email protected] " SubmitRegistration(userOne, t) userOne.EmailAddr = origEmailAddr webdriver.WaitFor(5*time.Second, webdriver.ElementToVanish, "div[class='selenium-flag']") msg, err = webdriver.FetchText("span[name='UserIdErrorMsg']") if err != nil { t.Fatalf("Failed to fetch the user id error message: %s", err) } if msg != userErr { t.Fatalf("Expected error \"%s\" was in fact, \"%s\"", userErr, msg) } }
func RequestPasswordResetFor(inEmailAddr string, t *testing.T) { url := "https://plog.org:8004/#/password" err := webdriver.Drv.Get(url) if err != nil { t.Fatalf("Failed to load %s: %s\n", url, err) } webdriver.WaitFor(5*time.Second, webdriver.ElementToVanish, "div[class='selenium-flag']") // Get elements elements, err := webdriver.FindNamedElements([]string{"Message", "EmailAddr", "ResetPasswordButton"}) if err != nil { t.Fatalf("FindNamedElements failed: %s", err) } // Fill elements["EmailAddr"].Clear() elements["EmailAddr"].SendKeys(inEmailAddr) // Submit elements["ResetPasswordButton"].Click() webdriver.WaitFor(5*time.Second, webdriver.ElementToVanish, "div[class='selenium-flag']") // Verify expected results actualMsg, err := webdriver.FetchText("p[name='Message']") if err != nil { t.Fatalf("Failed to fetch the main message: %s", err) } expectMsg := fmt.Sprintf("Check email (%s) for a reset token", inEmailAddr) if actualMsg != expectMsg { t.Fatalf("Expected message \"%s\", got \"%s\"", expectMsg, actualMsg) } }
func ExpectOnProfilePage(t *testing.T) { webdriver.WaitFor(5*time.Second, webdriver.ElementToVanish, "div[class='selenium-flag']") elist := []string{"editProfileForm", "Message", "UserId", "FirstName", "LastName", "EmailAddr", "ClearPassword", "ConfirmPassword", "SaveProfileButton"} // Get elements elements, err := webdriver.FindNamedElements(elist) if err != nil { t.Fatalf("FindNamedElements failed: %s", err) } var val string for k, v := range profExpected { val, err = elements[k].GetAttribute("value") if err != nil { t.Fatalf("Failed to get vlaue in %s: %s", k, err) } if val != v { t.Fatalf("Expected %s value \"%s\", got \"%s\"", k, v, val) } } }
func ExpectOnLoginPage(t *testing.T) { webdriver.WaitFor(5*time.Second, webdriver.ElementToAppear, "form[name=\"loginForm\"]") if webdriver.WaitForTimedOut { t.Fatalf("Expected to be on the login page, but timed out waiting for it") } // it may not be fully loaded yet ... }
// ExpectRegistrationSuccess gets call immediately after SubmitRegistration and verifies a successful registration func ExpectRegistrationSuccess(email string, t *testing.T) { webdriver.WaitFor(5*time.Second, webdriver.ElementToVanish, "div[class='selenium-flag']") msg, err := webdriver.FetchText("p[name='Message']") if err != nil { t.Fatalf("Failed to fetch the main message: %s", err) } expectMsg := fmt.Sprintf("Registration successful. Next step: Check your %s inbox and verify your email address.", email) if msg != expectMsg { t.Fatalf("Main message did not match expected, got %s", msg) } }
func doVerifyCase(t *testing.T, cur vCase) { url := fmt.Sprintf("https://plog.org:8004/#/verify/%s/token/%s", cur.email, cur.tok) err := webdriver.Drv.Get(url) if err != nil { t.Fatalf("Failed to load %s: %s\n", url, err) } webdriver.WaitFor(5*time.Second, webdriver.ElementToVanish, "div[class='selenium-flag']") actualMsg, err := webdriver.FetchText("p[name='Message']") if err != nil { t.Fatalf("Failed to fetch the main message: %s", err) } if actualMsg != cur.msg { t.Fatalf("Expected message \"%s\", got \"%s\"", cur.msg, actualMsg) } }
func ExpectProfileChangeSuccess(t *testing.T) { webdriver.WaitFor(5*time.Second, webdriver.ElementToVanish, "div[class='selenium-flag']") // Get elements elements, err := webdriver.FindNamedElements([]string{"Message", "UserId", "FirstName", "LastName", "EmailAddr", "TzName", "ClearPassword", "ConfirmPassword", "SaveProfileButton"}) if err != nil { t.Fatalf("FindNamedElements failed: %s", err) } msg, err := elements["Message"].Text() if err != nil { t.Fatalf("Could not get Message") } if msg != "Save successful" { t.Fatalf("Main message did not reflect success, got %s", msg) } }
// SubmitProfileChange fills in the form and presses the save button. It does not wait after the click. func SubmitProfileChange(profU UserProfile, t *testing.T) { webdriver.WaitFor(5*time.Second, webdriver.ElementToVanish, "div[class='selenium-flag']") // Get elements elements, err := webdriver.FindNamedElements([]string{"UserId", "FirstName", "LastName", "EmailAddr", "TzName", "ClearPassword", "ConfirmPassword", "SaveProfileButton"}) if err != nil { t.Fatalf("FindNamedElements failed: %s", err) } // Fill if profU.UserId != "" { elements["UserId"].Clear() elements["UserId"].SendKeys(profU.UserId) } if profU.FirstName != "" { elements["FirstName"].Clear() elements["FirstName"].SendKeys(profU.FirstName) } if profU.LastName != "" { elements["LastName"].Clear() elements["LastName"].SendKeys(profU.LastName) } if profU.EmailAddr != "" { elements["EmailAddr"].Clear() elements["EmailAddr"].SendKeys(profU.EmailAddr) } if profU.ClearPassword != "" { elements["ClearPassword"].Clear() elements["ClearPassword"].SendKeys(profU.ClearPassword) } if profU.ConfirmPassword != "" { elements["ConfirmPassword"].Clear() elements["ConfirmPassword"].SendKeys(profU.ConfirmPassword) } // Submit elements["SaveProfileButton"].Click() // NOT waiting here }
func Logout(t *testing.T) { ExpectSessionToken(t) err := webdriver.Drv.Get(LogoutPageUrl) if err != nil { t.Fatalf("Failed to load %s: %s\n", LogoutPageUrl, err) } webdriver.WaitFor(5*time.Second, webdriver.ElementToVanish, "div[class='selenium-flag']") sel := "[name=\"Logout\"]" LogoutLink, err := webdriver.Drv.FindElement(selenium.ByCSSSelector, sel) if err != nil { t.Fatalf("Failed to find element %s (%s)\n", sel, err) } LogoutLink.Click() // BUG ng-click with href="" fails under selenium ExpectOnLoginPage(t) ExpectNoSessionToken(t) }
func Test_Login_table(t *testing.T) { cases := []loginCase{ {"UserId", Login{"no such dude", "passwordpassword"}, "Authentication failed", "absent"}, {"UserId", Login{"wrong uid", userOne.ClearPassword}, "Authentication failed", "absent"}, {"UserId", Login{userOne.UserId, "wrong password"}, "Authentication failed", "absent"}, {"UserId", Login{userOne.UserId, userOne.ClearPassword}, "SKIP", "present"}, // if already logged in, automatic login will happen. So for testing should log out } for c := 0; c < len(cases); c++ { cur := cases[c] t.Logf("Case %d: UserIdentifier (%s)=%s ClearPassword=%s", c, cur.idTyp, cur.lin.UserIdentifier, cur.lin.ClearPassword) if len(cur.lin.ClearPassword) < 10 { t.Fatalf("Case is not valid since ClearPassword (%s) is too short") } GotoLogin(t) ExpectOnLoginPage(t) SubmitLogin(cur.lin, t) webdriver.WaitFor(5*time.Second, webdriver.ElementToVanish, "div[class='selenium-flag']") if cur.tokc == "absent" { // Login failure was expected msg, err := webdriver.FetchText("p[name='LoginMessage']") if err != nil { t.Fatalf("Failed to fetch the main message: %s", err) } if msg != "Authentication failed" { t.Errorf("Expected failure message, got \"%s\"", msg) } ExpectNoSessionToken(t) } else { ExpectSessionToken(t) } } }
// SubmitRegistration fills in the form and presses the register button. It does not wait after the click. func SubmitRegistration(regU RegisterUser, t *testing.T) { // Get and wait err := webdriver.Drv.Get(`https://plog.org:8004/#/register`) if err != nil { t.Fatalf("Failed to load page: %s", err) } webdriver.WaitFor(5*time.Second, webdriver.ElementToVanish, "div[class='selenium-flag']") // Check page title pageTitle := "ZM Plog" if title, err := webdriver.Drv.Title(); err == nil { if title != pageTitle { t.Fatalf("Title page, expected \"%s\" but actually got \"%s\"\n", pageTitle, title) } } else { t.Fatalf("Failed to get page title: %s", err) } // Get elements elements, err := webdriver.FindNamedElements([]string{"UserId", "FirstName", "LastName", "EmailAddr", "TzName", "ClearPassword", "ConfirmPassword", "RegisterButton"}) if err != nil { t.Fatalf("FindNamedElements failed: %s", err) } // Fill elements["UserId"].SendKeys(regU.UserId) elements["FirstName"].SendKeys(regU.FirstName) elements["LastName"].SendKeys(regU.LastName) elements["EmailAddr"].SendKeys(regU.EmailAddr) elements["ClearPassword"].SendKeys(regU.ClearPassword) elements["ConfirmPassword"].SendKeys(regU.ConfirmPassword) // Submit elements["RegisterButton"].Click() // NOT waiting here }
func Test_Logout(t *testing.T) { // happy path logout t.Logf("Case: happy path logout") Logout(t) // visit while logged out; expect to get login page // TODO add an Image URL pagesNeedingLogin := []string{"/#/album", "/#/album/2013-09%20September"} for p := 0; p < len(pagesNeedingLogin); p++ { page := fmt.Sprintf("https://plog.org:8004%s", pagesNeedingLogin[p]) t.Logf("Case: %s", pagesNeedingLogin[p]) err := webdriver.Drv.Get(page) if err != nil { t.Fatalf("Failed to load %s: %s\n", page, err) } webdriver.WaitFor(5*time.Second, webdriver.UrlIsCurrent, "https://plog.org:8004/#/login") if webdriver.WaitForTimedOut { t.Fatalf("Failed to land at expected URL") } msg, err := webdriver.FetchText("p[name='LoginMessage']") if err != nil { t.Fatalf("Failed to fetch the main message: %s", err) } if msg != "" { t.Errorf("Message was not blank: %s", msg) } result, err := webdriver.Drv.ExecuteScript("return localStorage['SessionToken']", nil) if err != nil { t.Fatalf("Failed to ExecuteScript: %s", err) } if result != nil { t.Errorf("Found a SessionToken in localStorage: %s", result) } } // automatic login when SessionToken is still valid: goto login, then autologin and end up on album page t.Logf("Case: Automatic login that works") GotoLogin(t) ExpectOnLoginPage(t) SubmitLogin(Login{userOne.UserId, userOne.ClearPassword}, t) webdriver.WaitFor(5*time.Second, webdriver.ElementToVanish, "div[class='selenium-flag']") ExpectSessionToken(t) // need to visit a page that needs login - the LogoutPageUrl page needs login err := webdriver.Drv.Get(LogoutPageUrl) if err != nil { t.Fatalf("Failed to load %s: %s\n", LogoutPageUrl, err) } webdriver.WaitFor(5*time.Second, webdriver.UrlIsCurrent, LogoutPageUrl) if webdriver.WaitForTimedOut { t.Fatalf("Failed to land at expected URL") } // automatic login when SessionToken is present but not valid t.Logf("Case: Automatic login with bad session token") /* This angular method does not work, need to use the document.cookie method :( js := ` var $injector = angular.injector(['ngCookies']); return $injector.invoke(function($cookies) { $cookies.SessionToken = '00000000-0000-0000-dead-beef00000000'; }); ` _, err = webdriver.Drv.ExecuteScript(js, nil) */ _, err = webdriver.Drv.ExecuteScript(`document.cookie = 'SessionToken=00000000-0000-0000-dead-beef00000000';`, nil) if err != nil { t.Fatalf("Failed to ExecuteScript: %s", err) } // need to visit a page that needs login - the LogoutPageUrl page needs login err = webdriver.Drv.Get(LogoutPageUrl) if err != nil { t.Fatalf("Failed to load %s: %s\n", LogoutPageUrl, err) } ExpectOnLoginPage(t) msg, err := webdriver.FetchText("p[name='LoginMessage']") if err != nil { t.Fatalf("Failed to fetch the login message: %s", err) } if msg != "" { // automatic logins don't produce "Authentication failed" messages t.Errorf("Expected LoginMessage to be blank, got \"%s\"", msg) } // TODO periodic re-login to extend session : Problem is how to test this without waiting 5 minutes // TODO login, visit page that calls loggedIn() - delete session in background - visit page that calls loggedIn() + expect it to return true - wait for background login attempt - visit page that calles loggedIn() + expect it to return false }