Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
anamhira47 committed Aug 31, 2024
1 parent baf92ee commit ea2d3b5
Show file tree
Hide file tree
Showing 14 changed files with 986 additions and 225 deletions.
36 changes: 22 additions & 14 deletions cookbook/examplescript.py
Original file line number Diff line number Diff line change
@@ -1,50 +1,58 @@
import asyncio
import base64
from mobileadapt import mobileadapt
from datetime import datetime
from PIL import Image
import io
import os
from datetime import datetime

from loguru import logger
''' From the root directory use the following command to start the script:
from PIL import Image

from mobileadapt import mobileadapt

""" From the root directory use the following command to start the script:
python example-scripts/examplescript.py
'''
"""


async def save_screenshot(screenshot_data, filename):
# Open the screenshot data as an image and save it
image = Image.open(io.BytesIO(screenshot_data))
image.save(filename)


async def main():
# Create an Android device instance
android_device = mobileadapt(platform="android")

# Initialize the device (starts the Appium session)
await android_device.start_device()

# Get the current state of the device
encoded_ui, screenshot, ui = await android_device.get_state()
logger.info(f"Current state: {encoded_ui}")

# Save the first screenshot
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
# filename1 = os.path.join(os.path.dirname(__file__), f"screenshot_before_{timestamp}.png")
#await save_screenshot(screenshot, filename1)
# await save_screenshot(screenshot, filename1)
# print(f"Screenshot saved as {filename1}")

# Perform a tap action at coordinates (100, 100)
await android_device.tap(100, 100)

# Get the state again after the tap action
new_encoded_ui, new_screenshot, new_ui = await android_device.get_state()
print("New state after tap:", new_encoded_ui)

# Save the second screenshot
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
filename2 = os.path.join(os.path.dirname(__file__), f"screenshot_after_{timestamp}.png")
filename2 = os.path.join(
os.path.dirname(__file__), f"screenshot_after_{timestamp}.png"
)
await save_screenshot(new_screenshot, filename2)
print(f"Screenshot saved as {filename2}")


if __name__ == "__main__":
# Run the main function asynchronously
asyncio.run(main())
asyncio.run(main())
26 changes: 19 additions & 7 deletions cookbook/examplescript2.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
import asyncio
import io
import os
from datetime import datetime

from PIL import Image
import io

from mobileadapt import mobileadapt


async def save_screenshot(screenshot_data, filename):
image = Image.open(io.BytesIO(screenshot_data))
image.save(filename)


async def perform_actions(device):
# Tap actions
await device.tap(200, 300)
print("Tapped at (200, 300)")
await device.tap(100, 400)
print("Tapped at (100, 400)")

# Swipe actions
await device.swipe("up")
print("Swiped up")
Expand All @@ -30,14 +34,17 @@ async def perform_actions(device):
await device.input(150, 500, "Hello, MobileAdapt!")
print("Input text at (150, 500)")


async def main():
android_device = mobileadapt(platform="android")
await android_device.start_device()

# Perform initial state capture
encoded_ui, screenshot, ui = await android_device.get_state()
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
filename = os.path.join(os.path.dirname(__file__), f"screenshot_initial_{timestamp}.png")
filename = os.path.join(
os.path.dirname(__file__), f"screenshot_initial_{timestamp}.png"
)
await save_screenshot(screenshot, filename)
print(f"Initial screenshot saved as {filename}")
print("Initial UI state:", encoded_ui)
Expand All @@ -46,10 +53,12 @@ async def main():
for i in range(3):
print(f"\nPerforming action set {i+1}")
await perform_actions(android_device)

encoded_ui, screenshot, ui = await android_device.get_state()
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
filename = os.path.join(os.path.dirname(__file__), f"screenshot_action{i+1}_{timestamp}.png")
filename = os.path.join(
os.path.dirname(__file__), f"screenshot_action{i+1}_{timestamp}.png"
)
await save_screenshot(screenshot, filename)
print(f"Screenshot after action set {i+1} saved as {filename}")
print(f"UI state after action set {i+1}:", encoded_ui)
Expand All @@ -65,10 +74,13 @@ async def main():
# Capture final state
encoded_ui, screenshot, ui = await android_device.get_state()
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
filename = os.path.join(os.path.dirname(__file__), f"screenshot_final_{timestamp}.png")
filename = os.path.join(
os.path.dirname(__file__), f"screenshot_final_{timestamp}.png"
)
await save_screenshot(screenshot, filename)
print(f"Final screenshot saved as {filename}")
print("Final UI state:", encoded_ui)


if __name__ == "__main__":
asyncio.run(main())
asyncio.run(main())
17 changes: 14 additions & 3 deletions mobileadapt/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
from .core import MobileAdapt
from .device.device_factory import DeviceFactory

def mobileadapt(platform: str, app_url: str = None, state_representation='aria', download_directory='default', session_id=None):
return DeviceFactory.create_device(platform, app_url, state_representation, download_directory, session_id)

__all__ = ['mobileadapt']
def mobileadapt(
platform: str,
app_url: str = None,
state_representation="aria",
download_directory="default",
session_id=None,
):
return DeviceFactory.create_device(
platform, app_url, state_representation, download_directory, session_id
)


__all__ = ["mobileadapt", "MobileAdapt"]
File renamed without changes.
Loading

0 comments on commit ea2d3b5

Please sign in to comment.