(English Below)
Androidのランダムテストを行うには、Android Debug Bridge(ADB)を使用して、ランダムなイベントを生成するスクリプトを実行する必要があります。以下は、Androidのランダムテストを実行するための手順です。
ADBをインストールする
ADBを使用するには、Android SDKをダウンロードしてインストールする必要があります。SDKをダウンロードし、インストールする方法については、Androidの公式ドキュメントを参照してください。
デバイスを接続する
テストを実行するデバイスをADBに接続します。以下のコマンドを使用して、デバイスが正しく接続されていることを確認できます。
adb devices
スクリプトを作成する
次に、ランダムなイベントを生成するPythonスクリプトを作成します。以下は、例です。
import subprocess
import random
import time
events = [
'input keyevent KEYCODE_HOME',
'input keyevent KEYCODE_BACK',
'input swipe 100 500 500 500',
'input keyevent KEYCODE_MENU',
'input keyevent KEYCODE_POWER',
'input keyevent KEYCODE_VOLUME_UP',
'input keyevent KEYCODE_VOLUME_DOWN',
'input keyevent KEYCODE_CAMERA',
'input keyevent KEYCODE_FOCUS',
'input keyevent KEYCODE_MEDIA_PLAY',
'input keyevent KEYCODE_MEDIA_PAUSE',
'input keyevent KEYCODE_MEDIA_PLAY_PAUSE',
'input keyevent KEYCODE_MEDIA_STOP',
'input keyevent KEYCODE_MEDIA_NEXT',
'input keyevent KEYCODE_MEDIA_PREVIOUS',
'input keyevent KEYCODE_MEDIA_REWIND',
'input keyevent KEYCODE_MEDIA_FAST_FORWARD',
'input keyevent KEYCODE_SLEEP',
'input keyevent KEYCODE_WAKEUP',
'input keyevent KEYCODE_MUTE',
'input keyevent KEYCODE_PAGE_UP',
'input keyevent KEYCODE_PAGE_DOWN',
'input keyevent KEYCODE_BOOKMARK',
'input keyevent KEYCODE_SEARCH',
'input keyevent KEYCODE_ENVELOPE',
'input keyevent KEYCODE_HEADSETHOOK',
'input keyevent KEYCODE_FOCUS',
'input keyevent KEYCODE_BRIGHTNESS_UP',
'input keyevent KEYCODE_BRIGHTNESS_DOWN',
'input keyevent KEYCODE_MEDIA_AUDIO_TRACK'
]
while True:
event = random.choice(events)
subprocess.call(['adb', 'shell', event])
time.sleep(1)
上記のスクリプトでは、ホーム、バック、スワイプに加えて、POWER、VOLUME、MEDIAなどのキーイベントを生成しています
スクリプトを実行する
最後に、スクリプトを実行します。以下のコマンドを使用して、スクリプトを実行できます。
python script.py
これで、Androidのランダムテストを実行する準備が整いました。スクリプトを実行すると、デバイス上でランダムなイベントが生成され、アプリケーションの振る舞いをテストできます。
結果確認
いくつかの方法で結果を観測できます。以下にいくつかの例を挙げます。
ログファイルの生成:
テストスクリプトを実行するときに、ログファイルを生成することができます。例えば、次のように実行すると、log.txtという名前のファイルにログが書き込まれます。
import subprocess
import random
import time
events = [
# events
]
with open('log.txt', 'w') as f:
while True:
event = random.choice(events)
subprocess.call(['adb', 'shell', event])
f.write(event + '\n')
time.sleep(1)
このログファイルには、テスト中に実行されたイベントの一覧が含まれます。このログファイルを分析することで、テストの実行中に発生した問題を特定することができます。
スクリーンショットの撮影
テスト中にアプリのスクリーンショットを撮影することもできます。以下のようにして、スクリーンショットを撮影することができます。
このスクリプトを実行すると、毎秒1回の間隔でスクリーンショットが撮影されます。これにより、アプリの外観や動作に問題がある場合に、それを視覚的に確認できます。
テストレポートの生成
もう一つの方法は、テストレポートを生成することです。例えば、次のようなスクリプトを実行することで、レポートを生成することができます。
import subprocess
import random
import time
import os
from datetime import datetime
events = [
# events
]
results =
while True:
event = random.choice(events)
subprocess.call(['adb', 'shell', event])
time.sleep(1)
output = subprocess.check_output(['adb', 'shell', 'dumpsys activity'])
results.append(output)
if len(results) >= 100:
timestamp = datetime.now().strftime('%Y-%m-%d-%H-%M-%S')
filename = f'test-report-{timestamp}.txt'
with open(filename, 'w') as f:
for result in results:
f.write(result.decode('utf-8'))
f.write('\n\n')
results =
このスクリプトでは、毎秒1回の間隔でテストを実行し、dumpsys activityコマンドを使用してアクティビティの状態を取得し、テストレポートに結果を追加しています。resultsリストには、取得されたすべての結果が含まれます。このスクリプトを実行すると、毎100回のテストの実行ごとに、タイムスタンプを含むテストレポートが生成されます。このレポートには、アプリの状態やパフォーマンスに関する詳細な情報が含まれます。
これらは、Androidのランダムテストを実行するときに使用できるいくつかの方法です。どの方法を使用するかは、目的や状況によって異なります。例えば、テストレポートを使用すると、アプリの内部状態に関する詳細な情報を取得できますが、ログファイルを使用すると、テスト中に実行されたイベントの一覧を取得できます。
====English translation====
To perform a random test for Android, you must use the Android Debug Bridge (ADB) to run a script that generates random events. Below are the steps to run a random test for Android.
Install ADB
To use ADB, you must download and install the Android SDK; see the official Android documentation for instructions on how to download and install the SDK.
Connecting the device
Connect the device you wish to test to ADB. You can verify that the device is properly connected by using the following command
adb devices
Create a script
Next, create a Python script to generate random events. Here is an example
import subprocess
import random
import time
events = [
'input keyevent KEYCODE_HOME',
'input keyevent KEYCODE_BACK',
'input swipe 100 500 500 500',
'input keyevent KEYCODE_MENU',
'input keyevent KEYCODE_POWER',
'input keyevent KEYCODE_VOLUME_UP',
'input keyevent KEYCODE_VOLUME_DOWN',
'input keyevent KEYCODE_CAMERA',
'input keyevent KEYCODE_FOCUS',
'input keyevent KEYCODE_MEDIA_PLAY',
'input keyevent KEYCODE_MEDIA_PAUSE',
'input keyevent KEYCODE_MEDIA_PLAY_PAUSE',
'input keyevent KEYCODE_MEDIA_STOP',
'input keyevent KEYCODE_MEDIA_NEXT',
'input keyevent KEYCODE_MEDIA_PREVIOUS',
'input keyevent KEYCODE_MEDIA_REWIND',
'input keyevent KEYCODE_MEDIA_FAST_FORWARD',
'input keyevent KEYCODE_SLEEP',
'input keyevent KEYCODE_WAKEUP',
'input keyevent KEYCODE_MUTE',
'input keyevent KEYCODE_PAGE_UP',
'input keyevent KEYCODE_PAGE_DOWN',
'input keyevent KEYCODE_BOOKMARK',
'input keyevent KEYCODE_SEARCH',
'input keyevent KEYCODE_ENVELOPE',
'input keyevent KEYCODE_HEADSETHOOK',
'input keyevent KEYCODE_FOCUS',
'input keyevent KEYCODE_BRIGHTNESS_UP',
'input keyevent KEYCODE_BRIGHTNESS_DOWN',
'input keyevent KEYCODE_MEDIA_AUDIO_TRACK'
]
while True:
event = random.choice(events)
subprocess.call(['adb', 'shell', event])
time.sleep(1)
The above script generates key events such as POWER, VOLUME, and MEDIA in addition to home, back, and swipe
Executing the Script
Finally, run the script. You can run the script using the following command
python script.py
Now you are ready to run a random test on Android. Running the script will generate random events on the device, allowing you to test the application's behavior.
TestResults
The results can be observed in several ways. Here are some examples
Log file generation
When you run the test script, you can generate a log file. For example, the following execution will write the log to a file named log.txt.
import subprocess
import random
import time
events = [
# events
]
with open('log.txt', 'w') as f:
while True:
event = random.choice(events)
subprocess.call(['adb', 'shell', event])
f.write(event + '\n')
time.sleep(1)
This log file contains a list of events executed during the test. By analyzing this log file, we can identify problems that occurred during the execution of the test.
Taking screenshots
You can also take screenshots of your application during testing. You can take a screenshot as follows
When this script is run, a screenshot is taken at an interval of one per second. This allows you to visually see if there are any problems with the appearance or behavior of the application.
Generate a test report
Another way is to generate a test report. For example, a report can be generated by running a script like the following
import subprocess
import random
import time
import os
from datetime import datetime
events = [
# events
]
results =
while True:
event = random.choice(events)
subprocess.call(['adb', 'shell', event])
time.sleep(1)
output = subprocess.check_output(['adb', 'shell', 'dumpsys activity'])
results.append(output)
if len(results) >= 100:
timestamp = datetime.now().strftime('%Y-%m-%d-%H-%M-%S')
filename = f'test-report-{timestamp}.txt'
with open(filename, 'w') as f:
for result in results:
f.write(result.decode('utf-8'))
f.write('\n\n')
results =
This script runs the test at an interval of once per second, retrieves the activity status using the dumpsys activity command, and adds the results to the test report. the results list contains all the results retrieved. Running this script will generate a test report with a timestamp for every 100 test runs. This report contains detailed information about the status and performance of the app.
These are a few methods that can be used when running random Android tests. Which method is used depends on the purpose and situation. For example, a test report can be used to get detailed information about the internal state of the app, while a log file can be used to get a list of events performed during the test.
evices