pythontestcomplete

TestComplete with Python - Python runtime error. NameError: name 'page' is not defined


I am new to python and test complete just trying to automate a sample webpage but stuck with below error. I have searched and know that I am surely making mistakes in defining the variables. have recorded the below script and it runs fine if I keep it one function but when I keep it in separate functions(to login into page have kept the code in Login() function and calling it in Test1() ) it fails though it login into the page .


global browser,page
def Login():
  Browsers.Item[btChrome].Navigate("http://secure.smartbearsoftware.com/samples/testcomplete11/WebOrders/login.aspx")
  browser=Aliases.browser
  page = browser.pageWebOrdersLogin
  form = page.formAspnetform
  form.textboxUsername.Keys("[Enter]")
  page.Wait()
  textbox = form.textboxUsername2
  textbox.SetText("Tester")
  textbox.Keys("[Tab]")
  passwordBox = form.passwordboxPassword
  passwordBox.SetText(Project.Variables.Password1)
  passwordBox.Keys("[Enter]")  
  page = browser.pageDefault
  page.Wait()
  

def Test1():
  global page
  Login()    
  page.formAspnetform.link.Click()
  page = browser.pageProcess
  page.Wait()
  form = page.formAspnetform
  form.selectProduct.ClickItem("FamilyAlbum")
  textbox = form.textboxQuantity
  textbox.SetText("40")
  form.submitbuttonCalculate.ClickButton()
  textbox = form.textboxCustomerName
  textbox.SetText("nitin")
  textbox.Keys("[Tab]")
  textbox = form.textboxStreet
  textbox.SetText("marvel")
  textbox.Keys("[Tab]")
  textbox = form.textboxCity
  textbox.SetText("pune")
  textbox.Keys("[Tab]")
  textbox = form.textboxState
  textbox.SetText("maharashta")
  textbox.Keys("[Tab]")
  form.textboxZip.SetText("411014")
  cell = form.cell
  cell.radiobuttonVisa.ClickButton()
  textbox = form.textboxCardNr
  textbox.SetText("411882781991")
  textbox = form.textboxExpireDateMmYy
  textbox.SetText("01/23")
  form.linkInsertbutton.Click()
  page.Wait()
  textNode = page.textnode
  aqObject.CheckProperty(textNode, "contentText", cmpEqual, "New order has been successfully added.")
  page.link.Click()
  browser.pageDefault2.Wait()

Error: Python runtime error.

NameError: name 'page' is not defined

Error location: Unit: "WebTesting\WebTesting\Script\WebTest" Line: 22 Column: 1.


Solution

  • The global declaration for page needs to be repeated inside the def Login; but a much better design is to pass non-global variables between these functions. Either

    def login():
        browser = ...
        page = ...
        ...
        return browser, page
    
    def test1():
        browser, page = login()
        ...
    

    or perhaps conversely have the caller define and pass these in;

    def login(browser, page):
        ...
    
    def test1()
        browser = ...
        page = ...
        login(browser, page)
        ...
    

    Your current design calls for the former, but both patterns are common, and the second is perhaps more logical. Generally, try to define variables in the context where they are going to be used, and then pass them down into other functions as necessary. As a rule of thumb, try to make your variables as narrowly scoped and short-lived as possible.

    Notice also how we usually do not capitalize function names in Python.