import unittest, time from appium import webdriver from appium.webdriver.common.touch_action import TouchAction class TestVK(unittest.TestCase): def find(self, name): return self.driver.find_element_by_name(name) def findAndroidElem(self, elId): return self.driver.find_element_by_android_uiautomator('resourceId("android:id/' + elId + '")') def id(self, elId): return self.driver.find_element_by_android_uiautomator('resourceId("' + self.package + ':id/' + elId + '")') def typeIn(self, el, text): el.send_keys(text) def tap(self, el): action = TouchAction(self.driver) action.tap(el).perform() def getValue(self, el): return el.get_attribute('text') def loginFromStartScreen(self, user, password): # Moving to Log In Screen auth_signup_btn = self.id('auth_login_btn') self.tap(auth_signup_btn) # Trying to fill fields 'Login' and 'Password' auth_login = self.id('auth_login') auth_pass = self.id('auth_pass') auth_button = self.id('auth_btn') self.typeIn(auth_login, user) self.typeIn(auth_pass, password) # Trying to log in self.tap(auth_button) def homeButton(self): self.driver.press_keycode(3) def setUp(self): desired_caps = {} desired_caps['platformName'] = 'Android' desired_caps['platformVersion'] = '4.4.4' desired_caps['deviceName'] = 'Android Emulator' desired_caps['app'] = 'C:\\VK.apk' self.login = '89050321853' self.password = 'karandash' self.package = 'com.vkontakte.android' self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps) def tearDown(self): self.driver.quit() def testNewStatus(self): self.loginFromStartScreen(self.login, self.password) newpost = self.id('newpost') self.tap(newpost) status = self.id('status_text_edit') self.typeIn(status, 'Hello, world!') post = self.id('ab_done_text') self.tap(post) helloworld = self.find('Hello, world!') self.assertIsNotNone(helloworld) def testSearchClub(self): self.loginFromStartScreen(self.login, self.password) nav = self.findAndroidElem('up') self.tap(nav) search_btn = self.id('left_quick_search_btn') self.tap(search_btn) search = self.id('quick_search_box') self.typeIn(search, 'club1') time.sleep(2) group = self.find('Group, vk.com/club1') self.assertIsNotNone(group) def testAuthWorks(self): self.loginFromStartScreen(self.login, self.password) time.sleep(5) news = self.find("News") self.assertIsNotNone(news) def testIncorrectAuthWorks(self): self.loginFromStartScreen('abc', 'abc') dialog_message = self.findAndroidElem('message') self.assertEqual('An error occurred while trying to log in. Please check your login and password and try again. (Error: invalid_client / Username or password is incorrect)', self.getValue(dialog_message)) def testWidgetInstall(self): self.homeButton() apps = self.driver.find_element_by_android_uiautomator('new UiSelector().description("Apps").instance(0)') self.tap(apps) self.tap(self.find('Apps')) self.tap(self.find('Widgets')) els = self.driver.find_elements_by_class_name('android.widget.GridLayout') action = TouchAction(self.driver) action.press(els[0], x=500, y=500).move_to(x=-300,y=0).release().perform() time.sleep(1) action.press(els[0], x=500, y=500).move_to(x=-300,y=0).release().perform() time.sleep(1) action.press(els[0], x=500, y=500).move_to(x=-300,y=0).release().perform() time.sleep(1) action.long_press(self.find("VK – music (big)")).move_to(x=216,y=600).release().perform() time.sleep(2) self.tap( self.find('Tap to open your music') ) time.sleep(2) mymusic = self.find('My music') self.homeButton() time.sleep(1) tapmusic = self.find('Tap to open your music') action.long_press(tapmusic).move_to(x=0,y=100).release().perform() self.assertIsNotNone(mymusic) if __name__ == '__main__': unittest.main()