Forum Discussion

ucuber's avatar
ucuber
Occasional Contributor
6 years ago
Solved

How to call methods of user defined objects in keyword test?

Hi *,

 

maybe this was asked before, but I can't find a solution.

 

Given: 

 

- language: python

- class definition:

  remark: I prefer identifying page elements with css-selectors because the name mapping tree seems to be to inconvenient to me, especially when there are still coming layout changes. Coming from angular there are too much panels, you know?

class AppLogin(WebElement):
  def __repr__(self):
    return "{}()".format(self.__class__.__name__)
  
  def __str__(self):
    return "{}".format(self._selectors)
    
  def __init__(self, page):
    super().__init__(page)
    self._selectors = dict(
      InputUsername=r'app-login input[type=text]',
      InputPassword=r'app-login input[type=password]',
      MessageArea=r'app-login p',
      ButtonLogin=r'app-login button[type=submit]',    
    )
  
  #-- API --     
  @property
  def InputUsername(self):
    return self.get("InputUsername")
  
  ...

  #-- Convenience --                 
  def set_username(self, text):
    self.InputUsername.Click()
    self.InputUsername.SetText(text)
    return self

  ...

- what I want to do:

 

  in a keywordtest I would like to define a step, where the object of type AppLogin is instantiated and the method set_username called: AppLogin(page).set_username("blah"). I do not want to write a wrapper function around it, because this adds another level of indirection, what I do not think is a good solution.

 

If someone of you has an idea to call the method directly as shown above in the keyword test?

 

Regards

 

Ulrich

 

  • Short answer: you can't.  Keyword tests do not have access to the objects declared in script code without having some sort of instance or wrapper around it.  You can call a Script Routine from a keyword test but the class you have created is not available to the keyword test engine.  Your best solution is to create a wrapper function around that class so that you can call it from the keyword test.

1 Reply

  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor

    Short answer: you can't.  Keyword tests do not have access to the objects declared in script code without having some sort of instance or wrapper around it.  You can call a Script Routine from a keyword test but the class you have created is not available to the keyword test engine.  Your best solution is to create a wrapper function around that class so that you can call it from the keyword test.