Skip to content

Commit

Permalink
Update for the new wait features.
Browse files Browse the repository at this point in the history
  • Loading branch information
bbangert committed Mar 16, 2011
1 parent 94857ea commit 17b65c8
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 22 deletions.
5 changes: 5 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
0.1.3
-----

- Added 'I should see ".." within X seconds' step.

0.1.2
-----

Expand Down
1 change: 1 addition & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ used with Given/When/Then/And as desired.
# general
I should see "Page Content"
I see "Page Content"
I should see "Page Content" within 4 seconds
I should not see "Foobar"
I should be at "http://foobar.com/"
I should see an element with id of "http://bar.com/"
Expand Down
21 changes: 20 additions & 1 deletion lettuce_webdriver/tests/test_webdriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,18 @@
Scenario: Everything fires up
When I go to "%(page)s"
Then I should see an element with id of "bio_field"
And I should see an element with id of "somediv" within 2 seconds
And I should not see an element with id of "hidden_text"
And I should see "Weeeee" within 1 second
""" % {'page': PAGES['basic_page']}


FEATURE13 = """
Feature: Basic page formstuff
Scenario: Everything fires up
When I go to "%(page)s"
Then I should see "Hello there" within 1 second
And I should see an element with id of "oops_field" within 1 second
And I should not see an element with id of "hidden_text"
""" % {'page': PAGES['basic_page']}

Expand Down Expand Up @@ -220,4 +232,11 @@ def test_feature12(self):
f = Feature.from_string(FEATURE12)
feature_result = f.run()
scenario_result = feature_result.scenario_results[0]
self.assertEquals(len(scenario_result.steps_passed), 3)
self.assertEquals(len(scenario_result.steps_passed), 4)

def test_feature13(self):
import lettuce_webdriver.webdriver
f = Feature.from_string(FEATURE13)
feature_result = f.run()
scenario_result = feature_result.scenario_results[0]
self.assertEquals(len(scenario_result.steps_passed), 2)
78 changes: 58 additions & 20 deletions lettuce_webdriver/webdriver.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
"""Webdriver support for lettuce"""
import time

from nose.tools import assert_true

from lettuce import step
from lettuce import world

Expand All @@ -10,11 +14,32 @@
from lettuce_webdriver.util import find_option


def wait_for_elem(browser, xpath, timeout=15):
start = time.time()
elems = []
while time.time() - start < timeout:
elems = browser.find_elements_by_xpath(xpath)
if elems:
return elems
time.sleep(0.2)
return elems


def wait_for_content(browser, content, timeout=15):
start = time.time()
while time.time() - start < timeout:
if content in world.browser.get_page_source():
return
time.sleep(0.2)
assert_true(content in world.browser.get_page_source())


## URLS
@step('I visit "(.*?)"')
def visit(step, url):
world.browser.get(url)


@step('I go to "(.*?)"')
def goto(step, url):
world.browser.get(url)
Expand All @@ -29,13 +54,13 @@ def click(step, name):

@step('I should see a link with the url "(.*?)"')
def should_see_link(step, link_url):
assert world.browser.find_element_by_xpath('//a[@href="%s"]' % link_url)
assert_true(world.browser.find_element_by_xpath('//a[@href="%s"]' % link_url))


@step('I should see a link to "(.*?)" with the url "(.*?)"')
def should_see_link_text(step, link_text, link_url):
assert world.browser.find_element_by_xpath('//a[@href="%s"][./text()="%s"]' %
(link_url, link_text))
assert_true(world.browser.find_element_by_xpath('//a[@href="%s"][./text()="%s"]' %
(link_url, link_text)))


@step('I should see a link that contains the text "(.*?)" and the url "(.*?)"')
Expand All @@ -54,55 +79,68 @@ def element_contains(step, element_id, value):
@step('The element with id of "(.*?)" does not contain "(.*?)"')
def element_contains(step, element_id, value):
elem = world.browser.find_element_by_xpath('//*[@id="%s"]' % element_id)
assert value not in elem.text
assert_true(value not in elem.text)


@step('I should see an element with id of "(.*?)" within (\d+) seconds?')
def should_see_id(step, element_id, timeout):
elem = wait_for_elem(world.browser, '//*[@id="%s"]' % element_id, int(timeout))
assert_true(elem)
elem = elem[0]
assert_true(elem.is_displayed())


@step('I should see an element with id of "(.*?)"')
def should_see_id(step, element_id):
elem = world.browser.find_element_by_xpath('//*[@id="%s"]' % element_id)
assert elem.is_displayed()
assert_true(elem.is_displayed())


@step('I should not see an element with id of "(.*?)"')
def should_see_id(step, element_id):
elem = world.browser.find_element_by_xpath('//*[@id="%s"]' % element_id)
assert not elem.is_displayed()
assert_true(not elem.is_displayed())


@step('I should see "([^"]+)" within (\d+) seconds?')
def should_see(step, text, timeout):
wait_for_content(world.browser, text, int(timeout))


@step('I should see "([^"]+)"')
def should_see(step, text):
assert text in world.browser.get_page_source()
assert_true(text in world.browser.get_page_source())


@step('I see "([^"]+)"')
def see(step, text):
assert text in world.browser.get_page_source()
assert_true(text in world.browser.get_page_source())


@step('I should not see "([^"]+)"')
def should_not_see(step, text):
assert text not in world.browser.get_page_source()
assert_true(text not in world.browser.get_page_source())


@step('I should be at "(.*?)"')
def url_should_be(step, url):
assert url == world.browser.current_url
assert_true(url == world.browser.current_url)


## Browser
@step('The browser\'s URL should be "(.*?)"')
def browser_url_should_be(step, url):
assert url == world.browser.current_url
assert_true(url == world.browser.current_url)


@step ('The browser\'s URL should contain "(.*?)"')
def url_should_contain(step, url):
assert url in world.browser.current_url
assert_true(url in world.browser.current_url)


@step ('The browser\'s URL should not contain "(.*?)"')
def url_should_not_contain(step, url):
assert url not in world.browser.current_url
assert_true(url not in world.browser.current_url)


## Forms
Expand Down Expand Up @@ -142,13 +180,13 @@ def uncheck_checkbox(step, value):
@step('The "(.*?)" checkbox should be checked')
def assert_checked_checkbox(step, value):
check_box = find_field(world.browser, 'checkbox', value)
assert check_box.is_selected()
assert_true(check_box.is_selected())


@step('The "(.*?)" checkbox should not be checked')
def assert_not_checked_checkbox(step, value):
check_box = find_field(world.browser, 'checkbox', value)
assert not check_box.is_selected()
assert_true(not check_box.is_selected())


@step('I select "(.*?)" from "(.*?)"')
Expand Down Expand Up @@ -177,7 +215,7 @@ def select_multi_items(step, select_name):
@step('The "(.*?)" option from "(.*?)" should be selected')
def assert_single_selected(step, option_name, select_name):
option_box = find_option(world.browser, select_name, option_name)
assert option_box.is_selected()
assert_true(option_box.is_selected())


@step('The following options from "(.*?)" should be selected')
Expand All @@ -191,9 +229,9 @@ def assert_multi_selected(step, select_name):
option.get_attribute('name') in option_names or \
option.get_attribute('value') in option_names or \
option.text in option_names:
assert option.is_selected()
assert_true(option.is_selected())
else:
assert not option.is_selected()
assert_true(not option.is_selected())


@step('I choose "(.*?)"')
Expand All @@ -205,10 +243,10 @@ def choose_radio(step, value):
@step('The "(.*?)" option should be chosen')
def assert_radio_selected(step, value):
box = find_field(world.browser, 'radio', value)
assert box.is_selected()
assert_true(box.is_selected())


@step('The "(.*?)" option should not be chosen')
def assert_radio_selected(step, value):
box = find_field(world.browser, 'radio', value)
assert not box.is_selected()
assert_true(not box.is_selected())
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = '0.1.2'
__version__ = '0.1.3'

import os

Expand Down

0 comments on commit 17b65c8

Please sign in to comment.