Forum Discussion

jose_pjoseph's avatar
jose_pjoseph
Contributor
7 years ago
Solved

How to achieve before() and after() functionality in Test Complete

Is there an equivalent, or is there a way to achieve, before(), after(), beforeEach(), and afterEach() functionality available @ https://mochajs.org/#hooks?

I want to execute my tests in the sequence below:

 

    Before:

        OpenBrowser()
        Login()
    Tests:
        Project1:
                Test1
                Test2
        Project2:
                Test1
        Project3:
                Test1
                Test2
                Test3
    After:
        Logout()
        CloseBrowser()

 

Update: 

I looked at Events > Test Engine Events > OnStartTest/OnStopTest, which is equivalent to beforeEach() and afterEach(). But could not find anything which is similar to before() and after() - execute steps once before all tests and once after all tests.

  • AlexKaras's avatar
    AlexKaras
    7 years ago

    Hi,

     

    As it was stated by Robert, TestComplete does not have means to execute some code at the start and end of the whole test run. You may try to emulate this in code. Consider this approach:

    -- I assume that you are testing web application;

    -- For web applications, the first step for almost any test is to get the reference to the tested web page via Page object provided by TestComplete;

    -- You may implement a function in your test code that you will call from any place where you need to get a reference to the tested page. This function will try to find the page and return it if found. Otherwise it will resort to the browser start and site login sequence.

11 Replies

  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor

    It depends somewhat as to how you have built your project in TestComplete.  There isn't a "on start project" or "on stop project" handler.  What I do in these cases is add Test Items in the project to perform those tasks when the project starts up and when it finishes.  These aren't something you need to control as events, just code/tests that need to be run.

    • jose_pjoseph's avatar
      jose_pjoseph
      Contributor

      tristaanogre, thanks for the reply.

      OpenBrowser() and CloseBrowser() mentioned above are script routines, and Login() and Logout() are test items which call those script routines in addition to performing the login and logout tasks. Test Complete structure is as below, currently:

       

      ProjectSuite:
          Project1:
                  Test1
                  Test2
          Project2:
                  Test1
          ..
          ....
          Project9:
                  <Tests>

       

      Each test needs to be executed while logged in. As of now, we are in the initial stages and have few tests and I am calling Login() and Logout() from each test. But I do not want to do that each time as that will add a lot of overhead when we have hundreds of tests. I am looking to have Login() called once at the beginning and Logout() called once at the end whether I am executing a single test, all the tests in the project or all the tests in the project suite.

       

      You mentioned "perform those tasks when the project starts up and when it finishes" in your reply. Can you please explain a bit more about how I can do that? I think that is basically what I am looking for, execute Login() when the project/project suite starts and execute Logout() when the project/project suite finishes.

    • jose_pjoseph's avatar
      jose_pjoseph
      Contributor

      @tristaanogre, thanks for the reply.

      OpenBrowser() and CloseBrowser() mentioned above are script routines, and Login() and Logout() are test items which call those script routines in addition to performing the login and logout tasks. Test Complete structure is as below, currently:

       

      ProjectSuite:
          Project1:
                  Test1
                  Test2
          Project2:
                  Test1
          ..
          ....
          Project9:
                  <Tests>

       

      Each test needs to be executed while logged in. As of now, we are in the initial stages and have few tests and I am calling Login() and Logout() from each test. But I do not want to do that each time as that will add a lot of overhead when we have hundreds of tests. I am looking to have Login() called once at the beginning and Logout() called once at the end whether I am executing a single test, all the tests in the project or all the tests in the project suite.

       

      You mentioned "perform those tasks when the project starts up and when it finishes" in your reply. Can you please explain a bit more about how I can do that? I think that is basically what I am looking for, execute Login() when the project/project suite starts and execute Logout() when the project/project suite finishes.

  • Looks like my posts are getting deleted. Tried posting a reply twice and can't find it now.

  • Trying a third time.

     

    tristaanogre, thanks for the reply.

    OpenBrowser() and CloseBrowser() mentioned above are script routines, and Login() and Logout() are test items which call those script routines in addition to performing the login and logout tasks. Test Complete structure is as below, currently:

     

        ProjectSuite:
            Project1:
                    Test1
                    Test2
            Project2:
                    Test1
            ..
            ....
            Project9:
                <Tests>

     

    Each test needs to be executed while logged in. As of now, we are in the initial stages and have few tests and I am calling Login() and Logout() from each test. But I do not want to do that each time as that will add a lot of overhead when we have hundreds of tests. I am looking to have Login() called once at the beginning and Logout() called once at the end whether I am executing a single test, all the tests in the project or all the tests in the project suite.

     

    You mentioned "perform those tasks when the project starts up and when it finishes" in your reply. Can you please explain a bit more about how I can do that? I think that is basically what I am looking for, execute Login() when the project/project suite starts and execute Logout() when the project/project suite finishes.

    • tristaanogre's avatar
      tristaanogre
      Esteemed Contributor

      Basically, you're talking about initializing and finalizaing a project.  To this end, what I've done in the past (and seems to be a general best practice) is that you have Test Items for these routines so that your structure looks like this

       

          ProjectSuite:
              Project1:

                      Initialization
                      Test1
                      Test2

                      Finalization
              Project2:

                      Initialization
                      Test1

                      Finalization
              ..
              ....
              Project9:
                  <Tests>

      • jose_pjoseph's avatar
        jose_pjoseph
        Contributor

        This would help only at the project level. I am thinking of a scenario where I am writing a new test or debugging an existing test and want to run only that one test multiple times. I assume this would not help in those cases?

         

        As I mentioned above, I am looking to have Login() called once at the beginning and Logout() called once at the end whether I am executing:

        • a single test
        • all the tests in the project
        • all the tests in the project suite