简体   繁体   English

Appium和Python:如何移至另一个应用程序?

[英]Appium & Python: How to move to another app?

I am making a testing bot with Python and Appium. 我正在使用Python和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. 我厌倦了提取href,但是按钮显然不是智能手机应用程序中的其他东西。

So I click on this button which open my gmail with the New message window and the email in the "To" field. 所以我点击这个按钮,用“新消息”窗口和“收件人”字段中的电子邮件打开我的gmail。

SO I investigate and I could find only 1 tutoriel in Java :-(. 因此,我进行了调查,发现Java中只有1个托儿所:-(.。

I found something else. 我发现了其他东西。 SOmeone propose to instantiate new driver: SOmeone建议实例化新驱动程序:

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: AN Pycharm显示此错误:

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 错误回溯(最近一次调用最近):testPartExecutor中的文件“ C:\\ Users \\ Nino \\ AppData \\ Local \\ Programs \\ Python \\ Python37 \\ lib \\ unittest \\ case.py”第59行产生文件“ C:\\ Users \\ Nino \\ AppData \\ Local \\ Programs \\ Python \\ Python37 \\ lib \\ unittest \\ case.py”,行628,在运行testMethod()文件“ C:\\ Users \\ Nino \\ PycharmProjects \\ mybot \\ mybot_mybot.py”,行92,在test_scrap_email中的电子邮件中= email2.find_element_by_id(“ com.google.android.gm:id/to”)。text文件“ C:\\ Users \\ Nino \\ PycharmProjects \\ mybot \\ venv \\ lib \\ site-packages \\ selenium \\ webdriver \\ remote \\ webdriver.py“,第360行,在find_element_by_id中返回self.find_element(by = By.ID,值= id_)文件“ C:\\ Users \\ Nino \\ PycharmProjects \\ mybot \\ venv \\ lib \\ site-packages \\ appium \\ webdriver \\ webdriver.py“,第276行,位于find_element'value':value})['value']文件“ C:\\ Users \\ Nino \\ PycharmProjects \\ mybot \\ venv \\ lib \\ site-packages \\ Selenium \\ webdriver \\ remote \\执行self.error_handler.check_response(response)文件“ C:\\ Users \\ Nino \\ PycharmProjects \\ mybot \\ venv \\ lib \\ site-packages \\ ap”中的第321行 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. pium \\ webdriver \\ errorhandler.py”,第29行,在check_response中,提高wde文件“ C:\\ Users \\ Nino \\ PycharmProjects \\ mybot \\ venv \\ lib \\ site-packages \\ appium \\ webdriver \\ errorhandler.py”,第24行,在check_response超级(MobileErrorHandler,自身)。check_response(响应)文件“ C:\\ Users \\ Nino \\ PycharmProjects \\ mybot \\ venv \\ lib \\ site-packages \\ selenium \\ webdriver \\ remote \\ errorhandler.py”,第242行,在check_response引发exception_class(消息,屏幕,堆栈跟踪)selenium.common.exceptions.NoSuchElementException:消息:使用给定的搜索参数无法在页面上找到元素。

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. 我正在使用unittest实例化一个驱动程序去1个应用程序(那里有电子邮件按钮),然后在代码中间实例化一个新驱动程序。 But it bugs. 但这很麻烦。 And Icannot find anywhere an article or forum question about switching from 1 app to other app. 而且我找不到任何有关从1个应用程序切换到其他应用程序的文章或论坛问题。

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 : 似乎您只需要switch context ,您就可以使用gmail面对网络,请尝试:

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 并且请尝试不使用新的initilize driver2

Please read this reference and this . 请阅读参考资料和this

It looks like you're looking for start_activity function 看起来您在寻找start_activity函数

The driver.start_activity method opens arbitrary activities on a device. driver.start_activity方法打开设备上的任意活动。 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 这样,您应该能够在同一webdriver实例的范围内切换应用程序

You might also find Launch command useful as it is cross-platform approach allowing kicking off any installed application. 您可能还会发现Launch命令很有用,因为它是跨平台方法,可以Launch任何已安装的应用程序。 The command is available via SeeTest Appium Extension . 可通过SeeTest Appium Extension获得该命令。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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