0
votes

I'm starter in Python and Robot framework. I'm trying to create and learn to use custom library in my robot framework test suite.

I have created a custom library with the following code:

from selenium import webdriver
import time

class CustomLibrary:
def Open_My_Browser(self):
    browser = webdriver.Chrome()
    browser.maximize_window()
    browser.get("http://demo.guru99.com/V4/")
    time.sleep(5)

I imported this custom library and had specified the keyword "Open My Browser". This keyword executes the code from my custom library but the next steps are from selenium2library like click button.

The execution stops and i get message "No browser is open". I understood that my selenium2library is not recognizing the browser which is opened by my customlibrary. But I'm not able to fix this problem. Can anyone please shed some advice

My robot file:

Documentation    Test the Guru99 Banking Website
Library         Selenium2Library
Library     CustomLibrary.py

*** Test Cases ***

Test Case: 001 - The user should be able to navigate to Guru99
    [Tags]  Smoke
    Open the Guru99 website

*** Keywords ***
Open the Guru99 website
    Open My Browser ```
2
Can you share your robot framework code?AutoTester213
Can you add why you want to use a custom python script? Most people that are new to Python and Robot Framework start with the out-of-the-box SeleniumLibrary keywords.A. Kootstra
until you create click element by your self because there is no session created with selenium2library has start.Sidara KEO
@A.Kootstra I'm learning on how to create and start using custom librariesDhanabalan
@SidaraKEO So that means i have to continue using custom library right ? For example, if i create a click element myself and then use selenium2library to fill in data to a textbox. Would that work ?Dhanabalan

2 Answers

3
votes

Well, of course the browser session will not be reused - it is owned by a separate object, the SeleniumLibrary/Selenium2Library has no knowledge or access to it.
It is the same as if you establish a DB or ssh connection manually, and then expect a library just to start using it - that doesn't happen.

If you want to use the keywords in the SeleniumLibrary, you need to use its Open Browser, so it has reference to it (the browser).

2
votes

You can add external Python classes(keywords) as plugins.

*** Settings ***
Library    SeleniumLibrary         plugins=${CURDIR}/Plugin.py

*** Test Cases ***
Open My Browser
    Open My Browser

Content in Plugin.py as follow:

from SeleniumLibrary import BrowserManagementKeywords
from robot.api.deco import keyword
import time


class Plugin(BrowserManagementKeywords):
    @keyword
    def open_my_browser(self):
        self.open_browser("http://demo.guru99.com/V4/", "chrome")
        self.driver.maximize_window()
        time.sleep(5)

By the way, you can also creating a new library by Extending SeleniumLibrary. Then replace Library Selenium2Library to Library <YourSeleniumLibrary>