diff --git a/CHANGES.txt b/CHANGES.txt index c0aaa7ac..eba27448 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -4,6 +4,9 @@ Changelog 3.1.2 - ------------------ +- Respect base_url when opening new windows. Fixes #129 + [ggozad] + - Use gherkin-lint for linting .feature files. [ggozad] diff --git a/src/behaving/web/steps/windows.py b/src/behaving/web/steps/windows.py index 7233c6ce..a7651b77 100644 --- a/src/behaving/web/steps/windows.py +++ b/src/behaving/web/steps/windows.py @@ -1,37 +1,40 @@ +from urllib.parse import urljoin + from behave import when def remember_window(context, window, name): - """ Associates a name to a Selenium WebDriver - window handle to facilitate future lookup. """ + """Associates a name to a Selenium WebDriver + window handle to facilitate future lookup.""" if not hasattr(context, "name_to_window_"): context.name_to_window_ = {} context.name_to_window_[name] = window def lookup_window(context, name): - """ Finds a Selenium WebDriver window handle by name """ + """Finds a Selenium WebDriver window handle by name""" assert hasattr(context, "name_to_window_"), "No saved windows" assert name in context.name_to_window_, f"{name} not found in saved windows" return context.name_to_window_[name] -@when(u'I name the current window "{name}"') +@when('I name the current window "{name}"') def name_window(context, name): current = context.browser.driver.current_window_handle remember_window(context, current, name) -@when(u'I open a new window named "{name}" at "{url}"') +@when('I open a new window named "{name}" at "{url}"') def open_window(context, name, url): driver = context.browser.driver - driver.switch_to.new_window('window') - driver.get(url) + driver.switch_to.new_window("window") + full_url = urljoin(context.base_url, url) + driver.get(full_url) handle = driver.window_handles[-1] remember_window(context, handle, name) -@when(u'I switch to the window named "{name}"') +@when('I switch to the window named "{name}"') def switch_window(context, name): """ Changes the window Selenium executes subsequent commands on. diff --git a/tests/features/windows.feature b/tests/features/windows.feature index eca99d0d..c5fb99eb 100644 --- a/tests/features/windows.feature +++ b/tests/features/windows.feature @@ -25,3 +25,11 @@ Feature: Windows When I switch to the window named "window1" Then I should see "I'm window 1" But I should not see "I'm window 2" + + Scenario: It can open a new window with relative urls when base_url is set. + Given a browser + Given the base url "http://web" + When I open a new window named "window1" at "/window1.html" + Then I should see "I'm window 1" + When I open a new window named "window2" at "http://web/window2.html" + Then I should see "I'm window 2"