Skip to content

Commit

Permalink
Optional URL in Open Browser (#1470)
Browse files Browse the repository at this point in the history
Making URL argument optional in the Open Browser keyword.

Fixes #1464
  • Loading branch information
JonKoser authored and aaltat committed Oct 4, 2019
1 parent 36645bd commit 9dd7017
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 11 deletions.
28 changes: 18 additions & 10 deletions src/SeleniumLibrary/keywords/browsermanagement.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ def close_browser(self):
self.drivers.close()

@keyword
def open_browser(self, url, browser='firefox', alias=None,
def open_browser(self, url=None, browser='firefox', alias=None,
remote_url=False, desired_capabilities=None,
ff_profile_dir=None, options=None, service_log_path=None):
"""Opens a new browser instance to the given ``url``.
"""Opens a new browser instance to the optional ``url``.
The ``browser`` argument specifies which browser to use. The
supported browsers are listed in the table below. The browser names
Expand Down Expand Up @@ -87,6 +87,9 @@ def open_browser(self, url, browser='firefox', alias=None,
Headless Chrome are new additions in SeleniumLibrary 3.1.0
and require Selenium 3.8.0 or newer.
After opening the browser, it is possible to use optional
``url`` to navigate the browser to the desired address.
Optional ``alias`` is an alias given for this browser instance and
it can be used for switching between browsers. When same ``alias``
is given with two `Open Browser` keywords, the first keyword will
Expand Down Expand Up @@ -212,6 +215,7 @@ def open_browser(self, url, browser='firefox', alias=None,
| `Open Browser` | http://example.com | Firefox | alias=Firefox |
| `Open Browser` | http://example.com | Edge | remote_url=http://127.0.0.1:4444/wd/hub |
| `Open Browser` | about:blank | | |
| `Open Browser` | browser=Chrome | | |
Alias examples:
| ${1_index} = | `Open Browser` | http://example.com | Chrome | alias=Chrome | # Opens new browser because alias is new. |
Expand Down Expand Up @@ -248,18 +252,21 @@ def open_browser(self, url, browser='firefox', alias=None,
accepting an instance of the `selenium.webdriver.FirefoxProfile`
and support defining FirefoxProfile with methods and
attributes are new in SeleniumLibrary 4.0.
Making ``url`` optional is new in SeleniumLibrary 4.1.
"""
index = self.drivers.get_index(alias)
if index:
self.info('Using existing browser from index %s.' % index)
self.switch_browser(alias)
self.go_to(url)
if is_truthy(url):
self.go_to(url)
return index
return self._make_new_browser(url, browser, alias, remote_url,
desired_capabilities, ff_profile_dir,
options, service_log_path)

def _make_new_browser(self, url, browser='firefox', alias=None,
def _make_new_browser(self, url=None, browser='firefox', alias=None,
remote_url=False, desired_capabilities=None,
ff_profile_dir=None, options=None, service_log_path=None):
if is_truthy(remote_url):
Expand All @@ -272,12 +279,13 @@ def _make_new_browser(self, url, browser='firefox', alias=None,
options, service_log_path)
driver = self._wrap_event_firing_webdriver(driver)
index = self.ctx.register_driver(driver, alias)
try:
driver.get(url)
except Exception:
self.debug("Opened browser with session id %s but failed "
"to open url '%s'." % (driver.session_id, url))
raise
if is_truthy(url):
try:
driver.get(url)
except Exception:
self.debug("Opened browser with session id %s but failed "
"to open url '%s'." % (driver.session_id, url))
raise
self.debug('Opened browser with session id %s.' % driver.session_id)
return index

Expand Down
31 changes: 30 additions & 1 deletion utest/test/keywords/test_keyword_arguments_browsermanagement.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import unittest

from mockito import mock, unstub, when
from mockito import mock, unstub, when, verify, ANY

from SeleniumLibrary.keywords import BrowserManagementKeywords

Expand All @@ -12,6 +12,7 @@ def setUp(self):
ctx.event_firing_webdriver = None
ctx._browser = mock()
ctx._drivers = mock()
self.ctx = ctx
self.brorser = BrowserManagementKeywords(ctx)

def tearDown(self):
Expand All @@ -31,3 +32,31 @@ def test_open_browser(self):
alias = self.brorser.open_browser(url, alias='None',
remote_url=remote_url)
self.assertEqual(alias, None)

def test_same_alias(self):
url = 'https://github.com/robotframework'
alias = 'tidii'
driver = mock()
driver.session_id = 'foobar'
self.ctx.driver = driver
when(self.ctx._drivers).get_index(alias).thenReturn(1)
when(self.ctx._drivers).switch(1).thenReturn(driver)
self.brorser.open_browser(url=url, alias=alias)
verify(driver, times=1).get(url)

def test_open_browser_no_get(self):
browser = mock()
when(self.brorser)._make_driver('firefox', None,
None, False, None, None).thenReturn(browser)
self.brorser.open_browser()
verify(browser, times=0).get(ANY)

def test_same_alias_and_not_get(self):
alias = 'tidii'
driver = mock()
driver.session_id = 'foobar'
self.ctx.driver = driver
when(self.ctx._drivers).get_index(alias).thenReturn(1)
when(self.ctx._drivers).switch(1).thenReturn(driver)
self.brorser.open_browser(alias=alias)
verify(driver, times=0).get(ANY)

0 comments on commit 9dd7017

Please sign in to comment.