mrsmith
mrsmith

Reputation: 11

How can I keep the browser open after I open it with a python function Open Browser in RobotFramework

I have the following python file OpenBrowser.py

from selenium import webdriver
from selenium.webdriver.chrome.options import Options



def openit(browser):

chrome_options = Options()
chrome_options.add_argument("--headless")

desired_capabilities = chrome_options.to_capabilities()
desired_capabilities['acceptInsecureCerts'] = True


driver = webdriver.Chrome()
#driver = webdriver.Chrome("C:\Python27\Scripts\chromedriver.exe", chrome_options=chrome_options,desired_capabilities=desired_capabilities)
driver.get("http://www.python.org")

return browser

and a robot file:

*** Settings ***
Documentation    Suite description
Library        OpenBrowser.py

*** Test Cases ***
Test title
    openit  browser

The browser is open, but then it closes and if I want to run another keyword in RF I get error: No brpwser is open

How can I run the python function and keep the browser open?

Upvotes: 1

Views: 1842

Answers (1)

A. Kootstra
A. Kootstra

Reputation: 6981

From the top of my head this should allow you to do what you want:

*** Settings ***
Library    SeleniumLibrary

Suite Teardown    Close All Browsers

*** Test Cases ***
TC
    # Options for startin Chrome
    ${chrome_options}=    Evaluate    sys.modules['selenium.webdriver'].ChromeOptions()    sys, selenium.webdriver

    Call Method    ${chrome_options}    add_argument    headless
    Call Method    ${chrome options}    add_argument    ignore-certificate-errors

    # Arguments for starting ChromeDriver
    @{service_args}    Create List
        ...                --verbose
        ...                --log-path=${EXECDIR}/chromedriver.log

    Create Webdriver    Chrome    chrome_options=${chrome_options}    service_args=${service_args} 

    Go To    https://self-signed.badssl.com/

    Capture Page Screenshot

The service arguments will instruct ChromeDriver to generate a log file for you in the directory where you start Robot Framework. This may help with the analysis.

Upvotes: 1

Related Questions