Practical edition of automating the testing of Flutter apps with Appium (Python)

Introduction

Environment construction to automate the test of Flutter application with Appium --Qiita It will be a continuation of. I'll actually write the test code and run it. This time, I will use "Python", which seems to be relatively easy to prepare, while also studying.

Prerequisites

Environment construction to automate the test of Flutter application with Appium --Qiita And the environment construction of Appium is completed

Install pip command

Python should come standard with your Mac, so you don't need to install it again. You should already be able to use the pip command, but if you didn't, https://bootstrap.pypa.io/get-pip.py Please download and execute the following command at the download destination. (If that doesn't work, try running it with `` `sudo``` in front of it)

python get-pip.py

Install Appium-Python-Client

Execute the following command to install Appium-Python-Client. (If that doesn't work, try running it with `` `sudo``` in front of it)

pip install Appium-Python-Client

Sample application used this time

We have prepared a very simple sample app below. (This is the count-up app that is written by default when you create a new Flutter project in Android Studio)

Screenshot_20191125-231835.jpg

Record test code in Appium Desktop

Start Appium Desktop and go to the point where you start the session with "Start Session". (Refer to Environment Construction for the procedure)

When you start the session, the following screen will appear. The screen of the app is displayed on the left side. If the display does not match the screen of the terminal, click the refresh button to refresh the screen. First, click the record button to start recording.

1.png

The screen will switch and a pause button will appear instead of the record button. To stop recording, click the pause button.

2.png

Then click the plus button from the Appium screen instead of the device. Information about the plus button will be displayed on the right side.

3.png

Click Tap to see the test code at the top. You can select the language of the test code and select "Python".

4.png

You can also select JS (wd), JS (Webdriver.io), Ruby, Java, and Robot Framework.

7.png

This allows you to record the test code when you press the button. The recorded test code is as follows.

el1 = self.driver.find_element_by_xpath("/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.view.View/android.view.View/android.view.View/android.view.View/android.widget.Button")
el1.click()

Next, I would like to test that the text labeled "1" is properly labeled as "1". You can get the element from "Selector" of "Selected Element" by clicking the element.

5.png

But I didn't know how to record it in Appium Desktop.

Appium-Python-Client · PyPI According to the site, you can use the get_attribute function to get the value set in the text.

el = driver.find_element_by_class_name('android.widget.EditText')
driver.set_value(el, 'Testing')

text = el.get_attribute('text')
assertEqual('Testing', text)

el.set_value('More testing')
text = el.get_attribute('text')
assertEqual('More testing', text)

The test code is described as follows.

el2 = self.driver.find_element_by_xpath("/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.view.View/android.view.View/android.view.View/android.view.View/android.view.View[3]")
text = el2.get_attribute('text')
self.assertEqual('1', text)

Allow the recorded test code to be executed

Just recording it is not ready for execution yet, so lay the groundwork for it. The full code is below.

flutter_app_for_appium_test.py


import os
import unittest
from appium import webdriver
from time import sleep

class FlutterAppTests(unittest.TestCase):
    "Class to run tests against the Chess Free app"
    def setUp(self):
        "Setup for the test"
        desired_caps = {}
        desired_caps['platformName'] = 'Android'
        desired_caps['platformVersion'] = '9'
        desired_caps['deviceName'] = '988a97354e4e4c5054'
        desired_caps['app'] = os.path.abspath(os.path.join(os.path.dirname(__file__), '/Users/Hitoshi/AndroidStudioProjects/flutter_app_for_appium/build/app/outputs/apk/release/app-release.apk'))
        self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)

    def tearDown(self):
        "Tear down the test"
        self.driver.quit()

    def test_single_player_mode(self):
        "Test the Flutter app launches correctly"
        sleep(1)
        # -----Paste the code recorded in Appium Desktop from here
        el1 = self.driver.find_element_by_xpath("/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.view.View/android.view.View/android.view.View/android.view.View/android.widget.Button")
        el1.click()
        el2 = self.driver.find_element_by_xpath("/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.view.View/android.view.View/android.view.View/android.view.View/android.view.View[3]")
        text = el2.get_attribute('text')
        self.assertEqual('1', text)
        # -----Paste the code recorded in Appium Desktop so far

if __name__ == '__main__':
    suite = unittest.TestLoader().loadTestsFromTestCase(FlutterAppTests)
    unittest.TextTestRunner(verbosity=2).run(suite)

Here, I will explain it briefly. First, in the setUp function, describe the device and APK information. This is OK if you enter the same information as "Desired Capabilities" of Appium Desktop.

desired_caps = {}
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '9'
desired_caps['deviceName'] = '988a97354e4e4c5054'
desired_caps['app'] = os.path.abspath(os.path.join(os.path.dirname(__file__), '/Users/Hitoshi/AndroidStudioProjects/flutter_app_for_appium/build/app/outputs/apk/release/app-release.apk'))

Next, in the test_single_player_mode function, paste the test code you just recorded in Appium Desktop. Of course, you can implement it yourself. Here is the actual test code.

el1 = self.driver.find_element_by_xpath("/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.view.View/android.view.View/android.view.View/android.view.View/android.widget.Button")
el1.click()
el2 = self.driver.find_element_by_xpath("/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.view.View/android.view.View/android.view.View/android.view.View/android.view.View[3]")
text = el2.get_attribute('text')
self.assertEqual('1', text)

Finally, use the tearDown function to terminate the driver.

self.driver.quit()

Execution of test code

Run the test code with the command.

python flutter_app_for_appium_test.py

You should see something like this: The app should start on the terminal side as well, and the button should be pressed to count up.

test_single_player_mode (__main__.FlutterAppTests)
Test the Flutter app launches correctly ... ok

----------------------------------------------------------------------
Ran 1 test in 25.573s

OK

reference

-Introduced appium for Android test automation (windows + python) --Qiita

Recommended Posts

Practical edition of automating the testing of Flutter apps with Appium (Python)
Check the existence of the file with python
Prepare the execution environment of Python3 with Docker
2016 The University of Tokyo Mathematics Solved with Python
Calculate the total number of combinations with python
Check the date of the flag duty with Python
Automating simple tasks with Python Table of contents
Convert the character code of the file with Python3
[Python] Determine the type of iris with SVM
Try out the touch of data-driven testing with Selenium Python Bindings and py.test
Extract the table of image files with OneDrive & Python
Learn Nim with Python (from the beginning of the year).
Destroy the intermediate expression of the sweep method with Python
Visualize the range of interpolation and extrapolation with python
Calculate the regression coefficient of simple regression analysis with python
The definitive edition of python scraping! (Target site: BicCamera)
Get the operation status of JR West with Python
Extract the band information of raster data with python
Try scraping the data of COVID-19 in Tokyo with Python
I tried "gamma correction" of the image with Python + OpenCV
The story of implementing the popular Facebook Messenger Bot with python
Unify the environment of the Python development team starting with Poetry
Visualize the results of decision trees performed with Python scikit-learn
Calculate the square root of 2 in millions of digits with python
I wrote the basic grammar of Python with Jupyter Lab
Tank game made with python About the behavior of tanks
Run the intellisense of your own python library with VScode.
Introducing the potential of Plotly scatter plots with practical examples
I evaluated the strategy of stock system trading with Python.
Check the scope of local variables with the Python locals function.
Let's touch the API of Netatmo Weather Station with Python. #Python #Netatmo
The story of rubyist struggling with python :: Dict data with pycall
[Homology] Count the number of holes in data with Python
Try to automate the operation of network devices with Python
Estimate the attitude of AR markers with Python + OpenCV + drone
Play with the password mechanism of GitHub Webhook and Python
Get the source of the page to load infinitely with python.
Towards the retirement of Python2
Automate python testing with CircleCI
About the ease of Python
Call the API with python3.
About the features of Python
The Power of Pandas: Python
I compared the speed of Hash with Topaz, Ruby and Python
I tried scraping the ranking of Qiita Advent Calendar with Python
I tried to solve the ant book beginner's edition with python
Save the result of the life game as a gif with python
[Python] Organize the basic structure of Flask apps (Aim for de-copying)
March 14th is Pi Day. The story of calculating pi with python
Color extraction with Python + OpenCV solved the mystery of the green background
[python, ruby] fetch the contents of a web page with selenium-webdriver
I want to output the beginning of the next month with Python
Output the contents of ~ .xlsx in the folder to HTML with Python
The story of making a standard driver for db with python.
Visualize the frequency of word occurrences in sentences with Word Cloud. [Python]
Let's summarize the degree of coupling between modules with Python code
The idea of feeding the config file with a python file instead of yaml
Tips: [Python] Calculate the average value of the specified area with bedgraph
From the introduction of JUMAN ++ to morphological analysis of Japanese with Python
Calculated the ease of stopping the house of the board game "Bunkers" with Python
I tried to improve the efficiency of daily work with Python