Best way to handle a Login

2 posts, 0 answers
  1. Steve
    Steve avatar
    1885 posts
    Member since:
    Dec 2008

    Posted 17 Nov 2010 Link to this post

    How do you recommend handling testing pages behind a login?

    ...should the first part of the test hit the logout, then login
    ...If statement for the Login (so if it sees it, it logs in)
    ...Seperate login .aii, then link it in with one of the above scenarios
  2. Stoich
    Stoich avatar
    1 posts

    Posted 19 Nov 2010 Link to this post

    Hello Steve,

        I'll explain what we consider to be Best Practices when it comes to handling Login:

    In the context of Automated testing modularization is a good thing. You should try to break down your test into different modules each tasked with handling a specific component - long tests aren't best practice.
    More specifically on the Login: you should put all the steps associated with the Login in one test and then add this test as a step where ever it is needed (i.e. in other tests that need to login to somewhere). This approach lets you reuse the Login module and also makes it easier to establish where and why a failure in your test occured. The only time you should consider not having a separate step for login is when you can handle the login in a single step: for instance a default Windows Security Login.

    In your Login subtest: 
        Before we login, we need to be sure we're logged out first. So we have to check for signs showing we're logged in and log out if necessary . For example: you navigate to the page you need to log in to. If a link containing the text "Log out" is present on the page you know you must be logged in right now. 

    Unfortunately you will need to write the code that checks this yourself. I've  

    I've attached a small sample test. In it I log in to my Telerik account. This is how we check whether I'm logged in:

    if (Find.ByExpression<HtmlInputSubmit>("Value=Log Out") != null) {
       // Pages.DeveloperProductivityTeam.YourAccountTmUsercontrolsPublicClientnetLoginnameandstatusAscx1BtnLogoutSubmit != null) {
    if (Find.ByExpression<HtmlInputSubmit>("Value=Log Out") != null) {
     Pages.DeveloperProductivityTeam.YourAccountTmUsercontrolsPublicClientnetLoginnameandstatusAscx1BtnLogoutSubmit != null) {

    Basically, first we test whether an element labeled "Log out" is present on the page. In your case the element indicating you're logged in may be different but the basic idea is the same. We know the element exists if it's Find Logic (in our case Find.ByExpression<HtmlInputSubmit>("Value=Log Out")  ) returns something (as opposed to null). If it exists you have to hit the logout button. First check whether this button is present (Find expression !=null) and if it is - press it. You're now ready to login.

    Having this custom coded logout step at the begining of your Login subtest will save you a lot of trouble because attempting to login to a page you're already logged into will result in a failure.

    Please take a look at the attached test which handles login into a Telerik account. It consists of "MainTest" and "LoginBestPractices" which is the Login substep. It "LoginBestPractices" don't forget  to chage the e-mail and password used to your own (steps 6 and 8 respectively). 

    I hope this answer you question - don't hesitate to contact us if there's anything else!

    Kind regards,
    the Telerik team
    See What’s New in WebUI Test Studio’s Q3 Release – Friday, November 19th, 2010 at 11am EST – Register Today!
Back to Top