Thursday 20 May 2021

Python Appium - Parallel Test execution on mobile devices

 Why there is requirement for Parallel execution of test script on multiple devices?

Having single script and executing them parallel on multiple devices can actually reduce the efforts, resources and mainly the "TIME".

So let us start with parallel test execution script. first we would need below package. For info refer https://pypi.org/project/pytest-xdist/

pip install pytest-xdist

Once the package is installed, we can make sure that our test will run on multiple devices in parallel

For that I have considered pytest framework. Lets start the code

@pytest.mark.parametrize("udid,platformVersion,deviceName,systemPort,",[('emulator-5554','7.0','Pixel API 24','8201'),('emulator-5556','7.0','Pixel 3 API 24','8202')]) 

Basically you have to use parameterize annotation here to pass multiple device info, which it will pick at the time of creation of thread. Two parameters means two threads will be created for parallel execution.

import pytest
from appium import webdriver
from pageObject.calculator.calculator_app import Calculator
from utilities.BaseClass import BaseClass


def __init__(self, driver):
self.driver = driver


@pytest.mark.parametrize("udid,platformVersion,deviceName,systemPort,",[('emulator-5554',
'7.0','Pixel API 24','8201'),('emulator-5556','7.0','Pixel 3 API 24','8202')])
def test_calculator_add_number(udid, platformVersion, deviceName, systemPort):
desired_caps = {
'platformName': 'android',
'udid': udid,
'deviceName': deviceName,
'platformVersion': platformVersion,
'appPackage': 'com.android.calculator2',
'appActivity': 'com.android.calculator2.Calculator',
'systemPort': int(systemPort)
}
driver = webdriver.Remote("http://localhost:4723/wd/hub", desired_caps)
cal = Calculator(driver)
cal.add_numbers(2, 2)

In above code you can pass as many as device details that will run in parallel. But make sure you pass unique system port.

To run this you have to give below command




No comments:

Post a Comment

Feature Posts

Python Appium - Step by step procedure for capturing screenshot

 Why To capture screenshot? It is as important as your logs. If there is any failure for any test scenario, we can provide screenshot for th...