Skip to content

Commit 86100fa

Browse files
authored
Merge pull request appium-boneyard#114 from jamie-sherriff/master
#110 add python pytest fixture examples
2 parents 554710e + 125acc7 commit 86100fa

6 files changed

Lines changed: 172 additions & 0 deletions

File tree

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
Python Pytest samples
2+
==============
3+
4+
Tested on Python 3.5
5+
pip install -r requirements.txt
6+
7+
#Run Syntax:
8+
py.test test_android_simple.py -v
9+
10+
Output will be in the format:
11+
12+
test_android_simple.py::TestSimpleAndroid::test_find_elements PASSED
13+
test_android_simple.py::TestSimpleAndroid::test_simple_actions PASSED
14+
15+
For more documentation please refer to http://doc.pytest.org/en/latest/
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import pytest
2+
import datetime
3+
import os
4+
from helpers import ensure_dir
5+
6+
7+
def pytest_configure(config):
8+
if not hasattr(config, "slaveinput"):
9+
current_day = (datetime.datetime.now().strftime("%Y_%m_%d_%H_%S"))
10+
ensure_dir("results")
11+
ensure_dir(os.path.join("results", current_day))
12+
result_dir = os.path.join(os.path.dirname(__file__), "results", current_day)
13+
ensure_dir(result_dir)
14+
result_dir_test_run = result_dir
15+
ensure_dir(os.path.join(result_dir_test_run, "screenshots"))
16+
ensure_dir(os.path.join(result_dir_test_run, "logcat"))
17+
config.screen_shot_dir = os.path.join(result_dir_test_run, "screenshots")
18+
config.logcat_dir = os.path.join(result_dir_test_run, "logcat")
19+
20+
21+
class DeviceLogger:
22+
def __init__(self, logcat_dir, screenshot_dir):
23+
self.screenshot_dir = screenshot_dir
24+
self.logcat_dir = logcat_dir
25+
26+
27+
@pytest.fixture(scope="session")
28+
def device_logger(request):
29+
logcat_dir = request.config.logcat_dir
30+
screenshot_dir = request.config.screen_shot_dir
31+
return DeviceLogger(logcat_dir, screenshot_dir)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import os
2+
3+
4+
def ensure_dir(directory):
5+
if not os.path.exists(directory):
6+
os.makedirs(directory)
7+
8+
9+
def take_screenhot_and_logcat(driver, device_logger, calling_request):
10+
logcat_dir = device_logger.logcat_dir
11+
screenshot_dir = device_logger.screenshot_dir
12+
driver.save_screenshot(os.path.join(screenshot_dir, calling_request + ".png"))
13+
logcat_file = open(os.path.join(logcat_dir, calling_request + "_logcat.log"), 'wb')
14+
logcat_data = driver.get_log('logcat')
15+
for data in logcat_data:
16+
data_string = str(data['timestamp']) + ": " + str(data['message'])
17+
logcat_file.write((data_string + '\n').encode("UTF-8"))
18+
logcat_file.close()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Appium-Python-Client
2+
pytest
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import os
2+
import pytest
3+
4+
from appium import webdriver
5+
6+
# Returns abs path relative to this file and not cwd
7+
PATH = lambda p: os.path.abspath(
8+
os.path.join(os.path.dirname(__file__), p)
9+
)
10+
APPIUM_LOCAL_HOST_URL = 'http://localhost:4723/wd/hub'
11+
PLATFORM_VERSION = '6.0'
12+
13+
14+
class TestWebViewAndroid():
15+
@pytest.fixture(scope="function")
16+
def driver(self, request):
17+
desired_caps = {
18+
'appPackage': 'com.example.android.contactmanager',
19+
'appActivity': '.ContactManager',
20+
'platformName': 'Android',
21+
'platformVersion': PLATFORM_VERSION,
22+
'deviceName': 'Android Emulator',
23+
'app': PATH('../../../../sample-code/apps/ContactManager/ContactManager.apk')
24+
}
25+
driver = webdriver.Remote(APPIUM_LOCAL_HOST_URL, desired_caps)
26+
27+
def fin():
28+
driver.quit()
29+
30+
request.addfinalizer(fin)
31+
return driver # provide the fixture value
32+
33+
def test_add_contacts(self, driver):
34+
el = driver.find_element_by_accessibility_id("Add Contact")
35+
el.click()
36+
37+
textfields = driver.find_elements_by_class_name("android.widget.EditText")
38+
textfields[0].send_keys("Appium User")
39+
textfields[2].send_keys("[email protected]")
40+
41+
assert 'Appium User' == textfields[0].text
42+
assert '[email protected]' == textfields[2].text
43+
44+
driver.find_element_by_accessibility_id("Save").click()
45+
46+
# for some reason "save" breaks things
47+
alert = driver.switch_to_alert()
48+
49+
# no way to handle alerts in Android
50+
driver.find_element_by_android_uiautomator('new UiSelector().clickable(true)').click()
51+
52+
driver.press_keycode(3)
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import os
2+
import pytest
3+
4+
from appium import webdriver
5+
from helpers import take_screenhot_and_logcat
6+
7+
# Returns abs path relative to this file and not cwd
8+
PATH = lambda p: os.path.abspath(
9+
os.path.join(os.path.dirname(__file__), p)
10+
)
11+
APPIUM_LOCAL_HOST_URL = 'http://localhost:4723/wd/hub'
12+
13+
14+
class TestSimpleAndroid():
15+
@pytest.fixture(scope="function")
16+
def driver(self, request, device_logger):
17+
desired_caps = {
18+
'platformName': 'Android',
19+
'platformVersion': '6.0',
20+
'deviceName': 'Android Emulator',
21+
'app': PATH('../../../../sample-code/apps/ApiDemos/bin/ApiDemos-debug.apk')
22+
}
23+
calling_request = request._pyfuncitem.name
24+
driver = webdriver.Remote(APPIUM_LOCAL_HOST_URL, desired_caps)
25+
26+
def fin():
27+
take_screenhot_and_logcat(driver, device_logger, calling_request)
28+
driver.quit()
29+
30+
request.addfinalizer(fin)
31+
return driver # provide the fixture value
32+
33+
def test_find_elements(self, driver):
34+
el = driver.find_element_by_accessibility_id('Graphics')
35+
el.click()
36+
el = driver.find_element_by_accessibility_id('Arcs')
37+
assert el is not None
38+
driver.back()
39+
40+
el = driver.find_element_by_accessibility_id("App")
41+
assert el is not None
42+
43+
els = driver.find_elements_by_android_uiautomator("new UiSelector().clickable(true)")
44+
assert len(els) >= 12
45+
driver.find_element_by_android_uiautomator('text("API Demos")')
46+
47+
def test_simple_actions(self, driver):
48+
el = driver.find_element_by_accessibility_id('Graphics')
49+
el.click()
50+
51+
el = driver.find_element_by_accessibility_id('Arcs')
52+
el.click()
53+
54+
driver.find_element_by_android_uiautomator('new UiSelector().text("Graphics/Arcs")')

0 commit comments

Comments
 (0)