繁体   English   中英

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

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

我正在使用Python和Appium制作测试机器人。

我需要提取按钮的电子邮件。 我厌倦了提取href,但是按钮显然不是智能手机应用程序中的其他东西。

所以我点击这个按钮,用“新消息”窗口和“收件人”字段中的电子邮件打开我的gmail。

因此,我进行了调查,发现Java中只有1个托儿所:-(.。

我发现了其他东西。 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

但是它会立即停止浏览器。 AN Pycharm显示此错误:

错误回溯(最近一次调用最近):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”,第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:消息:使用给定的搜索参数无法在页面上找到元素。

我正在使用unittest实例化一个驱动程序去1个应用程序(那里有电子邮件按钮),然后在代码中间实例化一个新驱动程序。 但这很麻烦。 而且我找不到任何有关从1个应用程序切换到其他应用程序的文章或论坛问题。

我更愿意让您测试我的机器人代码:

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)

有人可以帮我吗?

似乎您只需要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

要么

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

并且请尝试不使用新的initilize driver2

请阅读参考资料和this

看起来您在寻找start_activity函数

driver.start_activity方法打开设备上的任意活动。 如果活动不是测试中的应用程序的一部分,它还将启动活动的应用程序

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

这样,您应该能够在同一webdriver实例的范围内切换应用程序

您可能还会发现Launch命令很有用,因为它是跨平台方法,可以Launch任何已安装的应用程序。 可通过SeeTest Appium Extension获得该命令。

暂无
暂无

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

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