Skip to content

Commit 947a943

Browse files
authored
fix docstring, add getting available port number (appium#448)
* fix docstring, add getting available port number * add WebDriverWait * define custom wait * move get available port in another module * follow python wait condition name
1 parent eed8db7 commit 947a943

File tree

4 files changed

+48
-10
lines changed

4 files changed

+48
-10
lines changed

appium/webdriver/webdriver.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -705,7 +705,7 @@ def session(self):
705705
def all_sessions(self):
706706
""" Retrieves all sessions that are open
707707
Usage:
708-
sessions = driver.allSessions
708+
sessions = driver.all_sessions
709709
Returns:
710710
`dict containing all open sessions`
711711
"""

test/functional/ios/helper/desired_capabilities.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,11 @@ def wda_port():
6363

6464
return 8100
6565

66+
6667
# Before running tests, you must have iOS simulators named 'iPhone 6s - 8100' and 'iPhone 6s - 8101'
6768

6869

69-
def iphone_device_name():
70+
def iphone_device_name(port=None):
7071
if PytestXdistWorker.NUMBER == PytestXdistWorker.gw(0):
7172
return 'iPhone 8 - 8100'
7273
elif PytestXdistWorker.NUMBER == PytestXdistWorker.gw(1):

test/functional/ios/webdriver_tests.py

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,11 @@
1414

1515
import unittest
1616

17+
from selenium.webdriver.support.ui import WebDriverWait
18+
1719
from appium import webdriver
1820
from appium.webdriver.applicationstate import ApplicationState
21+
from test.functional.test_helper import get_available_from_port_range
1922

2023
from .helper import desired_capabilities
2124

@@ -24,20 +27,31 @@ class WebDriverTests(unittest.TestCase):
2427
def setUp(self):
2528
desired_caps = desired_capabilities.get_desired_capabilities('UICatalog.app.zip')
2629
self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
27-
desired_caps['deviceName'] = 'iPhone Xs Max'
28-
desired_caps['wdaLocalPort'] = '8102'
2930

3031
def tearDown(self):
3132
self.driver.quit()
3233

33-
def testAllSessions(self):
34+
def test_all_sessions(self):
35+
port = get_available_from_port_range(8200, 8300)
3436
desired_caps = desired_capabilities.get_desired_capabilities('UICatalog.app.zip')
3537
desired_caps['deviceName'] = 'iPhone Xs Max'
36-
desired_caps['wdaLocalPort'] = '8102'
37-
self.driver1 = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
38-
self.assertEqual(2, len(self.driver.all_sessions))
39-
if self.driver1:
40-
self.driver1.quit()
38+
desired_caps['wdaLocalPort'] = port
39+
40+
class session_counts_is_two(object):
41+
TIMEOUT = 10
42+
43+
def __call__(self, driver):
44+
return len(driver.all_sessions) == 2
45+
46+
driver2 = None
47+
try:
48+
driver2 = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
49+
WebDriverWait(
50+
driver2, session_counts_is_two.TIMEOUT).until(session_counts_is_two())
51+
self.assertEqual(2, len(self.driver.all_sessions))
52+
finally:
53+
if driver2 is not None:
54+
driver2.quit()
4155

4256
def test_app_management(self):
4357
# this only works in Xcode9+

test/functional/test_helper.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import socket
2+
3+
4+
class NoAvailablePortError(Exception):
5+
pass
6+
7+
8+
def get_available_from_port_range(from_port, to_port):
9+
"""Returns available local port number.
10+
"""
11+
r = range(from_port, to_port)
12+
13+
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
14+
15+
for port in r:
16+
try:
17+
if sock.connect_ex(('localhost', port)) != 0:
18+
return port
19+
finally:
20+
sock.close()
21+
22+
raise NoAvailablePortError('No available port between {} and {}'.format(
23+
from_port, to_port))

0 commit comments

Comments
 (0)