Ask a Question

Calling method of user defined class in keyword test

Occasional Contributor

Calling method of user defined class in keyword test

Hi *,


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




- 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):
    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 --     
  def InputUsername(self):
    return self.get("InputUsername")

  #-- Convenience --                 
  def set_username(self, 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?






Frequent Contributor

Hi @ucuber,

Have you tried using Run Script Routine which runs the script dirctly from Keyword test.


hope this helps you.

With Regards
Vallalarasu Pandiyan
Occasional Contributor



yes, tried it. But the class definition will not be shown. It only shows methods.

If I build a factory method, this will be shown, but I can not append the final method call (factory(page).method("Blah"))


The only idea of a solution is at the moment to define a project variable of the user defined type and then call the method by using this variable


1. set project variable from Class() as "o", type "object"

2. Code Snippet: Project.Variables.o.method("blah")


It is not so very elegant, perhaps I will fall back to the wrapper ... as a colleague once said: there is no problem that can not be solved with another level of indirection aside from problems of indirection 🙂


Kind Regards



Esteemed Contributor

Duplicated post.  Please avoid asking the same question twice.

Robert Martin
[Hall of Fame]
Please consider giving a Kudo if I write good stuff

Why automate?  I do automated testing because there's only so much a human being can do and remain healthy.  Sleep is a requirement.  So, while people sleep, automation that I create does what I've described above in order to make sure that nothing gets past the final defense of the testing group.
I love good food, good books, good friends, and good fun.

Mysterious Gremlin Master
Vegas Thrill Rider
Extensions available
Occasional Contributor

Sorry, posted it twice, because first try ended up rejecting it as spam.

Showing results for 
Search instead for 
Did you mean: