Forum Discussion

jonathon's avatar
jonathon
Contributor
5 years ago

Python BDD modules

Hi

 

Just getting started with TestComplete and BDD. I'm experienced with Python BDD (Behave) with Selenium. Just a little unsure how to work with python modules. I can't seem to import my classes currently. Wondered if anyone could point me in the right direction. I also wasn't sure how to create a .py file.

 

I included a file named "test", with no extension inside my Scripts folder. But not sure how to import that class into my step definitions?

 

  • Ok, I managed it like this:

     

    import login as login_object

     

    login = login_object.Login.get_instance()

     

     

    My Class is like this though:

     

    class Login:
        instance = None
    
        @classmethod
        def get_instance(cls):
            if cls.instance is None:
                cls.instance = Login()
            return cls.instance

    login = Login.get_instance()

     

    So looks a bit ugly, but does work. But if anyone has a better solution that would be cool

  • Ok, I managed it like this:

     

    import login as login_object

     

    login = login_object.Login.get_instance()

     

     

    My Class is like this though:

     

    class Login:
        instance = None
    
        @classmethod
        def get_instance(cls):
            if cls.instance is None:
                cls.instance = Login()
            return cls.instance

    login = Login.get_instance()

     

    So looks a bit ugly, but does work. But if anyone has a better solution that would be cool

    • Mr_Bro's avatar
      Mr_Bro
      Frequent Contributor

      Hi jonathon ,

       

      firstly when you add a file in python project under Script folder, it will automatically create a ".py" file.

      for importing classes from one file to another file, you can refer here (call procedures or variables declared in another unit) , 

      in your case if you want to use the definitions or variables declared n "test" file in any other file (sample),

      you have to Add the following to the beginning of the "sample" file

       

      import test

      and after that you can refer to the variables in "sample" script which are declared in "test" file like showen in the below code snippet.

      unser "test.py"

       

       

      UserName = 'sampleUsername'
      Password = 'samplePassword'

      under "sample.py"

       

       

      import test
      
      def sampleTest():
      uName = test.sampleUsername
      pwd = test.Password
      Log.Message(uName + pwd)
      ##### Output will be 'sampleUsernamesamplePassword'