Maintain Browser Session from Custom Library

Problem: How do you maintain the session and pass it back to be used in a following Keyword?
Error: Could not find active page

my robot file has a test:
authentication a user this is via a custom library which works by calling browser() in the function via from Browser import Browser
Click “Go To This Link” this would be using Library Browser in the robot file

Hi Bob,

Your question is not clear what you are after, but I think you need to establish and maintain a context? See Browser, Context and Page for details.

hope that helps,

Dave.

It appears as though the browser object when defined in the custom library doesn’t pass back to the calling robot file so that it can be used by Library Browser in the Click Click “Keyword Documentation” step.

ERROR: Could not find active page

ROBO.ROBOT
*** Settings ***
Library Browser
Library robo_lib.py

*** Test Cases ***
Test Using Python File
Test Keyword Filtering
Click “Keyword Documentation”
[Teardown] Close Browser

ROBO_LIB.PY
from Browser import Browser
from Browser import SupportedBrowsers

browser = Browser()

def test_keyword_filtering():
browser.new_browser(SupportedBrowsers.chromium, headless=False)
browser.new_page(“https://robotframework-browser.org/”)
assert browser.get_title() == “Browser Library”

Hi Bob,

I’ll have to leave that for a Browser Library maintainer to answer, but it looks to me like you have 2 instances of browser library, one in robot framework, and a separate one in the python file, so I’m not surprised by the error you got, but I also don’t know how to make your test case work the way you want.

Dave.

Seems as though this is the answer. Start the initial browser process off in the robot file. grab the started library Browser reference using BuiltIn().get_library_instance(‘Browser’) in the custom python library (do what you need to do there…), then you’ll be able to continue on the robot file with additional browser steps. Only Caveat seems like code complete doesn’t work on the builtIn object (at least in Pycharm for me)

*** Settings ***
Library Browser
Library RoboLib.py

*** Test Cases ***
Test Using Python File
New Page https://www.google.com
Test Custom Lib Keyword
Keyboard Key PRESS Enter
[Teardown] Close Browser

from Browser.utils.data_types import ElementRole
from robot.api.logger import info
from robot.libraries.BuiltIn import BuiltIn

class RoboLib:

ROBOT_LIBRARY_SCOPE = 'SUITE'

def test_custom_lib_keyword(self):

    pw_browser = BuiltIn().get_library_instance('Browser')

    info(pw_browser.get_title())
    assert pw_browser.get_title() == "Google"
    pw_browser.type_text(pw_browser.get_element_by_role(ElementRole.COMBOBOX, name="Search"), "Puppies")
1 Like