Skip to content

Commit 9dd7017

Browse files
JonKoseraaltat
authored andcommitted
Optional URL in Open Browser (robotframework#1470)
Making URL argument optional in the Open Browser keyword. Fixes robotframework#1464
1 parent 36645bd commit 9dd7017

File tree

2 files changed

+48
-11
lines changed

2 files changed

+48
-11
lines changed

src/SeleniumLibrary/keywords/browsermanagement.py

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,10 @@ def close_browser(self):
5656
self.drivers.close()
5757

5858
@keyword
59-
def open_browser(self, url, browser='firefox', alias=None,
59+
def open_browser(self, url=None, browser='firefox', alias=None,
6060
remote_url=False, desired_capabilities=None,
6161
ff_profile_dir=None, options=None, service_log_path=None):
62-
"""Opens a new browser instance to the given ``url``.
62+
"""Opens a new browser instance to the optional ``url``.
6363
6464
The ``browser`` argument specifies which browser to use. The
6565
supported browsers are listed in the table below. The browser names
@@ -87,6 +87,9 @@ def open_browser(self, url, browser='firefox', alias=None,
8787
Headless Chrome are new additions in SeleniumLibrary 3.1.0
8888
and require Selenium 3.8.0 or newer.
8989
90+
After opening the browser, it is possible to use optional
91+
``url`` to navigate the browser to the desired address.
92+
9093
Optional ``alias`` is an alias given for this browser instance and
9194
it can be used for switching between browsers. When same ``alias``
9295
is given with two `Open Browser` keywords, the first keyword will
@@ -212,6 +215,7 @@ def open_browser(self, url, browser='firefox', alias=None,
212215
| `Open Browser` | http://example.com | Firefox | alias=Firefox |
213216
| `Open Browser` | http://example.com | Edge | remote_url=http://127.0.0.1:4444/wd/hub |
214217
| `Open Browser` | about:blank | | |
218+
| `Open Browser` | browser=Chrome | | |
215219
216220
Alias examples:
217221
| ${1_index} = | `Open Browser` | http://example.com | Chrome | alias=Chrome | # Opens new browser because alias is new. |
@@ -248,18 +252,21 @@ def open_browser(self, url, browser='firefox', alias=None,
248252
accepting an instance of the `selenium.webdriver.FirefoxProfile`
249253
and support defining FirefoxProfile with methods and
250254
attributes are new in SeleniumLibrary 4.0.
255+
256+
Making ``url`` optional is new in SeleniumLibrary 4.1.
251257
"""
252258
index = self.drivers.get_index(alias)
253259
if index:
254260
self.info('Using existing browser from index %s.' % index)
255261
self.switch_browser(alias)
256-
self.go_to(url)
262+
if is_truthy(url):
263+
self.go_to(url)
257264
return index
258265
return self._make_new_browser(url, browser, alias, remote_url,
259266
desired_capabilities, ff_profile_dir,
260267
options, service_log_path)
261268

262-
def _make_new_browser(self, url, browser='firefox', alias=None,
269+
def _make_new_browser(self, url=None, browser='firefox', alias=None,
263270
remote_url=False, desired_capabilities=None,
264271
ff_profile_dir=None, options=None, service_log_path=None):
265272
if is_truthy(remote_url):
@@ -272,12 +279,13 @@ def _make_new_browser(self, url, browser='firefox', alias=None,
272279
options, service_log_path)
273280
driver = self._wrap_event_firing_webdriver(driver)
274281
index = self.ctx.register_driver(driver, alias)
275-
try:
276-
driver.get(url)
277-
except Exception:
278-
self.debug("Opened browser with session id %s but failed "
279-
"to open url '%s'." % (driver.session_id, url))
280-
raise
282+
if is_truthy(url):
283+
try:
284+
driver.get(url)
285+
except Exception:
286+
self.debug("Opened browser with session id %s but failed "
287+
"to open url '%s'." % (driver.session_id, url))
288+
raise
281289
self.debug('Opened browser with session id %s.' % driver.session_id)
282290
return index
283291

utest/test/keywords/test_keyword_arguments_browsermanagement.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import unittest
22

3-
from mockito import mock, unstub, when
3+
from mockito import mock, unstub, when, verify, ANY
44

55
from SeleniumLibrary.keywords import BrowserManagementKeywords
66

@@ -12,6 +12,7 @@ def setUp(self):
1212
ctx.event_firing_webdriver = None
1313
ctx._browser = mock()
1414
ctx._drivers = mock()
15+
self.ctx = ctx
1516
self.brorser = BrowserManagementKeywords(ctx)
1617

1718
def tearDown(self):
@@ -31,3 +32,31 @@ def test_open_browser(self):
3132
alias = self.brorser.open_browser(url, alias='None',
3233
remote_url=remote_url)
3334
self.assertEqual(alias, None)
35+
36+
def test_same_alias(self):
37+
url = 'https://github.com/robotframework'
38+
alias = 'tidii'
39+
driver = mock()
40+
driver.session_id = 'foobar'
41+
self.ctx.driver = driver
42+
when(self.ctx._drivers).get_index(alias).thenReturn(1)
43+
when(self.ctx._drivers).switch(1).thenReturn(driver)
44+
self.brorser.open_browser(url=url, alias=alias)
45+
verify(driver, times=1).get(url)
46+
47+
def test_open_browser_no_get(self):
48+
browser = mock()
49+
when(self.brorser)._make_driver('firefox', None,
50+
None, False, None, None).thenReturn(browser)
51+
self.brorser.open_browser()
52+
verify(browser, times=0).get(ANY)
53+
54+
def test_same_alias_and_not_get(self):
55+
alias = 'tidii'
56+
driver = mock()
57+
driver.session_id = 'foobar'
58+
self.ctx.driver = driver
59+
when(self.ctx._drivers).get_index(alias).thenReturn(1)
60+
when(self.ctx._drivers).switch(1).thenReturn(driver)
61+
self.brorser.open_browser(alias=alias)
62+
verify(driver, times=0).get(ANY)

0 commit comments

Comments
 (0)