Skip to content

keywords collecting with Hybrid library #86

Description

@mfo7mfo7mfo7

Hi @jnhyperion Thank you again for your support.

Please correct me if I'm wrong.
Robot Hybrid library is a common way to implement keyword. https://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html#hybrid-library-api
I found keywords are not parsed correctly, or checked improper way.

example1

BuiltIn Telnet library

image image image

Get Keyword Names is not a keyword but it checked as valid one.

example 2

i write a demo code for test hybrid library keyword collection.
image

  • Hybrid library are collecting keywords by the function: get_keyword_names()
    • Not A Keyword should not be parsed as keyword for example.
    • Run, Get Name, Sleep are keywords but don't check well.

Here are the demo code for your reference. Thank you. Cheers!

# demo.robot

*** Settings ***
Library    demo.Zoo    AS     Zoo

*** Test Cases ***
Test Zoo
    Zoo.Run
    ${name}=     Zoo.Get Name
    Log To Console    ${name}

    Zoo.Switch    dog
    ${name}=     Zoo.Get Name
    Log To Console    ${name}

    Zoo.Run
    Zoo.Sleep

# demo.py

class Animal:
    def __init__(self, name, speed):
        """Initialize an animal with a name and speed."""
        self.name = name
        self.speed = speed

    def get_name(self):
        """Returns the name of the animal."""
        return self.name

    def run(self):
        """Simulates the animal running."""
        return f"{self.name} runs at {self.speed} km/h."

    def sleep(self):
        """Simulates the animal sleeping."""
        return f"{self.name} is sleeping."

import inspect

class Zoo:
    def __init__(self):
        """Initialize the Zoo with a default animal and animal options."""
        self.animals = {
            "cat": Animal(name="Cat", speed=30),
            "dog": Animal(name="Dog", speed=40),
            "lion": Animal(name="Lion", speed=50),
            "elephant": Animal(name="Elephant", speed=25)
        }
        self.current_animal = self.animals["cat"]

    def switch(self, animal_name):
        """Switch the current animal to another animal in the zoo."""
        if animal_name in self.animals:
            self.current_animal = self.animals[animal_name]
        else:
            raise ValueError(f"Animal '{animal_name}' not found in the zoo.")

    def not_a_keyword(self):
        pass

    def get_keyword_names(self):
        """Returns the names of all keywords in this library and the current animal."""
        not_keyword_list = ['not_a_keyword', 'get_keyword_names']


        zoo_methods = [method for method in dir(self) if callable(getattr(self, method)) and not method.startswith("_")]
        for k in not_keyword_list:
            try:
                zoo_methods.remove(k)
            except ValueError:
                pass  # do nothing!

        animal_methods = [method for method in dir(self.current_animal) if callable(getattr(self.current_animal, method)) and not method.startswith("_")]
        publish_kws = zoo_methods + animal_methods
        print(f'publish_kws: {publish_kws}')
        return publish_kws

    def __getattr__(self, name):
        """Delegate attribute access to the current animal if not found in Zoo."""
        if hasattr(self.current_animal, name):
            return getattr(self.current_animal, name)
        raise AttributeError(f"'{self.__class__.__name__}' object has no attribute '{name}'")

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions