예제 #1
0
 def port_is_used(self, port_number):
     """检测端口是否占用"""
     dos = DosCmd()
     command = "netstat -ano | findstr " + str(port_number)
     result = dos.execute_cmd_result(command)
     if len(result) > 0:
         return True
     return False
    def test_new_install(self):
        self.check_install = DosCmd.excute_cmd_result(
            self, command=self.check_command)

        if "package:/data/app/com.xiaomi.hm.health-7hJ0INef8qDHy10fkeKCCA==/base.apk" in self.check_install:
            self.uninstall = DosCmd.excute_cmd_result(
                self, command=self.uninstall_command)
            time.sleep(4)
            self.install = DosCmd.excute_cmd_result(
                self, command=self.install_command)
            # self.save_screenshots("初次安装截图")
            # self.logInfo.logEnd("初次安装日志")
            assert "Success" in self.install
        else:
            self.install = DosCmd.excute_cmd_result(
                self, command=self.install_command)
            assert "Success" in self.install
예제 #3
0
 def __init__(self):
     self.dos_cmd = DosCmd()
     self.deviceName = os.environ["deviceName"]
     self.deviceBrandName = os.environ["deviceBrandName"]
     self.now = time.strftime("%Y-%m-%d-%H-%M-%S",
                              time.localtime(time.time()))
     # self.filename = FileConfig().get_path(type="log") + self.now + r".txt"
     self.filename = self.now + r".txt"
     self.file_path = FileConfig().get_path(type="logs") + self.filename
     self.logcat_file = open(self.file_path, 'w')
     # logcmd = "adb logcat -v time"
     logcmd = "sudo docker exec - i $" + self.deviceBrandName + " adb logcat -v time"
     # logcmd = "adb logcat -v time"
     self.Poplog = subprocess.Popen(logcmd,
                                    shell=True,
                                    stdout=self.logcat_file,
                                    stderr=subprocess.PIPE)
예제 #4
0
class Server:
    def __init__(self):
        self.dos = DosCmd()

    def kill_node_server(self):
        """
        查找:tasklist | find "node.exe"
        杀进程:taskkill -F -PID node.exe
        :return:
        """
        # 查找结果列表
        server_list = self.dos.excute_cmd_result('tasklist | find "node.exe"')
        if len(server_list) > 0:
            self.dos.excute_cmd('taskkill -F -PID node.exe')

    def kill_chrome_server(self):
        """
        查找:tasklist | find "chrome.exe"
        杀进程:taskkill -F -PID chrome.exe
        :return:
        """
        # 查找结果列表
        server_list = self.dos.excute_cmd_result(
            'tasklist | find "chrome.exe"')
        if len(server_list) > 0:
            self.dos.excute_cmd('taskkill -F -PID chrome.exe')
예제 #5
0
class LogInfo:
    def __init__(self):
        self.dos_cmd = DosCmd()
        self.deviceName = os.environ["deviceName"]
        self.deviceBrandName = os.environ["deviceBrandName"]
        self.now = time.strftime("%Y-%m-%d-%H-%M-%S",
                                 time.localtime(time.time()))
        # self.filename = FileConfig().get_path(type="log") + self.now + r".txt"
        self.filename = self.now + r".txt"
        self.file_path = FileConfig().get_path(type="logs") + self.filename
        self.logcat_file = open(self.file_path, 'w')
        # logcmd = "adb logcat -v time"
        logcmd = "sudo docker exec - i $" + self.deviceBrandName + " adb logcat -v time"
        # logcmd = "adb logcat -v time"
        self.Poplog = subprocess.Popen(logcmd,
                                       shell=True,
                                       stdout=self.logcat_file,
                                       stderr=subprocess.PIPE)
        # dos_cmd.excute_cmd(logcmd)

    def logEnd(self, log_doc):
        self.Poplog.terminate()
        file_object = open(self.file_path, 'rb')
        self.deviceName = os.environ["deviceName"]
        self.deviceBrandName = os.environ["deviceBrandName"]
        try:
            # for line in file_object:
            #     print(line)
            #     "\n"
            self.file = file_object.read()
        finally:
            allure.attach(self.file, log_doc, allure.attachment_type.TEXT)
            clear_logcat = "sudo docker exec -i $" + self.deviceBrandName + " adb logcat -c"
            # clear_logcat = "adb logcat -c"
            self.dos_cmd.excute_cmd(clear_logcat)
            file_object.close()
            self.logcat_file.close()
예제 #6
0
 def __init__(self):
     self.dos = DosCmd()
 def test_recover_install(self):
     self.install = DosCmd().excute_cmd_result(command=self.command)
     # self.save_screenshots("覆盖安装截图")
     # self.logInfo.logEnd("覆盖安装日志")
     assert "Success" in self.install
예제 #8
0
 def __init__(self):
     self.dos = DosCmd()
     self.write_file = WriteUserConfig()
     self.log_path = conftest.log_dir
예제 #9
0
class Server:
    def __init__(self):
        self.dos = DosCmd()
        self.write_file = WriteUserConfig()
        self.log_path = conftest.log_dir

    def get_devices(self):
        """
        获取设备信息
        :return:设备列表devices_list
        """
        devices_list = []
        time.sleep(2)
        try:
            result_list = self.dos.excute_cmd_result("adb devices")
            print("[MyLog]--------The length of the result is: " + str(len(result_list)) + "----------------")
            print("[MyLog]--------The results of [adb devices] is: " + str(result_list))
            if len(result_list) >= 2:
                for i in result_list:
                    if 'List' in i:
                        continue
                    devices_info = i.split("\t")
                    time.sleep(2)
                    try:
                        if devices_info[-1] == 'device':
                            devices_list.append(devices_info[0])
                    except Exception as msg:
                        print(u"[MyLog]--------获取device失败%s" % msg)
                    else:
                        time.sleep(2)
                return devices_list
            else:
                print(u'[MyLog]--------没有连接的设备,请检查……')
                quit()
        except Exception as msg:
            print(u"启动adb异常%s" % msg)
        time.sleep(2)

    def port_is_used(self, port_num, platform=None):
        """
        判断端口是否被占用
        :param platform: 平台:ios/android
        :param port_num: 检查的端口号
        :return:布尔值flag
        """
        if platform is None:
            command = "netstat -ano | findstr " + str(port_num)
        else:
            command = "netstat -anp tcp | grep " + str(port_num)
        result = self.dos.excute_cmd_result(command)
        if len(result) > 0:
            flag = True
        else:
            flag = False
        return flag

    def create_port_list(self, start_port, device_list, platform):
        """
        生成可用端口
        :param platform: 平台:ios/android
        :param device_list: 连接的设备
        :param start_port: 起始端口号,如4700
        :return:
        """
        port_list = []
        if device_list is not None:
            while len(port_list) != len(device_list):
                if self.port_is_used(start_port, platform) is not True:
                    port_list.append(start_port)
                start_port = start_port + 1
            return port_list
        else:
            print(u"[MyLog]--------生成可用端口失败")
            return None

    def create_appium_command(self, i, device_list, platform=None):
        """
        appium -p 4723 -bp 4701 -U 159beaa8
        :return:command_list
        os.system会阻塞进程,为避免不影响执行下一步,在命令前面一定要加start
        改为用os.system("start appium -a 127.0.0.1 -p %s -U %s")
        """
        command_list = []
        time.sleep(2)
        appium_port_list = self.create_port_list(4700, device_list, platform)
        bootstrap_port_list = self.create_port_list(4900, device_list, platform)
        if platform is None:
            command = "start /b appium -a 127.0.0.1 -p " + str(appium_port_list[i]) + " -bp " + str(bootstrap_port_list[i]) + \
                      " -U " + device_list[i] + " --session-override "
        else:
            command = "appium -a 127.0.0.1 -p " + str(appium_port_list[i]) + " --session-override "
        command_list.append(command)
        self.write_file.write_data(i, device_list[i], str(bootstrap_port_list[i]),
                                   str(appium_port_list[i]))
        return command_list

    def start_server(self, i, device_list, platform=None):
        """
        定义方法:启动appium server
        """
        start_appium_list = self.create_appium_command(i, device_list, platform)
        print("[MyLog]--------" + str(start_appium_list))
        self.dos.excute_cmd('set BUILD_ID=dontKillMe')
        self.dos.excute_cmd(start_appium_list[0])

    def kill_server(self):
        """
        定义方法:每次执行前先kill掉node进程
        """
        server_list = self.dos.excute_cmd_result('tasklist | find "node.exe"')
        if len(server_list)>0:
            self.dos.excute_cmd('taskkill -F -PID node.exe')

    def main(self, platform):
        """
        多线程,执行main方法
        step1:kill server
        step2:clear file(userconfig.yaml)
        step3:根据生成的 appium list 分线程执行 start server
        """
        if platform == 'android':
            android_device_list = self.get_devices()
            device_list = android_device_list
            self.kill_server()
        else:
            ios_device_list = ['iPhone Simulator']
            device_list = ios_device_list
        thread_list = []
        self.write_file.clear_data()
        try:
            len(device_list)
        except Exception as msg:
            print(u"[MyLog]--------设备读取异常%s" % msg)
        else:
            for i in range(len(device_list)):
                if platform == 'android':
                    appium_start = threading.Thread(target=self.start_server, args=(i, device_list))
                else:
                    appium_start = threading.Thread(target=self.start_server, args=(i, device_list, 'ios'))
                thread_list.append(appium_start)
            for j in thread_list:
                j.start()
            time.sleep(15)
예제 #10
0
    def andriod_driver(cls):
        cmd_result = DosCmd()
        port_check = Port()
        # write_file = WriteUserCommand()
        # deviceName = write_file.get_value('user_info_'+str(i), 'deviceName')
        # port = write_file.get_value('user_info_'+str(i), 'port')
        #        Initialization = os.environ["Initialization"]
        caps = {}
        caps["platformName"] = "android"
        # caps["deviceName"] = "10.1.14.146:5555"
        #        caps["deviceName"] = os.environ["UDID"]
        caps["deviceName"] = "37KNW18614022787"
        # caps["deviceName"] = deviceName
        caps["appPackage"] = "com.xiaomi.hm.health"
        caps["appActivity"] = "activity.StartUpActivity"
        caps["autoGrantPermissions"] = True
        caps["showChromedriverLog"] = True
        caps["noReset"] = True
        Initialization = False
        if Initialization == True:
            caps["skipServerInstallation"] = False
            caps["skipDeviceInitialization"] = False
        else:
            caps["skipServerInstallation"] = True
            caps["skipDeviceInitialization"] = True
        caps["automationName"] = "UiAutomator2"
        caps["newCommandTimeout"] = "600"
        if caps["deviceName"] == "37KNW18614022787":

            cls.start_docker_server = cmd_result.excute_cmd_result(
                "sudo docker run --privileged -d -p" + " 4723" +
                ":4723 --name" + " 37KNW18614022787" + " appium/appium")
            cls.driver = webdriver.Remote(
                "http://10.1.18.116:" + "4723" + "/wd/hub", caps)
            cls.connect_appium_server = cmd_result.excute_cmd_result(
                "sudo docker exec -i" + " 37KNW18614022787" + " adb connect" +
                " 37KNW18614022787")
        else:
            cls.start_docker_server = cmd_result.excute_cmd_result(
                "sudo docker run --privileged -d -p" + " 4724" +
                ":4723 --name" + " 4ad326fc" + " appium/appium")
            cls.driver = webdriver.Remote(
                "http://10.1.18.116:" + "4724" + "/wd/hub", caps)
            cls.connect_appium_server = cmd_result.excute_cmd_result(
                "sudo docker exec -i" + " 4ad326fc" + " adb connect" +
                " 4ad326fc")
        # i = 4723
        # check_port = True
        # while check_port:
        #     cls.check_port = port_check.port_is_used(i)
        #     i=i+1
        #     port=i
        #     cls.start_docker_server = cmd_result.excute_cmd_result("sudo docker run --privileged -d -p"+ str(port)+":4723 --name"+"37KNW18614022787" +"appium/appium")
        # else:
        #     port=i
        #     cls.start_docker_server = cmd_result.excute_cmd_result("sudo docker run --privileged -d -p"+ str(port)+":4723 --name"+"37KNW18614022787" +"appium/appium")

        #self.start_docker_server = cmd_result.excute_cmd_result("sudo docker run --privileged -d -p"+ port+":4723 --name $UDID appium/appium")

        #cls.driver = webdriver.Remote("http://10.1.18.116:"+str(port)+"/wd/hub",caps)
        #cls.connect_appium_server = cmd_result.excute_cmd_result("sudo docker exec -i"+"37KNW18614022787" +"adb connect"+"37KNW18614022787" )        # cls.driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub",caps)
        cls.driver.implicitly_wait(5)
        return MainPage(cls.driver)
예제 #11
0
 def __init__(self):
     self.dos = DosCmd()
     self.ini = ReadIni(
         file_name=
         r"D:\测试\自动化测试\App自动化测试\appium--慕课网登录模块\config\userconfig.ini")
예제 #12
0
class Server(object):
    def __init__(self):
        self.dos = DosCmd()
        self.ini = ReadIni(
            file_name=
            r"D:\测试\自动化测试\App自动化测试\appium--慕课网登录模块\config\userconfig.ini")

    def kill_node_server(self):
        server_list = self.dos.execute_cmd_result('tasklist | find "node.exe"')
        if len(server_list) > 0:
            self.dos.execute_cmd("taskkill -F -PID node.exe")

    def get_devices(self):
        """
        获取设备信息
        :return: 格式化后的设备信息
        """
        devices = self.dos.execute_cmd_result("adb devices")
        if len(devices) >= 2:
            result = [
                i.replace("\tdevice", "") for i in devices if "\tdevice" in i
            ]
        else:
            result = None
        return result

    def connect_device(self, devices_list):
        for i in devices_list:
            self.dos.execute_cmd("adb connect " + i)

    def create_port_list(self, start_port):
        """获取port"""
        p = Port()
        port_list = p.create_port_list(start_port, self.get_devices())
        return port_list

    def create_command_list(self):
        """创建一个启动appium的cmd命令"""
        command_list = []
        appium_port_list = self.create_port_list(4700)
        bootstrap_port_list = self.create_port_list(4900)
        devices_list = self.get_devices()
        self.connect_device(devices_list)
        for i in range(len(devices_list)):
            self.ini.write_ini("server_port", "appium_port" + str(i),
                               str(appium_port_list[i]))
            self.ini.write_ini("server_port", "bootstrap_port" + str(i),
                               str(bootstrap_port_list[i]))
            self.ini.write_ini("server_port", "devices" + str(i),
                               str(devices_list[i]))
            command = "appium -p " + str(appium_port_list[i]) + " -bp " + str(
                bootstrap_port_list[i]) + " -U " + str(
                    devices_list[i]) + " --no-reset --session-override"
            command_list.append(command)
        return command_list

    def start_server(self, i):
        self.dos.execute_cmd(i)

    def start_server_thread(self):
        self.kill_node_server()
        for i in self.create_command_list():
            self.appium_start = threading.Thread(target=self.start_server,
                                                 args=(i, ))
            self.appium_start.start()
        time.sleep(10)
예제 #13
0
 def __init__(self):
     self.dos = DosCmd()
     self.device_list = self.get_devices()
     self.write_file = WriteUserCommand()
     self.base_driver = BaseDriver()
예제 #14
0
class Server:
    def __init__(self):
        self.dos = DosCmd()
        self.device_list = self.get_devices()
        self.write_file = WriteUserCommand()
        self.base_driver = BaseDriver()

    def get_devices(self):
        """
        获得设备信息
        :return: 设备list
        """
        devices_list = []
        result_list = self.dos.excute_cmd_result("adb devices")

        if len(result_list) >= 2:
            for i in result_list:
                if 'List' in i:
                    continue
                devices_info = i.split('\t')
                if devices_info[1] == "device":
                    devices_list.append(devices_info[0])
            return devices_list
        else:
            return None

    def create_port_list(self, start_port):
        """
        创建可用端口
        :param start_port:
        :return:
        """
        port = Port()
        port_list = []
        port_list = port.create_port_list(start_port, self.device_list)
        return port_list

    def create_command(self, i):
        """
        根据设备个数,生成最终启动appium的命令行list
        :return:
        """
        # appium -p 4700 -bp 4701 -U 127.0.0.1:21503
        command_list = []
        port_list = self.create_port_list(4700)
        bp_list = self.create_port_list(4900)
        device_list = self.device_list

        command = "appium -p " + str(port_list[i]) + " -bp " + str(
            bp_list[i]
        ) + " -U " + device_list[i] + " --no-reset --session-override"
        command_list.append(command)
        self.write_file.write_data(i, device_list[i], str(bp_list[i]),
                                   str(port_list[i]))
        return command_list

    def start_server(self, i):
        """
        启动服务
        :param i:控制执行启动服务的次数,如果只有一个设备,则只启动一次服务,以此类推
        :return:
        """
        self.command_list = self.create_command(i)
        self.dos.excute_cmd(self.command_list[0])

    def kill_server(self):
        """
        杀掉appium进程
        :return:
        """
        server_list = self.dos.excute_cmd_result("ps aux |  grep appium")
        if len(server_list) > 0:
            self.dos.excute_cmd("pkill -f appium")

    def main(self):
        """
        多线程启动appium服务
        :return:
        """
        self.kill_server()
        self.write_file.clear_data()
        for i in range(len(self.device_list)):
            appium_start = threading.Thread(target=self.start_server,
                                            args=(i, ))
            appium_start.start()
            time.sleep(10)