Python adb test of repeated network disconnection in selfie 62

Posted by ViperSBT on Sat, 25 Apr 2020 15:42:12 +0200

Case story: during Android App or system testing, it involves abnormal test of network disconnection (in case of no network, whether the App or system prompts normal operation),
Networking test (in case of network recovery, whether the App or system prompts to be normal and operates normally). At present, the equipment basically has two kinds of networks, wifi and 4G,
It is necessary to consider whether both networks are disconnected or connected, and which network is preferred.


Preparation stage

The svc command of android can realize network management.

  1. adb shell svc wifi disable to turn off wifi
  2. adb shell svc wifi enable enables wifi
  3. When the ADC shell SVC wifi preference is opened with wifi+4G, wifi communication can be used preferentially
  4. adb shell svc data disable can turn off 4G data communication
  5. adb shell svc data enable enables 4G data communication
  6. When the ADB shell SVC data preference (wifi+4G) is opened together, 4G communication can be used first
  7. Because of 100 tests, random wifi priority or 4G priority can be considered.

Python batch script form

Remember the essence of batch scripts: execute statements in batch order.
Judge whether the string value is the same, use is and is not as much as possible, and use = = and less=

# coding=utf-8

import os
import time
import random

# Repeatedly disconnect the network for 100 times
for i in range(100):
    print("In progress%s Wheel break net + Networking test" % i)
    os.system("adb shell svc wifi disable")
    os.system("adb shell svc data disable")
    time.sleep(12)  # App will pop up without network after 10 seconds of network disconnection, so it is recommended to wait for 12 seconds
    os.system("adb shell svc wifi enable")
    os.system("adb shell svc data enable")
    perfer = random.choice(["wifi", "4G"])
    if perfer is "wifi":
        print("Priority enabled wifi network")
        os.system("adb shell svc wifi perfer")
    else:
        print("Priority enable 4 G network")
        os.system("adb shell svc data perfer")
    time.sleep(15)  # wifi connection takes time, 4G dial-up Internet also takes time, and it also takes time for App to identify network and communicate, so it is recommended to wait 15 seconds

os.system("pause")

Python procedure oriented function form

Process function oriented programming thinking should be as follows:
How many functions do you need to do this.
It is better to encapsulate all functions as much as possible, only exposing some parameter interfaces.

# coding=utf-8

import os
import time
import random


def enable_network():
    '''Open at the same time wifi,4G network'''
    os.system("adb shell svc wifi enable")
    os.system("adb shell svc data enable")


def disable_network():
    '''Close at the same time wifi,4G network'''
    os.system("adb shell svc wifi disable")
    os.system("adb shell svc data disable")


def perfer_network(network):
    '''Choose which network is the priority, yes wifi Internet, or 4 G network'''
    network = network.lower()  # Convert all to lowercase
    if network is "wifi":
        print("Priority enabled wifi network")
        os.system("adb shell svc wifi perfer")
    elif network is "4g":
        print("Priority enable 4 G network")
        os.system("adb shell svc 4G perfer")
    else:
        print("Only two types of networks are supported, wifi Or 4 G")


# Repeatedly disconnect the network for 100 times
for i in range(100):
    print("In progress%s Wheel break net + Networking test" % i)
    disable_network()
    time.sleep(12)  # App will pop up without network after 10 seconds of network disconnection, so it is recommended to wait for 12 seconds
    enable_network()
    perfer = random.choice(["wifi", "4G"])
    perfer_network(perfer)
    time.sleep(15)  # wifi connection takes time, 4G dial-up Internet also takes time, and it also takes time for App to identify network and communicate, so it is recommended to wait 15 seconds

os.system("pause")

Python object oriented class form

The programming thinking of object-oriented class should be as follows:
If you are given a blank world, what kinds of things do you need in this world,
What are the common attributes and methods of these kinds of things,
What is the relationship between these kinds of things (objects) and other kinds of things (objects).
Try to encapsulate these classes and only expose the external attributes (variables) and methods (functions).

# coding=utf-8

import os
import time
import random


class NetworkManger(object):
    '''Network manager, which only needs three methods'''

    def __init__(self):
        pass

    def enable_network(self):
        '''Open at the same time wifi,4G network'''
        os.system("adb shell svc wifi enable")
        os.system("adb shell svc data enable")

    def disable_network(self):
        '''Close at the same time wifi,4G network'''
        os.system("adb shell svc wifi disable")
        os.system("adb shell svc data disable")

    def perfer_network(self, network):
        '''Choose which network is the priority, yes wifi Internet, or 4 G network'''
        network = network.lower()  # Convert all to lowercase
        if network is "wifi":
            print("Priority enabled wifi network")
            os.system("adb shell svc wifi perfer")
        elif network is "4g":
            print("Priority enable 4 G network")
            os.system("adb shell svc 4G perfer")
        else:
            print("Only two types of networks are supported, wifi Or 4 G")


# Objects of newly instantiated classes
n_obj = NetworkManger()

# Disconnect the network again and again for 100 times
for i in range(100):
    print("In progress%s Wheel break net + Networking test" % i)
    n_obj.disable_network()
    time.sleep(12)  # App will pop up without network after 10 seconds of network disconnection, so it is recommended to wait for 12 seconds
    n_obj.enable_network()
    perfer = random.choice(["wifi", "4G"])
    n_obj.perfer_network(perfer)
    time.sleep(15)  # wifi connection takes time, 4G dial-up Internet also takes time, and it also takes time for App to identify network and communicate, so it is recommended to wait 15 seconds

os.system("pause")

Operation mode

Make sure the Android device is connected to the computer through the USB cable, and the adb device is effectively connected,
The three implementation forms of the above code can be run directly, for example, saved as network_test.py and placed on the desktop,
Open the App you need to test or jump to an interface of the system you need to test,
It is recommended to run python network_test.py, or double-click it.

For more and better original articles, please visit the official website: www.zipython.com
Selfie course (Python course of automatic test, compiled by Wu Sanren)
Original link: https://www.zipython.com/#/detail?id=73de3fe1ef094d44a5613e237106aa68
You can also follow the wechat subscription number of "wusanren" and accept the article push at any time.

Topics: Python network shell Android