Beispiel #1
0
 def quit(self):
     """
     Close the session.
     """
     self.generate_adb_logs()
     log_by_run_time("End.\n\nRun %d cases, %d passed." % (len(self.process_list), self.pass_count))
     self.driver.quit()
Beispiel #2
0
    def start_appium(self):
        """
        根据端口启动一个appium server
        """
        # 检查是否已有指定端口的服务在运行
        status, output = commands.getstatusoutput('ps aux | grep "lib/server/main.js"')
        output_lines = output.split("\n")
        pid = None
        line_tag = False
        for line in output_lines:
            if line.find("node") != -1 and line.find(str(self.port)) != -1:
                params = line.split(" ")
                for i in range(1, len(params)):
                    if params[i].strip():
                        pid = params[i]
                        line_tag = True
                        break
            if line_tag:
                break
        if pid:
            log_by_run_time("Kill original running appium with port " + str(self.port))
            status, output = commands.getstatusoutput("kill -9 " + pid)
            log_by_run_time("Kill result: " + str(status) + " " + str(output))

        appium_log_file = open(self.appium_log_path, "w+")
        subprocess.Popen(
            "sh /Users/wgx/.wgxtools/appium_start.sh " + str(self.port) + " " + str(self.device_name),
            shell=True,
            stdout=appium_log_file,
            stderr=appium_log_file,
        )

        # 等待服务启动
        sleep(8)
Beispiel #3
0
 def start(self):
     """
     Start.
     """
     for process in self.process_list:
         try:
             process.start()
             self.pass_count += 1
         except Exception as e:
             # 出现异常,输出log
             self.generate_adb_logs()
             log_by_run_time(e.message)
             log_by_run_time(traceback.format_exc())
     self.quit()
Beispiel #4
0
 def delay(seconds):
     """
     延时
     """
     log_by_run_time("delay : " + str(seconds))
     sleep(seconds)
Beispiel #5
0
 def log(self, text):
     log_by_run_time(str(self.robot.opt_count) + " : " + str(text))
Beispiel #6
0
    def __init__(self, port, device_name, need_appium_server=True):
        """
        初始化,Appium默认支持多设备,所以如果存在多个设备需要建立多个Appium server,每个端口对应一个设备
        :param port:    Appium server 的端口
        :param device_name: 设备名称
        :param need_appium_server   是否需要一个启动一个appium的server
        注意:如果指定端口的server已存在,则会干掉原来的进程,意味着原server上的测试会挂掉
        """
        self.port = port
        self.device_name = device_name
        self.run_id = get_run_id()

        # 新建一个appium server,每次执行对应一个,port不能重复
        if need_appium_server:
            self.appium_log_path = os.path.join(APPIUM_LOG_DIR_ROOT, self.run_id + ".log")
            log_by_run_time("Create appium log path : " + os.path.abspath(self.appium_log_path))
            self.start_appium()

        # 默认的配置,可以重写Eva来修改定制
        desired_caps = {"platformName": "Android"}
        desired_caps["platformVersion"] = self.platform_version
        desired_caps["deviceName"] = self.device_name
        desired_caps["app"] = os.path.abspath(os.path.join(os.path.dirname(__file__), self.apk_path))
        desired_caps["appPackage"] = self.package_name
        desired_caps["appActivity"] = self.launch_activity

        log_by_run_time("Connect to device ... ")
        self.driver = webdriver.Remote("http://localhost:%d/wd/hub" % int(self.port), desired_caps)

        # log_by_run_time('context : ' + str(self.driver.contexts))
        # self.driver.switch_to.context('WEBVIEW_1')
        # log_by_run_time('context : ' + str(self.driver.contexts))

        # 如果发现已安装,先卸载旧的
        log_by_run_time("Reinstall ... ")
        if self.driver.is_app_installed(self.package_name):
            # Appium的uninstall在多设备时有bug
            # self.driver.remove_app(self.package_name)
            commands.getstatusoutput("adb -s %s uninstall %s" % (self.device_name, self.package_name))
        # install 也不好使,醉了
        # self.driver.install_app(os.path.abspath(self.apk_path))
        commands.getstatusoutput("adb -s %s install %s" % (self.device_name, self.apk_path))

        # 事件、事件计数
        self.action = TouchAction(self.driver)
        self.opt_count = 0

        # 建立图片log的目录,根据执行时间
        self.png_log_dir = os.path.join(PNG_DIR_ROOT, self.run_id)
        self.adb_log_dir = os.path.join(ADB_LOG_DIR_ROOT, self.run_id)
        log_by_run_time("Create png log path : " + os.path.abspath(self.png_log_dir))
        log_by_run_time("Create adb log path : " + os.path.abspath(self.adb_log_dir))
        if not os.path.exists(self.png_log_dir):
            os.system("mkdir -p " + self.png_log_dir)
            os.system("mkdir -p " + self.adb_log_dir)

        # 启动首页,因为卸载过,原来的首页已经被干掉了
        self.driver.start_activity(self.package_name, self.launch_activity)

        # 获取分辨率,可能不太准,是最外层layout的大小
        eles = self.driver.find_elements_by_xpath(utils.android_framelayout)
        self.max_x = eles[0].size["width"]
        self.max_y = eles[0].size["height"]
        log_by_run_time("Screen size(px) : (%d, %d)" % (self.max_x, self.max_y))

        # 待执行的过程,应该是一堆Process的实例
        self.process_list = []
        self.pass_count = 0