Switching programming languages. Running into issues connecting to device

Looking for some help with this one. I am switching from Ruby to Python to test a few things and I am running into issues connecting to my device. I am using a physical device over wifi

Here is the ruby code I am using successfully:

require ‘io/console’
require ‘appium_lib’
require ‘./navigation.rb’

def main
build_args

desired_caps = {
host: ‘192.168.1.119’,
port: 5555,
caps: {
platformName: “Android”,
platformVersion: “7.1.1”,
deviceName: “0815f89369ea0604”,
appPackage: “com.testapp.test”,
appActivity: “com.tns.NativeScriptActivity”,
}

  }

$driver = Appium::Driver.new(desired_caps, true)
puts “Establishing connection and waiting for app to initialize…”
$driver.start_driver
sleep(45)

login_to_device

side_bar_navigate_to(“Sign Out”)
signout = $driver.find_element(:id, “android:id/button1”)
signout.click

finish_tests

end

this works well and I am having no issues with it.
When I try to do something similar via python I get a large error message pretty much right away:

python pythontest\ .py
test_find_elements (main.SimpleAndroidTests)
Login Test … ERROR

======================================================================
ERROR: test_find_elements (main.SimpleAndroidTests)
Login Test

Traceback (most recent call last):
File “C:\Users\ryand\AppData\Local\Programs\Python\Python37-32\lib\site-packages\urllib3\connectionpool\ .py”, line 600, in urlopen
chunked=chunked)
File “C:\Users\ryand\AppData\Local\Programs\Python\Python37-32\lib\site-packages\urllib3\connectionpool\ .py”, line 384, in _make_request
six.raise_from(e, None)
File “”, line 2, in raise_from
File “C:\Users\ryand\AppData\Local\Programs\Python\Python37-32\lib\site-packages\urllib3\connectionpool\ .py”, line 380, in _make_request
httplib_response = conn.getresponse()
File “C:\Users\ryand\AppData\Local\Programs\Python\Python37-32\lib\http\client\ .py”, line 1321, in getresponse
response.begin()
File “C:\Users\ryand\AppData\Local\Programs\Python\Python37-32\lib\http\client\ .py”, line 296, in begin
version, status, reason = self._read_status()
File “C:\Users\ryand\AppData\Local\Programs\Python\Python37-32\lib\http\client\ .py”, line 265, in _read_status
raise RemoteDisconnected(“Remote end closed connection without”
http.client.RemoteDisconnected: Remote end closed connection without response

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File “pythontest\ .py”, line 29, in setUp
self.driver = webdriver.Remote(‘192.168.1.119:5555’, desired_caps)
File “C:\Users\ryand\AppData\Local\Programs\Python\Python37-32\lib\site-packages\appium\webdriver\webdriver\ .py”, line 97, in init
super(WebDriver, self).init(command_executor, desired_capabilities, browser_profile, proxy, keep_alive)
File “C:\Users\ryand\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webdriver\ .py”, line 156, in init
self.start_session(capabilities, browser_profile)
File “C:\Users\ryand\AppData\Local\Programs\Python\Python37-32\lib\site-packages\appium\webdriver\webdriver\ .py”, line 136, in start_session
response = self.execute(RemoteCommand.NEW_SESSION, parameters)
File “C:\Users\ryand\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webdriver\ .py”, line 318, in execute
response = self.command_executor.execute(driver_command, params)
File “C:\Users\ryand\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\remote_connection\ .py”, line 375, in execute
return self._request(command_info[0], url, body=data)
File “C:\Users\ryand\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\remote_connection\ .py”, line 402, in _request
resp = http.request(method, url, body=body, headers=headers)
File “C:\Users\ryand\AppData\Local\Programs\Python\Python37-32\lib\site-packages\urllib3\request\ .py”, line 72, in request
**urlopen_kw)
File “C:\Users\ryand\AppData\Local\Programs\Python\Python37-32\lib\site-packages\urllib3\request\ .py”, line 150, in request_encode_body
return self.urlopen(method, url, **extra_kw)
File “C:\Users\ryand\AppData\Local\Programs\Python\Python37-32\lib\site-packages\urllib3\poolmanager\ .py”, line 322, in urlopen
response = conn.urlopen(method, u.request_uri, **kw)
File “C:\Users\ryand\AppData\Local\Programs\Python\Python37-32\lib\site-packages\urllib3\connectionpool\ .py”, line 638, in urlopen
_stacktrace=sys.exc_info()[2])
File “C:\Users\ryand\AppData\Local\Programs\Python\Python37-32\lib\site-packages\urllib3\util\retry\ .py”, line 367, in increment
raise six.reraise(type(error), error, _stacktrace)
File “C:\Users\ryand\AppData\Local\Programs\Python\Python37-32\lib\site-packages\urllib3\packages\six\ .py”, line 685, in reraise
raise value.with_traceback(tb)
File “C:\Users\ryand\AppData\Local\Programs\Python\Python37-32\lib\site-packages\urllib3\connectionpool\ .py”, line 600, in urlopen
chunked=chunked)
File “C:\Users\ryand\AppData\Local\Programs\Python\Python37-32\lib\site-packages\urllib3\connectionpool\ .py”, line 384, in _make_request
six.raise_from(e, None)
File “”, line 2, in raise_from
File “C:\Users\ryand\AppData\Local\Programs\Python\Python37-32\lib\site-packages\urllib3\connectionpool\ .py”, line 380, in _make_request
httplib_response = conn.getresponse()
File “C:\Users\ryand\AppData\Local\Programs\Python\Python37-32\lib\http\client\ .py”, line 1321, in getresponse
response.begin()
File “C:\Users\ryand\AppData\Local\Programs\Python\Python37-32\lib\http\client\ .py”, line 296, in begin
version, status, reason = self._read_status()
File “C:\Users\ryand\AppData\Local\Programs\Python\Python37-32\lib\http\client\ .py”, line 265, in _read_status
raise RemoteDisconnected(“Remote end closed connection without”
urllib3.exceptions.ProtocolError: (‘Connection aborted.’, RemoteDisconnected(‘Remote end closed connection without response’))


Ran 1 test in 0.124s

FAILED (errors=1)

Here is what my code looks like:

import os
from time import sleep

import unittest

from appium import webdriver

PATH = lambda p: os.path.abspath(
os.path.join(os.path.dirname(file), p)
)

class SimpleAndroidTests(unittest.TestCase):
def setUp(self):
“Setup”
desired_caps = {}
desired_caps[‘platformName’] = ‘Android’
desired_caps[‘platformVersion’] = ‘7.1.1’
desired_caps[‘deviceName’] = ‘0815f89369ea0604’
desired_caps[‘appPackage’] = ‘com.testapp.test’
desired_caps[‘appActivity’] = ‘com.tns.NativeScriptActivity’

    self.driver = webdriver.Remote('192.168.1.119:5555', desired_caps)

def tearDown(self):
    "End"
    # end the session
    self.driver.quit()

def test_find_elements(self):
  "Login Test"
  el1 = driver.find_element_by_accessibility_id("Username")
  el1.click()
  el1.send_keys("Testuser1")
  el2 = driver.find_element_by_accessibility_id("Password")
  el2.send_keys("Testuser1")
  el4 = driver.find_element_by_accessibility_id("Sign In")
  el4.click()

if name == ‘main’:
suite = unittest.TestLoader().loadTestsFromTestCase(SimpleAndroidTests)
unittest.TextTestRunner(verbosity=2).run(suite)

It looks like it is crashing trying to connect but not many resources mention connecting via wifi with Python. Any advice on what I should be doing differently?

Sorry about the bad formatting. I had to do some search and replaces so Discuss would let me post this. It kept seeing references to .py files as links

1 Like

@Ryan_D Did you get a solution tho this?

I did. I have two files, test_main.py and desired_capabilities.py

Here is a chopped down version of test_main.py

import unittest
import time
from appium import webdriver
import desired_capabilities
from appium.webdriver.common.touch_action import TouchAction
from retrying import retry
import datetime
import os


class TestDevice(unittest.TestCase):
    @classmethod
    def setUpClass(self):
        desired_caps = desired_capabilities.get_desired_capabilities()
        self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)

    def _login(self):


        self.driver.find_element_by_accessibility_id('Username').send_keys("Testuser1")
        self.driver.find_element_by_accessibility_id('Password').send_keys("Testuser1")
        
        try:
            self.driver.find_element_by_accessibility_id('Sign In').click()
        except:
            time.sleep(2)
            TouchAction(self.driver)   .press(x=25, y=202)   .move_to(x=25, y=81)   .release()   .perform()
            time.sleep(2)
            TouchAction(self.driver)   .press(x=25, y=202)   .move_to(x=25, y=81)   .release()   .perform()
            time.sleep(2)
            TouchAction(self.driver)   .press(x=25, y=202)   .move_to(x=25, y=81)   .release()   .perform()
            time.sleep(2)
            TouchAction(self.driver)   .press(x=25, y=202)   .move_to(x=25, y=81)   .release()   .perform()
            time.sleep(2)
            self.driver.find_element_by_accessibility_id('Sign In').click()
        
        time.sleep(40)
    
  
    def test_main(self):
        time.sleep(30)
        
        self._login()
        
  
    def tearDown(self):
        self.driver.quit()


if __name__ == "__main__":
    unittest.main()

And here is desired_capabilities.py

import os


def get_desired_capabilities():
    desired_caps = {
        'platformName': 'Android',
        'deviceName': 'TestDevice',
        'platformVersion': '8.1',
        'autoAcceptAlerts': 'true',
        'autoDismissAlerts': 'true',
        'noReset': 'true',
        'appPackage': 'com.test.app',
        'appActivity': 'com.tns.NativeScriptActivity'

    }
    return desired_caps

this is what eventually worked but I have had nothing but issues with the AWS Device farm I am attempting to test against. That is yet unresolved in another thread.

@Ryan_D having two separate files for desired capability and the main code worked for you. How did you stumble upon this?

LOTS of experimentation and a youtube video or two from different authors (Can’t seem to find them at the moment)