簡體   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