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.
Environment construction to automate the test of Flutter application with Appium --Qiita And the environment construction of Appium is completed
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
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
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)
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.
The screen will switch and a pause button will appear instead of the record button. To stop recording, click the pause button.
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.
Click Tap to see the test code at the top. You can select the language of the test code and select "Python".
You can also select JS (wd), JS (Webdriver.io), Ruby, Java, and Robot Framework.
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.
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)
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()
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
-Introduced appium for Android test automation (windows + python) --Qiita
Recommended Posts