Skip to content

Instantly share code, notes, and snippets.

@GLMeece
Last active September 27, 2019 08:55
Show Gist options
  • Save GLMeece/30fdb358e70213508bf64b89f351ad85 to your computer and use it in GitHub Desktop.
Save GLMeece/30fdb358e70213508bf64b89f351ad85 to your computer and use it in GitHub Desktop.
Open Browser Examples
# Starting with a generic "Open Browser to Page" keyword, following that are examples for:
# Google Chrome
# PhantomJS
#
# Assumed global or suite variables:
# ${browser} - the browser you want to use in testing
# ${delay} - the "speed" that Selenium verbs execute at
# ${user_agent} - User Agent string like: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36
#
# Note: The documentation is written with the libdoc utility in mind. See:
# http://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html#library-documentation-tool-libdoc
Open Browser to Page
[Documentation] Opens one of:
... - Google Chrome
... - Mozilla Firefox
... - Microsoft Internet Explorer
... - PhantomJS
... to a given web page.
...
[Arguments] ${URL}
Run Keyword If '${browser}' == 'Chrome' Open Chrome Browser to Page ${URL}
# ... ELSE IF '${browser}' == 'Firefox' Open Firefox Browser to Page ${URL} # Not in this example
# ... ELSE IF '${browser}' == 'IE' Open Internet Explorer to Page ${URL} # Not in this example
... ELSE IF '${browser}' == 'PhantomJS' Open PhantomJS to Page ${URL}
# ... ELSE Log to Console We probably should throw an error if an invalid argument is passed...
Set Selenium Speed ${delay}
Open Chrome Browser to Page
[Documentation] Opens _Google Chrome_ to a given web page.
... For more information on what capabilities that _Google Chrome_
... supports, see [https://sites.google.com/a/chromium.org/chromedriver/capabilities|this Capabilities & ChromeOptions page].
... Also, you can see [http://peter.sh/experiments/chromium-command-line-switches|what switches Chrome/Chromium supports] as well.
[Arguments] ${URL}
${chrm_opts} = Evaluate sys.modules['selenium.webdriver'].ChromeOptions() sys, selenium.webdriver
${prefs} Create Dictionary credentials_enable_service=${False} download.default_directory=${EXECDIR}${/}data_files${/}
Call Method ${chrm_opts} add_experimental_option prefs ${prefs}
# Call Method ${chrm_opts} add_argument test-type # <-- May be needed for privately-signed certs
# Call Method ${chrm_opts} add_argument disable-extensions # <-- Probably deprecated
Call Method ${chrm_opts} add_argument disable-infobars # No "this is being remotely controlled", etc.
# Next line suppresses error: child_thread_impl.cc(762)] Request for unknown Channel-associated interface: ui::mojom::GpuMain
Call Method ${chrm_opts} add_argument disable-gpu
# Call Method ${chrm_opts} add_argument user-agent\=${user_agent} # Only if you care about User Agent strings
# Call Method ${chrm_opts} add_argument headless # If you want to run in headless mode
Run Keyword If os.sep == '/' Create Webdriver Chrome crm_alias chrome_options=${chrm_opts} executable_path=/usr/lib/chromium-browser/chromedriver
... ELSE Create Webdriver Chrome crm_alias chrome_options=${chrm_opts} #service_log_path=${sevargs}
# Maximize Browser Window # doesn't work under XVFB
Set Window Size 1500 800
# Go To http://www.useragentstring.com # <-- Enable this line to verify user agent string
# Sleep 20 seconds # <-- Convenience for the above
Go To ${URL}
Open PhantomJS to Page
[Documentation] Opens _PhantomJS_ to a given web page.
... For more information on _PhantomJS_, see
... [http://phantomjs.org/documentation/|the project documentation page].
...
... *Note*: The ``userAgent`` and ``desired_capabilities`` portions are optional. YMMV!
[Arguments] ${URL}
&{phantom_caps} = Create Dictionary phantomjs.page.settings.userAgent=${user_agent}
Open Browser ${URL} browser=phantomjs alias=PhantomBrowser desired_capabilities=&{phantom_caps}
Set Window Size 1850 960 # Set this to whatever makes the most sense for your usage
@bhanutejabobby
Copy link

bhanutejabobby commented Dec 21, 2017

Hi,

Can you suggest what would be the code for IE browser like chrome with download path setup please?

Thank You!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment