How to call open browser via API of SeleniumLibrary from python file ?

175 views
Skip to first unread message

Jagan Shankar

unread,
Mar 24, 2020, 1:08:14 PM3/24/20
to robotframework-users
I am writing python class file with a method def login().
I would like to import the right libraries and call open_browser, how to do this ?
Instead of using keywords I want call Python class method which does all Selenium actions like open browser, quit, click, get, etc., of SeleniumLibrary.

from SeleniumLibrary import SeleniumLibrary
from robot.libraries.BuiltIn import BuiltIn

class TestClass(object):
    def __init__(self, login_dict):
        self.login_dict = login_dict

    def print_args(self, url):
        print(self.login_dict)
        print(url)
        
    def login(self):
        ?????? I want to launch browser, send username, password - How to do it ?

Robot File:
*** Settings ***
Suite Teardown    Close All Browsers
Library           SeleniumLibrary
Library           TestClass.py    abcd1    WITH NAME    testclass1

*** Variables ***
${BROWSER}        Firefox
${username}       admin
${password}       admin
&{login_dict}     browser=firefox    url=https://1.1.1.1    username=admin   password=admin

*** Test Cases ***
Testcase1
    #########################################################
    #Instantiating object/Create an alias and call methods
    #########################################################
    testclass1.login

Tatu Aalto

unread,
Mar 24, 2020, 2:29:26 PM3/24/20
to jagans...@gmail.com, robotframework-users
Hi

The answer depends do you want your library and SeleniumLibrary to share the WebDriver instance to control the browser. If WebDriver instance is shared, in test data side one can use SeleniumLibrary keywords to create user keywords. But if the plan is to replace SeleniumLibrary (and only use it from the Python side) example with custom library keywords, then the solution is bit different.

SeleniumLibrary extending documentation explain simplified examples for both use cases. Get library instance [1] demonstrates the former and inheritance [2] example the later.

-Tatu
Send from my mobile

--
You received this message because you are subscribed to the Google Groups "robotframework-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to robotframework-u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/robotframework-users/43de43a6-925b-4ae4-beb3-28acc5fffdc7%40googlegroups.com.

Jagan Shankar

unread,
Mar 27, 2020, 7:59:23 AM3/27/20
to robotframework-users
Thanks for your reply and pointing to the right example - https://github.com/robotframework/SeleniumLibrary/blob/master/docs/extending/extending/get_instance/GetSeleniumLibraryInstance.py

My intention is write Python class called Browser.py with methods like login(), logout(), etc.,
These methods should call SeleniumLibrary internal methods like open_browser(), close(), etc.,. Until here I understand and could write the same.

BTW, I was able to launch browser via myclass.login()  and input text username via the keywords as well.
So I want to continue to have this working, which means I need Browser session to be accessed both by my code and keywords.

Here comes another requirement. I need to open multiple browser (may be same or different browsers) from same script.
In that case, I saw an option called alias in Open Browser keyword so that i can Open Browser with alias and launch multiple browsers and also i can switch the browser with this alias and execute commands to it like sending input text or something.

How can I achieve same in Python way if I call SeleniumLibrary methods liek open_browser() way. In the above example you have shared it has 2 methods:
open_browser()
get_browser_desired_capabilities()
In both methods I see we are calling sl = BuiltIn().get_library_instance('SeleniumLibrary')
get_library_instance returns the active instance.
But if I want to switch between browsers how can I do it ?

My Code:
========
from SeleniumLibrary import SeleniumLibrary
from robot.api import logger
from robot.libraries.BuiltIn import BuiltIn
#import REST
#import Locators.py


class Browser(object):
    def __init__(self):
        self.sel = None

    def login(self, input_dict):
        #Login Page elements
        txt_loginUserName = "name:username"
        txt_loginPassword = "name:password"
        btn_signIn = "id:btnSubmit_6"
        self.open_browser(input_dict)
        self.get_browser_desired_capabilities()
        self.sel.input_text(txt_loginUserName,input_dict['username'] )
        self.sel.input_password(txt_loginPassword,input_dict['password'] )
        title = self.sel.get_title()
        
    def open_browser(self,input_dict):
        sl = BuiltIn().get_library_instance('SeleniumLibrary')
        sl.open_browser(input_dict['url'], input_dict['browser'], input_dict['alias'], input_dict['remote_url'])
        self.sel = sl

    def get_browser_desired_capabilities(self):
        logger.info('Getting currently open browser desired capabilities')
        sl = BuiltIn().get_library_instance('SeleniumLibrary')
        print(sl.driver.desired_capabilities)
        

Robot Script:
==============
*** Settings ***
Library           SeleniumLibrary
Library           Browser.py    WITH NAME    objbrowser1
Library           Browser.py    WITH NAME    objbrowser2

*** Variables *** &{login_dict} type=admin browser=firefox url=https://11.1.1.1 username=admin password=admin alias=one remote_url=False

*** Test Cases *** Testcase1
Browser.login ${login_dict} Browser.get_browser_desired_capabilities

To unsubscribe from this group and stop receiving emails from it, send an email to robotframework-users+unsub...@googlegroups.com.

Tatu Aalto

unread,
Mar 29, 2020, 6:19:46 AM3/29/20
to jagans...@gmail.com, robotframework-users
Hi

SeleniumLibrary has an internal cache which stores the created WebDriver instances. In practice SeleniumLibrary creates multiple WebDriver instances, stores them in the internal cache and keeps one of the WebDriver's active in the browser.

It depends how you want to implement your code, but if you want to have multiple WebDrivers, then yuo need to have cache somewhere.

-Tatu



To unsubscribe from this group and stop receiving emails from it, send an email to robotframework-u...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "robotframework-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to robotframework-u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/robotframework-users/cf448fd9-1676-4b39-a726-91899d4299a1%40googlegroups.com.

Jagan Shankar

unread,
Mar 29, 2020, 2:23:15 PM3/29/20
to robotframework-users
Thanks.
1. I need to use this line sl = BuiltIn().get_library_instance('SeleniumLibrary') in all my methods, right ? Without this line I cannot access browser ?
So if i want to maintain one instance at a time then it is fine right ?

2. You said cache maintained for WebDriver instances, so incase if you call Switch Browser keyword, you switch it to get that browser info, is that possible to do via Python way, like I call switch_browser(aliasname) so I can talk to that browser ?
To unsubscribe from this group and stop receiving emails from it, send an email to robotframework-users+unsub...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "robotframework-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to robotframework-users+unsub...@googlegroups.com.

Tatu Aalto

unread,
Mar 29, 2020, 2:30:54 PM3/29/20
to jagans...@gmail.com, robotframework-users
1) That line will give you access to the SeleniumLibrary instance. The currently active browser is the driver attribute. Example
so.driver....

2) Yes that is possible.

Have you seen the SeleniumLibrary extending documentation: https://github.com/robotframework/SeleniumLibrary/blob/master/docs/extending/extending.rst


-Tatu
Send from my mobile

To unsubscribe from this group and stop receiving emails from it, send an email to robotframework-u...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "robotframework-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to robotframework-u...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "robotframework-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to robotframework-u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/robotframework-users/87ff793a-76fa-49e7-b0cd-e4f4b1e89957%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages