Skip to content

Instantly share code, notes, and snippets.

@vrunoa
Last active August 14, 2019 02:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vrunoa/05c6c5d1e89617695ec0fea5781ac16c to your computer and use it in GitHub Desktop.
Save vrunoa/05c6c5d1e89617695ec0fea5781ac16c to your computer and use it in GitHub Desktop.
import json
import sys
import os
import re
import unittest
from appium import webdriver
from time import sleep
from os.path import basename
import os
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from datetime import datetime
from datetime import timedelta
class MobileShellTest(unittest.TestCase):
def setUp(self):
capabilities = {}
capabilities["platformName"] = "Android"
capabilities["platformVersion"] = "8.0"
capabilities["deviceName"] = "Google Pixel GoogleApi Emulator"
capabilities["app"] = "https://www.dropbox.com/s/1rtoxhmldayz3z2/android-emulator-test-app-1.0.9-normal-debug.apk?dl=1"
capabilities["appPackage"] = "com.saucelabs.android.emulators.test.app"
capabilities["appActivity"] = ".activities.MainActivity"
capabilities["automationName"] = "uiautomator2"
capabilities["appiumVersion"] = "1.13.0"
# host = "http://localhost:4723/wd/hub"
host = "http://%s:%s@ondemand.saucelabs.com:80/wd/hub" % (os.environ["SAUCE_USERNAME"], os.environ["SAUCE_ACCESS_KEY"])
self.driver = webdriver.Remote(host, capabilities)
def tearDown(self):
self.driver.quit()
def test_mobile(self):
# close notification bar
self.driver.execute_script(
"mobile: shell",
{"command": "service call statusbar 2"}
)
# clear notification bar
self.driver.execute_script(
"mobile: shell",
{"command": "service call notification 1"}
)
# send broadcast to launch the notification
self.driver.execute_script(
"mobile: shell",
{"command": "am broadcast -n com.saucelabs.android.emulators.test.app/.services.NotificationReceiver"}
)
sleep(2)
# move app to background
self.driver.execute_script(
"mobile: shell",
{"command": "input keyevent 3"}
)
sleep(1)
# open notifiation bar
self.driver.execute_script(
"mobile: shell",
{"command": "service call statusbar 1"}
)
sleep(1)
el = self.driver.find_element_by_id('com.android.systemui:id/notification_stack_scroller')
els = el.find_elements_by_id('android:id/app_name_text')
notification_index = None
# find the app notification
for i in range(0, len(els)):
if els[i].text == 'AndroidEmulatorsTestApp':
notification_index = i
if notification_index is None:
raise "Couldnt find notification"
els = self.driver.find_elements_by_id('android:id/expand_button')
el = els[notification_index]
el.click()
command = 'new UiSelector().text("Message from: SauceLabs").className("android.widget.TextView")'
el = self.driver.find_element_by_android_uiautomator(command)
self.assertIsNotNone(el)
command = 'new UiSelector().textContains("Hey... did you").className("android.widget.TextView")'
el = self.driver.find_element_by_android_uiautomator(command)
self.assertIsNotNone(el)
self.assertEquals(el.text, 'Hey... did you know we enabled ADB commands on our Android Emulators? Arent we Saucesome?')
# clicking on the notification
el.click()
sleep(1)
#$ making sure it opened the right activity on our app
self.assertEquals(self.driver.current_activity, ".activities.NotificationTestActivity")
sleep(2)
def main():
suite = unittest.TestLoader().loadTestsFromTestCase(MobileShellTest)
ret = not unittest.TextTestRunner(verbosity=3).run(suite).wasSuccessful()
sys.exit(ret)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment