Thursday 20 May 2021

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 the same. Its part of good practice to include in your test framework for each scenarios. Later point of time we will discuss about how to accommodate same in your framework.

Let us see how we can capture screenshot

1. Get the current activity name

2. Get the current time stamp

3. capture and save the screenshot at specified location

import pytest
from appium import webdriver
from login import Login
import time
import os
import base64
from calculator_app import Calculator


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

def test_calculator_add_number():
desired_caps = {
'platformName': 'android',
'udid': 'emulator-5556',
'deviceName': 'Pixel 2 API 28',
'platformVersion': '9.0',
'appPackage': 'com.android.calculator2',
'appActivity': 'com.android.calculator2.Calculator'
}
driver = webdriver.Remote("http://localhost:4723/wd/hub", desired_caps)
cal = Calculator(driver)
cal.add_numbers(2, 2)
file_name = driver.current_activity + time.strftime("%Y_%m_%d_%H%M%S")
filepath = os.path.join("C:/Data/2021/May/Sample/", file_name + ".png")
driver.save_screenshot(filepath)


This the result from above script.






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...