Skip to content

Commit

Permalink
Add Python 3 support (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
SUNx2YCH authored Jun 22, 2016
1 parent 38c7fc0 commit 19f998b
Show file tree
Hide file tree
Showing 21 changed files with 44 additions and 36 deletions.
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ language: python
python:
- "2.6"
- "2.7"
- "3.3"
- "3.4"
- "3.5"
install:
- pip install flake8
- pip install .
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ if __name__ == '__main__':
home_page.text_field.send_keys('Page Object')
home_page.button.click()
results_page = ResultsPage()
print 'Results summary: %s' % results_page.stat.text
print('Results summary: ' + results_page.stat.text)
for item in results_page.results:
print item.link.text
print(item.link.text)
get_driver().quit()
```

Expand Down
2 changes: 1 addition & 1 deletion examples/10_containers.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ def __init__(self):
page.open()
for vacancy in page.vacancies_list:
# search by CLASS_NAME='vacancy_title' is performed within one node
print 'Title: %s' % vacancy.title.text
print('Title: ' + vacancy.title.text)
4 changes: 2 additions & 2 deletions examples/11_logical_containers.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ def __init__(self):
if __name__ == '__main__':
page = StructuredPage()
page.open()
print page.header.sign_in.text
print page.header.register.text
print(page.header.sign_in.text)
print(page.header.register.text)
4 changes: 2 additions & 2 deletions examples/12_is_element_present.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ def __init__(self):
if __name__ == '__main__':
page = PresencePage()
page.open()
print 'This should be True: %s' % page.is_element_present('join_link')
print 'This should be False: %s' % page.is_element_present('no_such_link')
print('This should be True: ' + str(page.is_element_present('join_link')))
print('This should be False: ' + str(page.is_element_present('no_such_link')))
4 changes: 2 additions & 2 deletions examples/1_basic_google.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class ResultsPage(BasePage):
home_page.text_field.send_keys('Page Object')
home_page.button.click()
results_page = ResultsPage()
print 'Results summary: %s' % results_page.stat.text
print('Results summary: ' + results_page.stat.text)
for item in results_page.results:
print item.link.text
print(item.link.text)
get_driver().quit()
2 changes: 1 addition & 1 deletion examples/4_base_page_driver_handling.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ def __init__(self, *args, **kwargs):
my_driver = Firefox()
page = DriverHandlingPage(driver=my_driver)
page.open()
print 'Page title: %s' % my_driver.title
print('Page title: ' + my_driver.title)
my_driver.quit()
2 changes: 1 addition & 1 deletion examples/6_find_ui_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ def __init__(self):
if __name__ == '__main__':
page = TypedPage()
page.open()
print 'Is link secure: %s' % page.join_link.is_secure()
print('Is link secure: ' + str(page.join_link.is_secure()))
2 changes: 1 addition & 1 deletion examples/7_find_dynamic.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def __init__(self):
super(DynamicPage, self).__init__(url='http://wargaming.net')

def get_link_by_href(self, href):
return Find(by=By.CSS_SELECTOR, value='a[href*="%s"]' % href, context=self)
return Find(by=By.CSS_SELECTOR, value='a[href*="{0}"]'.format(href), context=self)

if __name__ == '__main__':
page = DynamicPage()
Expand Down
2 changes: 1 addition & 1 deletion examples/8_finds.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ def __init__(self):
if __name__ == '__main__':
page = LinksPage()
page.open()
print 'Number of links: %s' % len(page.links)
print('Number of links: ' + str(len(page.links)))
2 changes: 1 addition & 1 deletion examples/9_finds_ui_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ def get_unsecured_links(self):
if __name__ == '__main__':
page = TypedPage()
page.open()
print 'Number of unsecured links: %s' % len(page.get_unsecured_links())
print('Number of unsecured links: ' + str(len(page.get_unsecured_links())))
4 changes: 4 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
'Programming Language :: Python',
'Programming Language :: Python :: 2.6',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3.3',
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3.5',
],
packages=[
'webium',
Expand All @@ -24,6 +27,7 @@
'It allows you to extend WebElement class to your custom controls '
'like Link, Button and group them as pages.',
install_requires=[
'future>=0.14',
'selenium',
'waiting>=1.2.1',
],
Expand Down
9 changes: 4 additions & 5 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import SimpleHTTPServer
import SocketServer
from http.server import HTTPServer, SimpleHTTPRequestHandler
from threading import Thread

from webium.driver import close_driver

httpd = SocketServer.TCPServer(('', 0), SimpleHTTPServer.SimpleHTTPRequestHandler)
httpd = HTTPServer(('', 0), SimpleHTTPRequestHandler)
PORT = httpd.server_address[1]


def setup_package():
print "serving at port", PORT
print("serving at port " + str(PORT))
thread = Thread(target=lambda: httpd.serve_forever())
thread.daemon = True
thread.start()
Expand All @@ -20,4 +19,4 @@ def teardown_package():


def get_url(suffix=''):
return 'http://localhost:%s/tests/pages/%s' % (PORT, suffix)
return 'http://localhost:{0}/tests/pages/{1}'.format(PORT, suffix)
4 changes: 3 additions & 1 deletion tests/cookie/test_cookie.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from http.cookiejar import Cookie
from unittest import TestCase
from cookielib import Cookie

from nose.tools import eq_

from webium.cookie import convert_cookie_to_dict


Expand Down
4 changes: 2 additions & 2 deletions tests/simple_page/finds/test_finds.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ def test_dynamic_find(self):
eq_(paragraphs[1].text, 'after pre')

def test_custom_elements(self):
anchors = self.page.anchor_list
eq_(filter(lambda x: x.get_href(), anchors)[0].get_href(), get_url('resultPage.html'))
hrefs = [a.get_href() for a in self.page.anchor_list if a.get_href()]
eq_(hrefs[0], get_url('resultPage.html'))

def test_is_element_present(self):
divs = self.page.div_list
Expand Down
2 changes: 1 addition & 1 deletion tests/simple_page/test_no_wait_decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def test_no_wait_decorator(self):
start = time.time()
self.method_with_decorator()
exec_time = time.time() - start
ok_(exec_time < 3, 'Execution took too much time %s' % exec_time)
ok_(exec_time < 3, 'Execution took too much time: ' + str(exec_time))

def tearDown(self):
webium.driver.get_driver().implicitly_wait(webium.settings.implicit_timeout)
Expand Down
2 changes: 1 addition & 1 deletion webium/base_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def _get_driver():
def is_displayed():
element = getattr(self, element_name, None)
if not element:
raise WebiumException('No element "%s" within container %s' % (element_name, self))
raise WebiumException('No element "{0}" within container {1}'.format(element_name, self))
return element.is_displayed()

is_displayed() if just_in_dom else wait(lambda: is_displayed(), timeout_seconds=timeout)
Expand Down
12 changes: 6 additions & 6 deletions webium/controls/select.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def select_by_visible_text(self, text):
@params text - string visible text
"""
xpath = './/option[normalize-space(.) = %s]' % self._escape_string(text)
xpath = './/option[normalize-space(.) = {0}]'.format(self._escape_string(text))
opts = self.find_elements_by_xpath(xpath)
matched = False
for opt in opts:
Expand All @@ -75,7 +75,7 @@ def select_by_visible_text(self, text):
if sub_string_without_space == "":
candidates = self.get_options()
else:
xpath = ".//option[contains(.,%s)]" % self._escape_string(sub_string_without_space)
xpath = ".//option[contains(.,{0})]".format(self._escape_string(sub_string_without_space))
candidates = self.find_elements_by_xpath(xpath)
for candidate in candidates:
if text == candidate.text:
Expand All @@ -84,25 +84,25 @@ def select_by_visible_text(self, text):
return
matched = True
if not matched:
raise NoSuchElementException("Could not locate element with visible text: %s" % text)
raise NoSuchElementException("Could not locate element with visible text: " + str(text))

@staticmethod
def _escape_string(value):
if '"' in value and "'" in value:
substrings = value.split('"')
result = ['concat(']
for substring in substrings:
result.append('"%s"' % substring)
result.append('"{0}"'.format(substring))
result.append(', \'"\', ')
result.pop()
if value.endswith('"'):
result.append(', \'"\'')
return ''.join(result) + ')'

if '"' in value:
return "'%s'" % value
return "'{0}'".format(value)

return '"%s"' % value
return '"{0}"'.format(value)

@staticmethod
def _get_longest_token(value):
Expand Down
2 changes: 1 addition & 1 deletion webium/cookie.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@


def _to_unicode_if_str(s):
if isinstance(s, str):
if isinstance(s, bytes):
try:
return s.decode('utf-8')
except UnicodeDecodeError:
Expand Down
8 changes: 4 additions & 4 deletions webium/jquery/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ def __init__(self, element):

def __getattr__(self, name):
def jquery_func(*args):
jquery = 'return $(arguments[0]).%(func)s(%(args)s);' % {
'func': name,
'args': ','.join(['arguments[%d]' % (1 + i) for i in xrange(len(args))])
}
jquery = 'return $(arguments[0]).{func}({args});'.format(
func=name,
args=','.join(['arguments[%d]' % (1 + i) for i in range(len(args))])
)
return self.driver.execute_script(jquery, self.element, *args)
return jquery_func
2 changes: 1 addition & 1 deletion webium/windows_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def save_window_set(self):
def new_window(self):
new_handles = set(self._driver.window_handles)
new_handles = new_handles - self._snapshot_of_handles
return iter(new_handles).next()
return next(iter(new_handles))

@property
def active_window(self):
Expand Down

0 comments on commit 19f998b

Please sign in to comment.