Wednesday 1 February 2012

NUnit with TeamCity, Working with IFrames When Writing System Tests

System tests are structured differently than normal unit tests in the sense that we won't have specific Arrange/Act/Verify sections. System tests have to test whether the whole process works correctly and can contain many steps.

[Test]
public void LoginRegisterPage_WithValidEmailAddress_ShouldSendReminderEmail()

{
// go to loginregister url
this.GoToUrl(LoginRegisterPageUrl);
this.ClickElementIfExists(By.LinkText("Accept"));

// click the forgotten password
this.ClickElementByLinkText("Forgotten your password?");

// enter your email address
this.TypeInTextBox("PasswordResetRequest1_PasswordResetRequest_Username", TestEmailAddress);
DateTime dateTimeBeforeSubmit = DateTime.Now;

// click submit
this.ClickElementById("PasswordResetRequest1_PasswordResetRequest_PasswordResetRequestSubmit");

// assert Thank You
Assert.IsTrue(this.FireFoxWebDriver.Url.Contains("ThankYou"));

// wait for 2sec
Thread.Sleep(2000);

// Find the email log from database
string urlForResettingPassword = getPasswordReminderUrl(dateTimeBeforeSubmit);

// navigate to the url
this.GoToUrl(urlForResettingPassword);

string newPassword = this.generateRandomPassword();
// enter a new password

this.TypeInTextBox("PasswordReset1_PasswordReset_Password", newPassword);
this.TypeInTextBox("PasswordReset1_PasswordReset_PasswordConfirmation", newPassword);

// click submit
this.ClickElementById("PasswordReset1_PasswordReset_PasswordResetSubmit");

// Thank you exists
var pageTitle = this.FireFoxWebDriver.FindElement(By.XPath(@"//div/h1"));

Assert.AreEqual("Thank you", pageTitle.Text);

// go to the login page

this.GoToUrl(LoginRegisterPageUrl);

// login using the new password
this.TypeInTextBox("UserLogin1_UserLogin_Username", TestEmailAddress);
this.TypeInTextBox("UserLogin1_UserLogin_Password", newPassword);
this.ClickElementById("UserLogin1_UserLogin_LoginSubmit");

// verify whether the login was successful
Assert.IsNotNull(this.GetElementIfExists(By.Id("UserProfile1_UserProfile_UserProfileSubmit")));
}

No comments: