How to use Python properly for automation test using aws device farm

2

I have a mobile application and I want to use aws device farm to perform automation test . I am working on Mac, I have tried a simple test of my Android application: touch a button of sign in , type username and password and login. I used appium to get Python code to perform my test, I uploaded my apk and a zip file to aws but the test always fails and I can not make screenshots either. I'm new using Python and I could not find an example to help me.

I followed all the steps in link but without success in my test.

This is my code:

from selenium.webdriver.firefox.webdriver import WebDriver
from selenium.webdriver.common.action_chains import ActionChains
import time
import os.path
import unittest
from selenium import webdriver

success = True
desired_caps = {}
desired_caps['appium-version'] = '1.0'
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '5.0.1'
desired_caps['app'] = os.path.abspath('/Users/developer/Documents/AWS/workspace/APK/Squeeze.apk')
desired_caps['appPackage'] = 'com.example.mkim.aut'
desired_caps['appActivity'] = 'com.example.mkim.aut.SuccessfulLogin'

wd = webdriver.Remote('http://0.0.0.0:4723/wd/hub', desired_caps)
wd.implicitly_wait(60)

screenshot_folder = os.getenv('SCREENSHOT_PATH', '')
wd.save_screenshot(screenshot_folder + "/screenshot.png")


def is_alert_present(wd):
    try:
        wd.switch_to_alert().text
        return True
    except:
        return False

try:
    #self.driver.save_screenshot(screenshot_folder + "/screenshot.png")
    wd.execute_script("mobile: tap", {"tapCount": 1, "touchCount": 1, "duration": 0.5, "x": 666, "y": 1519 })
    wd.save_screenshot(screenshot_folder + "/screenshot1.png")
    wd.find_element_by_xpath("//android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.webkit.WebView[1]/android.webkit.WebView[1]/android.view.View[1]/android.view.View[1]").click()
    wd.execute_script("mobile: tap", {"tapCount": 1, "touchCount": 1, "duration": 0.5, "x": 121, "y": 726 })
    wd.find_element_by_name("(null)").send_keys("[email protected]")
    wd.execute_script("mobile: tap", {"tapCount": 1, "touchCount": 1, "duration": 0.5, "x": 191, "y": 919 })
    wd.find_element_by_name("(null)").send_keys("Password")
    wd.execute_script("mobile: tap", {"tapCount": 1, "touchCount": 1, "duration": 0.5, "x": 563, "y": 1079 })
except:
    wd.quit()
    if not success:
        raise Exception("Test failed.")
    
asked by Luis16287 23.03.2016 в 16:46
source

2 answers

1

In the case of Appium, your url is incorrect,

wd = webdriver.Remote('http://0.0.0.0:4723/wd/hub', desired_caps)

the URL of the WebDriver must be http://appium.testdroid.com/wd/hub :

wd = webdriver.Remote('http://appium.testdroid.com/wd/hub', desired_caps)

Check the example scripts, if you are having problems with Python you can see the examples of Java as I imagine these more familiar when developing in android:

link

This is the example that would undoubtedly help you to take the screenshots:

link

Remember that you must upload a .zip file that contains your test package for both Appium Python :

link

as for Appium Java :

link

    
answered by 24.03.2016 / 00:42
source
1

Sorry for the late response, they changed some requirements in the application and I could not continue with the tests until now. I've been guided by the @Elenasys response links but I still do not have success in Device Farm. This is my new .py file:

from selenium.webdriver.firefox.webdriver import WebDriver
from selenium.webdriver.common.action_chains import ActionChains
import time
import os
from selenium import webdriver
import unittest
from time import sleep

#screenshotDir = os.getenv('SCREENSHOT_PATH', '/tmp')
screenshotDir = os.getenv('/Users/developer/Documents/AWS/workspace/screenshots', '/tmp')
desired_caps = {}
desired_caps['appium-version'] = '1.0'
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '5.0.1'
desired_caps['app'] = os.path.abspath('/Users/developer/Documents/AWS/workspace/APK/April-4-Android.apk')
desired_caps['appPackage'] = 'com.example.mkim.aut'
desired_caps['appActivity'] = 'com.example.mkim.aut.SuccessfulLogin'
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
# the emulator is sometimes slow and needs time to think
SLEEPY_TIME = 1

def log(msg):
    print (time.strftime("%H:%M:%S") + ": " + msg)

class SqueezeLogin(unittest.TestCase):
    def tearDown(self):
        log ("Quitting")
        driver.quit()

    def test_LoginTest(self):
        log ("Test: Squeeze Login Test")
        # log (" Getting device screen size")
        # print driver.get_windows_size()
        isSelendroid = None
        if 'automationName' in driver.capabilities:
            if driver.capabilities['automationName'] == 'selendroid':
                isSelendroid = True

        log (" Taking screenshot: start-page.png")
        # driver.save_screenshot(screenshotDir + "/start-page.png")
        driver.get_screenshot_as_file("/Users/developer/Documents/AWS/workspace/screenshots/start-page.png")

        log (" Tap on the sign in link")
        driver.execute_script("mobile: tap", {"tapCount": 1, "touchCount": 1, "duration": 0.5, "x": 666, "y": 1519 })

        log (" Taking screenshot: login-page.png")
        #driver.save_screenshot(screenshotDir + "/login-page.png")
        driver.get_screenshot_as_file("/Users/developer/Documents/AWS/workspace/screenshots/login-page.png")

        log (" Tap on username and type [email protected]")
        driver.find_element_by_xpath("//android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.webkit.WebView[1]/android.webkit.WebView[1]/android.view.View[1]/android.view.View[1]/android.widget.EditText[1]").send_keys("[email protected]")

        log (" Taking screenshot: set-username.png")
        #driver.save_screenshot(screenshotDir + "/set-username.png")
        driver.get_screenshot_as_file("/Users/developer/Documents/AWS/workspace/screenshots/set-username.png")

        log ("Tap on Password and type Password")
        driver.find_element_by_xpath("//android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.webkit.WebView[1]/android.webkit.WebView[1]/android.view.View[1]/android.view.View[1]/android.widget.EditText[2]").send_keys("Password")

        log (" Taking screenshot: set-password")
        #driver.save_screenshot(screenshotDir + "/set-password.png")
        driver.get_screenshot_as_file("/Users/developer/Documents/AWS/workspace/screenshots/set-password.png")

        log ("Hidding keyboard")
        driver.execute_script("mobile: tap", {"tapCount": 1, "touchCount": 1, "duration": 0.5, "x": 302, "y": 405 })
        sleep(5)


        log (" Tap on Log in button")
        #driver.find_element_by_name("Log In").click()
        #driver.execute_script("mobile: tap", {"tapCount": 1, "touchCount": 1, "duration": 0.5, "x": 548, "y": 1093 })
        driver.execute_script("mobile: tap", {"tapCount": 1, "touchCount": 1, "duration": 0.5, "x": 547, "y": 1093 })

        log (" Taking screenshot: verticals-page")
        #driver.save_screenshot(screenshotDir + "/verticals-page.png")
        driver.get_screenshot_as_file("/Users/developer/Documents/AWS/workspace/screenshots/verticals-page.png")

        log ("Test Success")

if __name__ == "__main__":
    # suite = unittest.TestLoader().loadTestsFromTestCase(SqueezeLogin)
    # unittest.TextTestRunner(verbosity=2).run(suite)
    unittest.main()

Create an AWS folder in this path:

/ Users / developer / Documents

Then run:

$ virtualenv workspace
$ cd workspace 
$ source bin/activate
$ pip install pytest

So this is my virtualenv path:

/ Users / developer / Documents / AWS / workspace and its structure:

- workspace
 |
 |_ APK (.apk used in appium)
 |_ bin
 |_ include
 |_ lib
 |_ screenshots
 |_ selenium
 |_ tests (where my .py file are stored)
 |_ wheelhouse

When I run $ py.test --collect-only tests / it recognizes my test and running $ python test_xxx.py while appium server is running and my application runs in the emulator, it works fine. Then I execute:

$ pip freeze > requirements.txt
$ pip wheel --wheel-dir wheelhouse -r requirements.txt y me devuelve:

    Collecting Appium-Python-Client==0.22 (from -r requirements.txt (line 1))
    File was already downloaded /Users/developer/Documents/AWS/workspace/wheelhouse/Appium_Python_Client-0.22-cp27-none-any.whl
    Collecting appium-selector==1.2.7 (from -r requirements.txt (line 2))
    File was already downloaded /Users/developer/Documents/AWS/workspace/wheelhouse/appium_selector-1.2.7-cp27-none-any.whl
    Collecting beautifulsoup4==4.4.1 (from -r requirements.txt (line 3))
    File was already downloaded /Users/developer/Documents/AWS/workspace/wheelhouse/beautifulsoup4-4.4.1-py2-none-any.whl
    Collecting extras==0.0.3 (from -r requirements.txt (line 4))
    File was already downloaded /Users/developer/Documents/AWS/workspace/wheelhouse/extras-0.0.3-cp27-none-any.whl
    Collecting fixtures==1.4.0 (from -r requirements.txt (line 5))
    File was already downloaded /Users/developer/Documents/AWS/workspace/wheelhouse/fixtures-1.4.0-py2.py3-none-any.whl
    Collecting hotdog==1.5.21 (from -r requirements.txt (line 6))
    File was already downloaded /Users/developer/Documents/AWS/workspace/wheelhouse/hotdog-1.5.21-cp27-none-any.whl
    Collecting linecache2==1.0.0 (from -r requirements.txt (line 7))
    File was already downloaded /Users/developer/Documents/AWS/workspace/wheelhouse/linecache2-1.0.0-py2.py3-none-any.whl
    Collecting pbr==1.8.1 (from -r requirements.txt (line 8))
    File was already downloaded /Users/developer/Documents/AWS/workspace/wheelhouse/pbr-1.8.1-py2.py3-none-any.whl
    Collecting pyforge==1.3.0 (from -r requirements.txt (line 9))
    File was already downloaded /Users/developer/Documents/AWS/workspace/wheelhouse/pyforge-1.3.0-cp27-none-any.whl
    Collecting pyrsistent==0.11.13 (from -r requirements.txt (line 10))
    File was already downloaded /Users/developer/Documents/AWS/workspace/wheelhouse/pyrsistent-0.11.13-cp27-cp27m-macosx_10_6_intel.whl
    Collecting python-mimeparse==1.5.1 (from -r requirements.txt (line 11))
    File was already downloaded /Users/developer/Documents/AWS/workspace/wheelhouse/python_mimeparse-1.5.1-cp27-none-any.whl
    Collecting requests==2.9.1 (from -r requirements.txt (line 12))
    File was already downloaded /Users/developer/Documents/AWS/workspace/wheelhouse/requests-2.9.1-py2.py3-none-any.whl
    Collecting sauceclient==0.2.1 (from -r requirements.txt (line 13))
    File was already downloaded /Users/developer/Documents/AWS/workspace/wheelhouse/sauceclient-0.2.1-cp27-none-any.whl
    Collecting selenium==2.53.1 (from -r requirements.txt (line 14))
    File was already downloaded /Users/developer/Documents/AWS/workspace/wheelhouse/selenium-2.53.1-py2-none-any.whl
    Collecting sentinels==0.0.6 (from -r requirements.txt (line 15))
    File was already downloaded /Users/developer/Documents/AWS/workspace/wheelhouse/sentinels-0.0.6-cp27-none-any.whl
    Collecting six==1.10.0 (from -r requirements.txt (line 16))
    File was already downloaded /Users/developer/Documents/AWS/workspace/wheelhouse/six-1.10.0-py2.py3-none-any.whl
    Collecting testtools==2.0.0 (from -r requirements.txt (line 17))
    File was already downloaded /Users/developer/Documents/AWS/workspace/wheelhouse/testtools-2.0.0-py2.py3-none-any.whl
    Collecting timeout-decorator==0.3.2 (from -r requirements.txt (line 18))
    File was already downloaded /Users/developer/Documents/AWS/workspace/wheelhouse/timeout_decorator-0.3.2-cp27-none-any.whl
    Collecting traceback2==1.4.0 (from -r requirements.txt (line 19))
    File was already downloaded /Users/developer/Documents/AWS/workspace/wheelhouse/traceback2-1.4.0-py2.py3-none-any.whl
    Collecting unittest2==1.1.0 (from -r requirements.txt (line 20))
    File was already downloaded /Users/developer/Documents/AWS/workspace/wheelhouse/unittest2-1.1.0-py2.py3-none-any.whl
    Collecting vboxapi==1.0 (from -r requirements.txt (line 21))
    Could not find a version that satisfies the requirement vboxapi==1.0 (from -r requirements.txt (line 21)) (from versions: )
    No matching distribution found for vboxapi==1.0 (from -r requirements.txt (line 21))

$ zip -r test_login.zip tests/ wheelhouse/ requirements.txt 

Then I login in the AWS console, create a new project, upload my .apk file, upload my .zip file with my test, select the devices and run the test. But I still have errors in it:

Test Result

No screenshot

So I'm still not able to get positive results on AWS = (

Neither can I get the screenshots, using:

    screenshotDir = os.getenv('SCREENSHOT_PATH', '/tmp')
    driver.save_screenshot(screenshotDir + "/screenshot_name.png") 

It has not worked for me, so I use:

    driver.get_screenshot_as_file("/Users/developer/Documents/AWS/workspace/screenshots/screenshot_name.png")

for each screenshot.

If it helps with anything, I'm using the Meteor Framework in my project.

I do not know if I need something else so that my test is successful in Device Farm, and how I can get the screenshots taken, also I would like to know how Device Farm returns a log or that I should add to my .py to generate the same when running my test on Device Farm.

    
answered by 07.04.2016 в 17:20