Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save neerajgupta2407/3878f209cb4202b696380b5135e9b965 to your computer and use it in GitHub Desktop.
Save neerajgupta2407/3878f209cb4202b696380b5135e9b965 to your computer and use it in GitHub Desktop.
Code snippet for Appium Python
from appium import webdriver
"""
Desired Capabilities
TODO:
Provide device name, full path of the apk file, app package and app activity
"""
# TODO provide the desired cap as per your requirement
desired_cap ={
"platformName": "Android",
"deviceName": " ",
"app": " ",
"appPackage": " ",
"appWaitActivity": " "
}
# Create the driver instance
driver = webdriver.Remote("http://localhost:4723/wd/hub", desired_cap)
driver.implicitly_wait(30)
# close the popup
driver.find_element_by_id("com.flipkart.android:id/btn_skip").click()
# Get the price of the first laptop and verify the price
driver.find_element_by_xpath("//android.widget.ImageButton[@content-desc='Drawer']").click()
driver.find_element_by_xpath("//android.widget.TextView[@text='Electronics']").click()
driver.find_element_by_xpath("//android.widget.TextView[@text='Laptops']").click()
driver.find_element_by_id("com.flipkart.android:id/tv_card_view_holder").click()
driver.find_element_by_id("com.flipkart.android:id/not_now_button").click()
# Get the price and assert
price = driver.find_element_by_xpath("//android.widget.TextView[@bounds='[226,475][339,509]']").get_attribute("text")
print("The first mobile price value is " +price)
assert price =="₹68,89", "The price is not matched"
for i in range(10):
print(i)
import time
from appium import webdriver
# Setting up desired capabilities
from appium.webdriver.common.touch_action import TouchAction
# TODO provide deice name
desire_cap = {
"platformName": "Android",
"deviceName": "",
"appPackage": "com.android.contacts",
"appActivity": "com.android.contacts.activities.PeopleActivity",
"newCommandTimeout": 600,
}
# Invoke driver
driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", desire_cap)
driver.implicitly_wait(30)
# Allow app to access device data
for i in range(4):
driver.find_element_by_id("com.android.packageinstaller:id/permission_allow_button").click()
time.sleep(2)
# Create a contact
driver.find_element_by_xpath("//android.widget.TextView[@text='Favourites']").click()
user_action = TouchAction(driver)
user_action.tap(x=601, y=1086).perform()
driver.find_element_by_id("android:id/text1").click()
time.sleep(3)
driver.find_element_by_id("com.android.contacts:id/more_fields").click()
"""
Click on phones and mobile dropdown
"""
phone = driver.find_element_by_xpath("//android.widget.EditText[@text='Phone']")
phone.send_keys("4000000000")
driver.hide_keyboard()
time.sleep(3)
driver.find_element_by_xpath("//android.widget.TextView[@text='Mobile']").click()
time.sleep(3)
"""
Interact with list of elements
list of elements_xpath: //android.widget.CheckedTextView[@resource-id='android:id/text1']
Mobile option_xpath: //android.widget.CheckedTextView[@text='Mobile']
"""
# TO-DO
list_of_values = driver.find_elements_by_xpath("//android.widget.CheckedTextView[@resource-id='android:id/text1']")
print(len(list_of_values))
expected_list = ['Mobile', 'Home', 'Work', 'Work Fax', 'Home Fax', 'Pager', 'Other']
actual_list = []
for value in list_of_values:
ele_name = value.get_attribute("text")
print(ele_name)
actual_list.append(ele_name)
print(actual_list)
assert expected_list == actual_list, "The list did not match"
import time
from appium import webdriver
# Setting up desired capabilities
from appium.webdriver.common.touch_action import TouchAction
# TODO provide device name
desire_cap = {
"platformName": "Android",
"deviceName": "",
"appPackage": "com.android.contacts",
"appActivity": "com.android.contacts.activities.PeopleActivity",
"newCommandTimeout": 600,
}
# Invoke driver
driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", desire_cap)
driver.implicitly_wait(30)
# Allow app to access device data
for i in range(4):
driver.find_element_by_id("com.android.packageinstaller:id/permission_allow_button").click()
time.sleep(2)
# Create a contact
driver.find_element_by_xpath("//android.widget.TextView[@text='Favourites']").click()
"""
AUTOMATING MOBILE GESTURES
"""
user_action = TouchAction(driver)
# Tap on the Add icon
"""
Add id co-ordinates: x=601, y=1086
Select PHONE -ID: android:id/text1
More fields - ID: com.android.contacts:id/more_fields
swipe back and forth: co-ord: x=347, y=794 ,x=352, y=391
x=366, y=351, x=354, y=794
name - Xpath: //android.widget.EditText[@text='Name']
"""
user_action.tap(x=601, y=1086).perform()
driver.find_element_by_id("android:id/text1").click()
time.sleep(3)
driver.find_element_by_id("com.android.contacts:id/more_fields").click()
# Sroll down and scroll up
time.sleep(5)
user_action.press(x=347, y=794).move_to(x=352, y=391).release().perform()
time.sleep(5)
user_action.press(x=366, y=351).move_to(x=354, y=794).release().perform()
time.sleep(3)
# Long press
name = driver.find_element_by_xpath("//android.widget.EditText[@text='Name']")
name.send_keys("Appium Python")
time.sleep(3)
user_action.long_press(name, duration=3000).perform()
# hide keyboard
driver.hide_keyboard()
import time
print(time.strftime("%Y_%m_%d_%H%M%S"))
import time
from appium import webdriver
from appium.webdriver.common.touch_action import TouchAction
"""
Desired Capabilities
"""
# TODO provide the desired cap as per your requirement
desired_cap ={
"platformName": "Android",
"deviceName": "",
"app": "",
"appPackage": "",
"appWaitActivity": "",
"newCommandTimeout": 5000
}
# Create the driver instance
driver = webdriver.Remote("http://localhost:4723/wd/hub", desired_cap)
driver.implicitly_wait(30)
# close the popup
driver.find_element_by_id("com.flipkart.android:id/btn_skip").click()
# Get the price of the first laptop and verify the price
driver.find_element_by_xpath("//android.widget.ImageButton[@content-desc='Drawer']").click()
driver.find_element_by_xpath("//android.widget.TextView[@text='Electronics']").click()
driver.find_element_by_xpath("//android.widget.TextView[@text='Laptops']").click()
driver.find_element_by_id("com.flipkart.android:id/tv_card_view_holder").click()
driver.find_element_by_id("com.flipkart.android:id/not_now_button").click()
"""
Touch Actions
TouchAction(driver) .press(x=318, y=972) .move_to(x=338, y=476) .release() .perform()
"""
for i in range(5):
touch = TouchAction(driver)
touch.press(x=318, y=972).move_to(x=318, y=476).release().perform()
time.sleep(3)
import time
from appium import webdriver
from appium.webdriver.common.touch_action import TouchAction
"""
Desired Capabilities
"""
# TODO provide the desired cap as per your requirement
desired_cap ={
"platformName": "Android",
"deviceName": "",
"app": "",
"appPackage": "",
"appWaitActivity": ""
}
# Create the driver instance
driver = webdriver.Remote("http://localhost:4723/wd/hub", desired_cap)
driver.implicitly_wait(30)
# close the popup
driver.find_element_by_id("com.flipkart.android:id/btn_skip").click()
"""
TAKING SCREENSHOTS AND STORING IT WITH CURRENT ACTIVITY AND TIMESTAMP
@driver methods
- current_activity
- save_screenshot
@ Python methods/properties
- strftime
"""
ts = time.strftime("%Y_%m_%d_%H%M%S")
activityname = driver.current_activity
filename = activityname+ts
time.sleep(3)
driver.save_screenshot("/Users/peri/PycharmProjects/AppiumSession/Screenshots/"+filename+".png")
import base64
import os
import time
from appium import webdriver
from appium.webdriver.common.touch_action import TouchAction
"""
Desired Capabilities
"""
# TODO provide the desired cap as per your requirement
desired_cap ={
"platformName": "Android",
"deviceName": "",
"app": "",
"appPackage": "",
"appWaitActivity": ""
}
# Create the driver instance
driver = webdriver.Remote("http://localhost:4723/wd/hub", desired_cap)
driver.implicitly_wait(30)
# start video
driver.start_recording_screen()
# close the popup
driver.find_element_by_id("com.flipkart.android:id/btn_skip").click()
# Get the price of the first laptop and verify the price
driver.find_element_by_xpath("//android.widget.ImageButton[@content-desc='Drawer']").click()
driver.find_element_by_xpath("//android.widget.TextView[@text='Electronics']").click()
driver.find_element_by_xpath("//android.widget.TextView[@text='Laptops']").click()
driver.find_element_by_id("com.flipkart.android:id/tv_card_view_holder").click()
driver.find_element_by_id("com.flipkart.android:id/not_now_button").click()
video_rawdata = driver.stop_recording_screen()
vide_name = driver.current_activity + time.strftime("%Y_%m_%d_%H%M%S")
"""
Video Recording:
@ driver methods
- start_recording_screen()
- stop_recording_screen()
@ Python
- convert Base-64 into media(mp4) file
@ steps:
1. Create filepath (using os.path.join (directory, filename)
2. save the video base64 file from stop recording method
3. Convert base64 into media file (mp4) using with <expression> as <variable>: decorde base64 file
"""
filepath = os.path.join("/Users/peri/PycharmProjects/AppiumSession/Screenshots/", vide_name+".mp4")
with open(filepath,"wb") as vd:
vd.write(base64.b64decode(video_rawdata))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment