简体   繁体   中英

Appium & Python: How to move to another app?

I am making a testing bot with Python and Appium.

I need to extract the email of a button. I tired to extract href, but button are obviously something else than in smartphone applications.

So I click on this button which open my gmail with the New message window and the email in the "To" field.

SO I investigate and I could find only 1 tutoriel in Java :-(.

I found something else. SOmeone propose to instantiate new driver:

driver2 = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps2)
print("we setup driver2")
email = driver2.find_element_by_id("com.google.android.gm:id/to").text

But it stop immediately the browser. ANd Pycharm displayed this error:

Error Traceback (most recent call last): File "C:\\Users\\Nino\\AppData\\Local\\Programs\\Python\\Python37\\lib\\unittest\\case.py", line 59, in testPartExecutor yield File "C:\\Users\\Nino\\AppData\\Local\\Programs\\Python\\Python37\\lib\\unittest\\case.py", line 628, in run testMethod() File "C:\\Users\\Nino\\PycharmProjects\\mybot\\mybot_mybot.py", line 92, in test_scrap_email email = driver2.find_element_by_id("com.google.android.gm:id/to").text File "C:\\Users\\Nino\\PycharmProjects\\mybot\\venv\\lib\\site-packages\\selenium\\webdriver\\remote\\webdriver.py", line 360, in find_element_by_id return self.find_element(by=By.ID, value=id_) File "C:\\Users\\Nino\\PycharmProjects\\mybot\\venv\\lib\\site-packages\\appium\\webdriver\\webdriver.py", line 276, in find_element 'value': value})['value'] File "C:\\Users\\Nino\\PycharmProjects\\mybot\\venv\\lib\\site-packages\\selenium\\webdriver\\remote\\webdriver.py", line 321, in execute self.error_handler.check_response(response) File "C:\\Users\\Nino\\PycharmProjects\\mybot\\venv\\lib\\site-packages\\ap pium\\webdriver\\errorhandler.py", line 29, in check_response raise wde File "C:\\Users\\Nino\\PycharmProjects\\mybot\\venv\\lib\\site-packages\\appium\\webdriver\\errorhandler.py", line 24, in check_response super(MobileErrorHandler, self).check_response(response) File "C:\\Users\\Nino\\PycharmProjects\\mybot\\venv\\lib\\site-packages\\selenium\\webdriver\\remote\\errorhandler.py", line 242, in check_response raise exception_class(message, screen, stacktrace) selenium.common.exceptions.NoSuchElementException: Message: An element could not be located on the page using the given search parameters.

I am using unittest which instantiate one driver going to 1 app (where there si the email button), then I instantiate in the middle of the code a new driver. But it bugs. And Icannot find anywhere an article or forum question about switching from 1 app to other app.

I prefer to let you te code of my bot:

from datetime import time
from time import sleep

from appium import webdriver
import unittest

from selenium.webdriver.common.by import By


class apptotest1(unittest.TestCase):
    def setUp(self):
        desired_caps = {}
        desired_caps['platformName']='Android'
        desired_caps['platformVersion']='6.0'
        desired_caps['deviceName']='S6S5IN3G'
        desired_caps['noReset']='true'
        desired_caps['appPackage']='com.apptotest1'
        desired_caps['appActivity']=' com.apptotest1.android/com.apptotest1.android.activity.MainTabActivity'

        self.driver = webdriver.Remote('http://localhost:4723/wd/hub',desired_caps)
        #self.driver = webdriver.Remote('http://0.0.0.0:4723/wd/hub',desired_caps)

    def tearDown(self):
        self.driver.quit()

    def test_scrap_email(self):
        search_button = self.driver.find_element(By.XPATH,"//android.widget.ImageView[@bounds='[126,800][162,836]']")
        #search_button = self.driver.find_element(By.XPATH ("//android.widget.ImageView[@content-desc='Rechercher et explorer']"))
        if search_button:
            print("search_button was found!")
            search_button.click()

        else:
            print("search_button was not found :-(")

        search_field = self.driver.find_element_by_id('com.apptotest1.android:id/action_bar_search_edit_text')
        search_field.send_keys('marketing')
        users_button = self.driver.find_element_by_id('com.apptotest1.android:id/tab_button_fallback_icon')
        if users_button:
            print("users_button was found!")
            users_button.click()


        else:
            print("users_button was not found :-(")


        users_button2 = self.driver.find_element(By.XPATH, "//android.widget.ImageView[@bounds='[162,123][198,159]']")

        if users_button2:
            print("users_button2 was found!")
            users_button2.click()

        else:
            print("users_button2 was not found :-(")

        sleep(5)
        profile_test = self.driver.find_elements_by_id("com.apptotest1.android:id/row_search_user_username")[1]
        if profile_test:
            print("profile_test was found!")
            profile_test.click()


        else:
            print("profile_test was not found :-(")

        sleep(5)

        button_email = self.driver.find_element(By.XPATH,"//android.widget.TextView[@text='Adresse e-mail']")
        if button_email:
            print("button_email was found!")

            button_text = button_email.text
            print("button_text is :" + str(button_text))
            button_email.click()

        else:
            print("button_email was not found :-(")



        desired_caps2 = {}
        desired_caps2['platformName'] = 'Android'
        desired_caps2['platformVersion'] = '6.0'
        desired_caps2['deviceName'] = 'S6S5IN3G'
        desired_caps2['noReset'] = 'true'
        desired_caps2['appPackage'] = 'com.google.android.gm'
        desired_caps2['appActivity'] = ' com.google.android.gm.ComposeActivityGmailExternal'

        driver2 = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps2)
        print("we setup driver2")
        email = driver2.find_element_by_id("com.google.android.gm:id/to").text
        sleep(10)
        if email:
            print("email was found!")
            print("Es eso que querias :-) =>" + str(email))



        else:
            print("Email was not found :-(")


        sleep(5)





if __name__ == '__main__':
    suite = unittest.Testloader().loadTestsFromTestCase(apptotest1)
    unittest.TextTestRunner(verbosity=1).run(suite)

Does anyone can help me please?

It seems like you just need switch context , you facing web in gmail , try :

driver2 = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps2)
print("we setup driver2")
driver2.switch_to.context('WEBVIEW_1')
email = driver2.find_element_by_id("com.google.android.gm:id/to").text

Or

# switch to webview
webview = driver.contexts.last
driver.switch_to.context(webview)

And please try without new initilize driver2

Please read this reference and this .

It looks like you're looking for start_activity function

The driver.start_activity method opens arbitrary activities on a device. If the activity is not part of the application under test, it will also launch the activity's application .

 driver.start_activity('com.foo.app', '.MyActivity') 

This way you should be able to switch between applications within the bounds of the same webdriver instance

You might also find Launch command useful as it is cross-platform approach allowing kicking off any installed application. The command is available via SeeTest Appium Extension .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM