Pass the Browser Instance to CustomLibrary

Hello everybody,

I’m new to Robot Framework and Keyword based programming.

I got the Following Code files:

Test.robot

Summary

*** Settings ***

Documentation RobotFramework-testen
Metadata Author Tobias Marx
Metadata Version 1.1
Force Tags IPRL-Testing
Suite Setup Open Browser \ ${BROWSER}
Suite Teardown Close Browser
Library SeleniumLibrary
Library OperatingSystem
Resource …/Resources/locators.resource
Resource …/Resources/methods.resource

*** Keywords ***

*** Variables ***
${Umgebung} *****
${URL} *****
${BROWSER} chrome
${USERNAME} *****
${PASSWORD} *****
${OUTPUT_DIR} /log/

*** Test Cases ***
Test
Navigate and Login ${URL} ${USERNAME_TEXTAREA} ${USERNAME} ${PASSWORD_TEXTAREA} ${PASSWORD} ${ANMELDEN_BUTTON}

Einfach Weiter
Click Element ${AKTUELLE_AUSSCHR_LINK}
Sleep 1
Click Element ${AUSSCHR_VERW_LINK}
Sleep 1
Click Element ${PRODUKTART_DROPDOWN}
Sleep 1
Click Element ${PRODUKTART_DD_SRL}
Sleep 2
Search Open Run
Sleep 5

Methods.resource

Summary

*** Settings ***
Library OperatingSystem
Library SeleniumLibrary
Library Collections
Library CustomLibrary.py

*** Keywords ***

Search Open Run
Find And Click Link

CustomLibrary.py

Summary

from SeleniumLibrary import SeleniumLibrary
from SeleniumLibrary.base import keyword, LibraryComponent
from SeleniumLibrary.keywords import BrowserManagementKeywords
from robot.libraries.BuiltIn import BuiltIn
from selenium.webdriver.common.by import By

def find_and_click_link():
flag = False
link = None
seleniumlib = BuiltIn().get_library_instance(“SeleniumLibrary”)

# Find the table
table = seleniumlib.find_element(By.TAG_NAME, "table")

# Get all rows in the table
rows = table.find_elements(By.TAG_NAME, "tr")

for row in rows:
    # Get the cells in each row
    cells = row.find_elements(By.TAG_NAME, "td")

    # Iterate over the cells
    for cell in cells:
        if flag:
            cell.find_element(By.TAG_NAME, "a").click()
            break
        elif cell.text == "Warte auf Vergabevorschlag":
            flag = True

    if flag:
        flag = False
        break

My Problem is, i need to pass the browser Instance i started in the Test.robot file to my CustomLibrary.py
But i always get errors like this:
ValueError: Parent must be Selenium WebElement but it was <class ‘str’>.

I don’t think that is possible:

Passing a custom object between Robot files or between a Robot file and custom library code (in python, Java, remote library, etc.). Such functionality and support I believe is not defined nor implemented in the Robot Framework infrastructure.

Instead, you may have to workaround the limitation and wrap the generic selenium library code in your robot to be within custom library and then just call that from robot file. So thus your custom library contains some more generic boilerplate setup/teardown functions.

And if you ever have to pass a reference of the browser (or rather the WebDriver) between robot file and code library, then your best option is to handle the logic of dealing with reference in the code library, and you may have to use one library, can’t easily mix two different libraries to share reference. And how you pass the reference in/out to robot file could be a unique identifier string on robot file side, on the custom code library side, you maintain a dict/map of unique ID strings to actual object references. So with this approach, you aren’t actually passing the reference or instance around, you’re just passing the ID link to the instance/reference held within the custom library itself. Such only works with custom libraries as well - I don’t think such a feature is built into the SeleniumLibrary itself but I could be wrong.