Skip to content

Commit 69e41cb

Browse files
Merge pull request #4 from dmberezovskyii/PPA-0003
added driver configuration
2 parents 3b02505 + 6551ffa commit 69e41cb

8 files changed

Lines changed: 106 additions & 23 deletions

File tree

config.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,5 @@
99
load_dotenv=True,
1010
envvar_prefix="APPIUM",
1111
env_switcher="ENV_FOR_APPIUM",
12-
dotenv_path="configs/.env", # Enable env switcher
12+
dotenv_path="configs/.env", # Enable env switcher
1313
)
14-
15-
16-
print(settings.APPIUM_SERVER)

config/settings.yaml

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
1-
default:
2-
DEBUG: True
3-
APPIUM_SERVER: "http://localhost:4723/wd/hub"
4-
51
stage:
62
DEBUG: True
7-
APPIUM_SERVER: "http://stage-address:4723/wd/hub"
3+
APPIUM_SERVER: "http://127.0.0.1:4723/wd/hub"
84
ANDROID:
95
platformName: "Android"
106
deviceName: "emulator"
11-
app: ""
127
platformVersion: "15"
8+
newCommandTimeout: 3000
9+
autoGrantPermissions: True
10+
appWaitForLaunch: True
11+
maxRetryCount: 40
12+
noReset: True
13+
appWaitDuration: 30000
14+
appPackage: "io.appium.android.apis"
15+
appActivity: "io.appium.android.apis.ApiDemos"
1316
automationName: "uiautomator2"
17+
1418
IOS:
1519
platformName: "iOS"
1620
deviceName: "iPhone 14"
17-
bundleId: "com.example.app"
21+
bundleId: "ua.com.test.app"
1822
automationName: "XCUITest"
1923

2024
prod:

conftest.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import pytest
2+
3+
from drivers.driver_factory import Driver
4+
5+
6+
@pytest.hookimpl
7+
def pytest_addoption(parser):
8+
"""
9+
Adds command-line options for pytest.
10+
"""
11+
parser.addoption(
12+
"--app", action="store", default="ios", help="Define App: ios or android"
13+
)
14+
parser.addoption(
15+
"--device", action="store", default="emulator", help="Define Device Type"
16+
)
17+
parser.addoption(
18+
"--platform", action="store", default="ios", help="Define Platform"
19+
)
20+
parser.addoption(
21+
"--env",
22+
action="store",
23+
default="stage",
24+
help="Define test environment (e.g., stage, prod)",
25+
)
26+
parser.addoption(
27+
"--listeners",
28+
action="store",
29+
default="events",
30+
help="Define listeners for the test run",
31+
)
32+
33+
34+
@pytest.fixture(scope="session")
35+
def app(request):
36+
"""
37+
Retrieves the application type specified via the --app command-line option.
38+
"""
39+
return request.config.getoption("--app")
40+
41+
42+
@pytest.fixture(scope="session")
43+
def device(request):
44+
"""
45+
Retrieves the device type specified via the --device command-line option.
46+
"""
47+
return request.config.getoption("--device")
48+
49+
50+
@pytest.fixture(scope="function")
51+
def driver(request):
52+
platform = request.config.getoption("--platform")
53+
try:
54+
driver = Driver.get_driver(platform)
55+
except Exception as e:
56+
pytest.fail(f"Failed to initialize driver: {e}")
57+
58+
yield driver
59+
if driver is not None:
60+
driver.quit()

pyproject.toml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ python = "^3.12"
1616
pytest = "^8.3.4"
1717
dynaconf = "^3.2.10"
1818
requests = "^2.31"
19-
pytest-asyncio = "^0.24.0"
2019
pytest-html = "^4.1.1"
2120
Appium-Python-Client = "^4.5.1"
2221

@@ -28,8 +27,8 @@ requires = ["poetry-core>=1.0.0"]
2827
build-backend = "poetry.core.masonry.api"
2928

3029
[tool.pytest.ini_options]
31-
addopts = "-rA -v --env=stage --device-type=android --listeners=events --capture=no -p no:cacheprovider --html=../reports/test_report.html --self-contained-html"
32-
#asyncio_mode = "auto"
30+
addopts = "-rA -v --env=stage --platform=android --device=emulator --listeners=events --capture=no -p no:cacheprovider --html=../reports/test_report.html --self-contained-html"
31+
asyncio_default_fixture_loop_scope = "function"
3332
markers = [
3433
{ name = "smoke", description = "run smoke tests" },
3534
{ name = "regression", description = "run regression tests" }

src/drivers/android_driver.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ def get_caps():
1212
if not caps:
1313
raise ValueError("❌ ANDROID capabilities not found in settings.yaml")
1414

15-
caps["app"] = str(Path(__file__).resolve().parents[2] / "data/apps/app.apk")
15+
caps["app"] = str(Path(__file__).resolve().parents[2] / "data/apps/demo.apk")
1616

1717
return caps

src/drivers/driver_factory.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,29 @@
1+
from appium.options.android import UiAutomator2Options
2+
from appium.options.ios import XCUITestOptions
13
from appium import webdriver
24

35
from config import settings
4-
from src.drivers.android_driver import AndroidCaps
6+
from drivers.android_driver import AndroidCaps
7+
from drivers.ios_driver import IOSCaps
58

69

710
class Driver:
811
@staticmethod
912
def get_driver(platform: str):
1013
"""Get driver by platform, uses appropriate capabilities for Android or iOS."""
14+
caps = (
15+
AndroidCaps.get_caps()
16+
if platform.lower() == "android"
17+
else IOSCaps.get_caps()
18+
)
19+
20+
if not caps:
21+
raise ValueError(f"Capabilities not found for platform ❌: {platform}")
22+
1123
if platform.lower() == "android":
12-
caps = AndroidCaps.get_caps()
24+
options = UiAutomator2Options().load_capabilities(caps)
1325
else:
14-
caps = settings.iOS.to_dict()
26+
options = XCUITestOptions().load_capabilities(caps)
1527

16-
driver = webdriver.Remote(settings.APPIUM_SERVER, caps)
28+
driver = webdriver.Remote(settings.APPIUM_SERVER, options=options)
1729
return driver

src/drivers/ios_driver.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
1-
from appium.webdriver import webdriver
1+
from pathlib import Path
22

33
from config import settings
44

55

6-
class iOSDriver:
6+
class IOSCaps:
77
@staticmethod
8-
def get_driver(platform: str):
9-
return settings.iOS.to_dict()
8+
def get_caps():
9+
"""Generate and return Android capabilities, with adding dynamic 'app' path."""
10+
caps = settings.iOS.to_dict()
11+
12+
if not caps:
13+
raise ValueError("❌ ANDROID capabilities not found in settings.yaml")
14+
15+
caps["app"] = str(Path(__file__).resolve().parents[2] / "data/apps/demo.ipa")
16+
17+
return caps

tests/test_p1/test_p1.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Test1:
2+
def test_1(self, driver):
3+
pass

0 commit comments

Comments
 (0)