Skip to content

Commit

Permalink
Respect base_url when opening new windows. Fixes #129
Browse files Browse the repository at this point in the history
  • Loading branch information
ggozad committed Dec 21, 2022
1 parent cee76ad commit cd5d4f9
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
3 changes: 3 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down
19 changes: 11 additions & 8 deletions src/behaving/web/steps/windows.py
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
8 changes: 8 additions & 0 deletions tests/features/windows.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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"

0 comments on commit cd5d4f9

Please sign in to comment.