[Python] No internet connection in android app after starting new test (mitmproxy + Appium)

Discussão em 'Python' iniciado por Stack, Janeiro 22, 2025.

  1. Stack

    Stack Membro Participativo

    I am encountering an internet connection error in the Android application when running tests using Appium + mitmproxy. The first test runs successfully, but subsequent tests fail due to the internet connection error. I think that the proxy server cannot start properly after the first test is finished. Could you please advise what the issue might be and how to fix it? I'm using mitmproxy version 7.0.4. I apologize for any possible mistakes, English is not my native language.

    import unittest
    import asyncio
    from appium import webdriver
    from threading import Thread


    HOST = '0.0.0.0'

    def loop_in_thread(loop, proxy):
    asyncio.set_event_loop(loop)
    proxy.run_loop(loop.run_forever)

    сlass BaseTest(unittest.TestCase):

    def setUp(self, addon=Addon()):
    self.start_proxy(addon)
    desired_capabilities = {'platformName': 'Android',
    'automationName': 'UiAutomator2',
    'udid': '<device udid>'
    'app': '<path to apk>',
    'appWaitActivity': '<LauncherActivity>',
    'waitForIdleTimeout': 200,
    'clearDeviceLogsOnStart': True
    }
    self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_capabilities, keep_alive=False)
    sleep(2)

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


    def start_proxy(self, addon):


    options = Options(listen_host=HOST, listen_port=<proxy port>,
    ignore_hosts=['google.com','googleapis.com'],
    ssl_insecure=True)

    try:
    self.loop = asyncio.get_event_loop()
    if self.loop.is_closed():
    self.loop = asyncio.new_event_loop()
    asyncio.set_event_loop(self.loop)
    except RuntimeError:
    self.loop = asyncio.new_event_loop()
    asyncio.set_event_loop(self.loop)

    self.master = DumpMaster(options, with_termlog=False, with_dumper=False)

    self.master.addons.add(addon)

    self.proxy_thread = Thread(target=loop_in_thread, args=(self.loop, self.master))
    self.proxy_thread.start()

    def stop_proxy(self):
    if self.master is not None:
    self.master.shutdown()
    self.proxy_thread.join()
    self.master = None

    Continue reading...

Compartilhe esta Página