472,127 Members | 2,102 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,127 software developers and data experts.

NUnitASP testing for asp:Login control

I've been searching for a way to test this control with my NUnitASP tests for awhile now and have yet to come up with anything. I have all of my tests written, but until I can get past the initial login page, all my tests fail.

Here is some of the login page code:

Expand|Select|Wrap|Line Numbers
  1. <asp:Login ID="Login1" runat="server" Height="134px" 
  2.         OnAuthenticate="Login1_Authenticate" 
  3.         OnLoginError="OnLoginError" Width="336px" CreateUserText="Create a new account" CreateUserUrl="CreateUser.aspx" DestinationPageUrl="~/Main.aspx" MembershipProvider="SqlProvider" PasswordRecoveryText="Retrieve Password" PasswordRecoveryUrl="PasswordRecovery.aspx">
  4.         </asp:Login>
On my NUnit tests, this is the code I have written so far...

Expand|Select|Wrap|Line Numbers
  1. Browser.GetPage(MyPageURL);
  2. // This is where I need to know the control tester for my login control
  3. // in order to use it instead of CurrentWebForm for the following
  4. TextBoxTester user = new TextBoxTester("UserName", CurrentWebForm);
  5. TextBoxTester password = new TextBoxTester("Password", CurrentWebForm);
  6. ButtonTester logon = new ButtonTester("LoginButton", CurrentWebForm);
  7.  
  8. user.Text = "validusername";
  9. password.Text = "validpassword";
  10. logon.Click();
I put a writeline into my unit test right after the getpage call and it was returning the correct page, but the error message given when I run the unit test is:
NUnit.Extensions.Asp.HtmlTag+ElementNotVisibleExce ption : Couldn't find 'UserName' on ' '.

Thanks,
Heather
Dec 22 '06 #1
3 6217
For future reference if someone else runs into this problem, here was the hack solution I came up with:

In the web.config file for my main program, I commented out the line
Expand|Select|Wrap|Line Numbers
  1. <deny users="?"/>
just for the duration of when I want to run my unit tests. This way, when I want to release from my localhost to my server, I can just take out the comments and everything still has the security that I needed for the main page.

Also, on all of my URLs for the Browser.GetPage() calls, I made sure to set them to read to my http://localhost:port#/....aspx instead of calling to the main server.

After doing that, all of the unit tests passed. This probably isn't the best solution to the problem, but it does give you a single point of control when you want to run your unit tests, just make sure to comment out the deny users line in the config file, but don't forget to take out the commenting before releasing it back to the server!

--This was simply the solution I came up with, and I posted it here should anyone else arrive at the same problem and look for a solution--

Heather

I've been searching for a way to test this control with my NUnitASP tests for awhile now and have yet to come up with anything. I have all of my tests written, but until I can get past the initial login page, all my tests fail.

Here is some of the login page code:

Expand|Select|Wrap|Line Numbers
  1. <asp:Login ID="Login1" runat="server" Height="134px" 
  2.         OnAuthenticate="Login1_Authenticate" 
  3.         OnLoginError="OnLoginError" Width="336px" CreateUserText="Create a new account" CreateUserUrl="CreateUser.aspx" DestinationPageUrl="~/Main.aspx" MembershipProvider="SqlProvider" PasswordRecoveryText="Retrieve Password" PasswordRecoveryUrl="PasswordRecovery.aspx">
  4.         </asp:Login>
On my NUnit tests, this is the code I have written so far...

Expand|Select|Wrap|Line Numbers
  1. Browser.GetPage(MyPageURL);
  2. // This is where I need to know the control tester for my login control
  3. // in order to use it instead of CurrentWebForm for the following
  4. TextBoxTester user = new TextBoxTester("UserName", CurrentWebForm);
  5. TextBoxTester password = new TextBoxTester("Password", CurrentWebForm);
  6. ButtonTester logon = new ButtonTester("LoginButton", CurrentWebForm);
  7.  
  8. user.Text = "validusername";
  9. password.Text = "validpassword";
  10. logon.Click();
I put a writeline into my unit test right after the getpage call and it was returning the correct page, but the error message given when I run the unit test is:
NUnit.Extensions.Asp.HtmlTag+ElementNotVisibleExce ption : Couldn't find 'UserName' on ' '.

Thanks,
Heather
Dec 28 '06 #2
Not sure if anybody will see this, but here is a better solution...

The problem is that NUnitAsp "Tester" objects look for a control by it's HtmlID. And ASP builds control IDs by appending all parent container names with an '_'. So, the ASP.NET Login control has the following controls:

UserName
Password
LoginButton

But, the login control itself is called "Login1". So, the UserName control's htmlid would be "Login1_UserName". If you have a masterpage, webpart, or anything else, you will also need to append those names. Here is a sample of the code I am using to test one of my login controls:

Expand|Select|Wrap|Line Numbers
  1. Browser.GetPage(AppPath + "Default.aspx"); //AppPath previously defined
  2. UserControlTester master = new UserControlTester("ctl00", CurrentWebForm); // master page
  3. UserControlTester webPart = new UserControlTester("wpMgr", master); // web part manager
  4. UserControlTester panel = new UserControlTester("gwppanLogin", webPart); // web part container
  5. UserControlTester login = new UserControlTester("Login1", panel); // Login control
  6. TextBoxTester txtUserName = new TextBoxTester("UserName", login);
  7. TextBoxTester txtPassword = new TextBoxTester("Password", login);
  8. ButtonTester btnLogin = new ButtonTester("LoginButton", login);
  9.  
  10. AssertVisibility(txtUserName, true);
  11. AssertVisibility(txtPassword, true);
  12. AssertVisibility(btnLogin, true);
  13.  
  14. txtUserName.Text = "username";
  15. txtPassword.Text = "password";
  16. btnLogin.Click();
Alternatively, you can pull up the source code, and find the full id of the control you want to grab. (ex. "ctl00_wpMgr_gwppanLogin_Login1_UserName").
Jul 11 '07 #3
Hi, there everyone. Currently I have just started working on NUnit ASP. For the past two weeks I have been working on it.

The problem I am facing now is with the visibility of the controls in NUnit.

When I tried to run the test case for validator testing, i was getting an webassertionexception where unexpectedly not visible error was coming out for all the asp controls i used. I found out that all the controls were nested in ASP:Content . Can you suggest me the way to include asp:content in unit testing in NUnit.
Oct 23 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by R.A.M. | last post: by
reply views Thread by R.A.M. | last post: by
1 post views Thread by Formentz | last post: by
3 posts views Thread by =?Utf-8?B?RHVrZSAoQU4yNDcp?= | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.