Forum Discussion

efuller's avatar
efuller
Occasional Contributor
7 years ago

TestComplete Python Scripting

Spoiler
 

Hello Everyone, 

 

I came from the world of C# and Selenium automated testing and I'm trying to find my ground with scripting in TestComplete. 

 

In C#, I can create extension methods to shorten code and make things much more readable and I'm trying to translate that into python scripts for TestComplete. Here is my example:

 

Method : ClickOn

 

browser = Sys.Browser()
page = browser.Page()

def ClickOn(button): obj = page.FindChildByXpath(button, True) if (GetVarType(obj) != varNull): obj.Click() else: Log.Message('Unable to locate button') return button

LookForObject Method:

def LookForObject(object):
  obj = page.FindChildByXpath(object)
  if (GetVarType(obj)) != varNull:
    Log.Message("Verified Object Exists")
  else:
    Log.Message("Object is not present on page")
  return object

 

NameMapping file:

 

SignInPage_SignInButton = "//[@TesterXPath]";

Actual Script:

import Actions
import SiteCore_NameMapping

def U010_Navigate_to_SiteCore_Login_Page():
  #Clicks on the the Sign In button in the Navigation Bar
  Actions.ClickOn(NavigationBar_SignInButton)
  #Validates navigation was successful by verifying fields and buttons exist
  Actions.LookForObject(SignInPage_SignInButton)
  Actions.LookForObject(SignInPage_SignInEmailField)
  Actions.LookForObject(SignInPage_SignInPasswordField)

My problem may be two-fold; I'm still getting familiar with Python syntax, and I'm still learning how TestComplete will be able to interpret my scripting (Maybe threefold, I'm very stuck in my ways with how simple scripts can look with C# :smileyhappy: )

 

Anyway, I'm receiving a runtime error: Python runtime error:

 

 

NameError: name 'NavigationBar_SignInButton' is not defined
 

 

Can someone please shed some light as to why I'm receiving this error? As you can see I did define the variable but maybe not in the correct/needed format?

5 Replies

  • AlexKaras's avatar
    AlexKaras
    Champion Level 3

    Hi,

     

    Out of question scope, but anyway...

    > I came from the world of C# and Selenium automated testing and I'm trying to find my ground with scripting in TestComplete.

     

    While TestComplete supports XPath search, I would recommend not to use it and move to complimentary .FindXXX() methods (.FindChild, .FindAllChildren, .WaitAliasChild, .WaitPanel, ...).

    Reasons:
    -- They are faster than XPath search;

    -- They always return TestComplete objects while XPath search can return native DOM object and this will complicate your test code;

    -- They support search using regular expressions which is pretty useful and handy sometimes;

    -- Their arguments are of same clarity as XPath expression.

     

    XPath in TestComplete is a last resort means when tested application is written so poorly (from the internal design point of view) that nothing else does not work.

  • kevin_kapell's avatar
    kevin_kapell
    Frequent Contributor

    Why are you using Python when TC can consume C#? Use what you are comfortable with unless your department requires you to use Python.

    • efuller's avatar
      efuller
      Occasional Contributor

      Hello Kevin, 

       

      The reason is because TC uses C# Script, and not the actual C# language. The syntax is different between the two which would take the same amount of effort to understand and adapt to the new syntax, just like the efforts now with Python. I can be mistaken by saying this, but I believe C# Script is more akin to JScript syntax.

       

      Python was chosen for my company because Python is used widely throughout our normal day-to-day scripting. Also the fact that its an easy language to grasp.

  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor

    The problem is in this line:

      Actions.ClickOn(NavigationBar_SignInButton)

    According to what you posted, your custom object identification doesn't have NavigationBar_SignInButton defined, hence the error...

     

     

    • baxatob's avatar
      baxatob
      Community Hero

      Agreed with tristaanogre

       

       

      You should define NavigationBar_SignInButton somewhere inside the module or explicitly import it from another module:

       

      from SiteCore_NameMapping import NavigationBar_SignInButton