def read_linux(file_path, host_name=helper.read_config_item("linux", "host_name"), username=helper.read_config_item("linux", "username"), password=helper.read_config_item("linux", "password"), port=helper.read_config_item("linux", "port")): """ # 读取Linux服务器上文件 Args: file_path: 读取的文件路径,必填项 host_name: 读取的文件路径,默认值,配置文件helper.read_config_item("linux", "host_name") username: 读取的文件路径,默认值,配置文件helper.read_config_item("linux", "username") password: 读取的文件路径,默认值,配置文件helper.read_config_item("linux", "password") port: 读取的文件路径,默认值,配置文件int(helper.read_config_item("linux", "port") Returns: 读取后的xml文件 """ client = paramiko.SSHClient() client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) port = int(port) client.connect(host_name, port, username, password, compress=True) sftp_client = client.open_sftp() path = helper.read_config_item( "linux", "file_path") + str(file_path).replace("\\", "") remote_file = sftp_client.open(path) return remote_file # 文件路径
def __init__(self, res): self.desired_caps['platformName'] = helper.read_config_item( "desired_caps", "platform_name") self.desired_caps['appPackage'] = helper.read_config_item( "desired_caps", "app_package") self.desired_caps['appActivity'] = helper.read_config_item( "desired_caps", "app_activity") self.desired_caps['noReset'] = helper.read_config_item( "desired_caps", "noreset") self.desired_caps['automationName'] = helper.read_config_item( "desired_caps", "automationName") self.res_body = json.loads(res['msg_body']) self.base_info = self.res_body['base_info'] self.case_info = self.res_body['case_info'] # self.base_info = helper.read_json_file('task_1.json')['base_info'] # self.case_info = helper.read_json_file('task_1.json')['case_info'] self.command = { "command0": 'adb shell ime list -s', # 列出手机所有的输入法 "command1": 'adb shell ime set io.appium.android.ime/.UnicodeIME', # appium输入法 "command2": 'adb shell ime set com.sohu.inputmethod.sogou/.SogouIME', # 搜狗输入法 "command3": 'adb shell ime set com.example.android.softkeyboard/.SoftKeyboard' } # 模拟器自带英文输入
def login(phone, password=helper.read_config_item("account_info", "password")): """ 登录操作 Args: phone: 电话号码,默认值配置文件的account_info的phone password: 密码,默认值配置文件的account_info的password Returns: True """ check.wait_element("n,我") oper.click("n,我") check.wait_element("i,com.xingjiabi.shengsheng:id/btnUserLogin") oper.click("i,com.xingjiabi.shengsheng:id/btnUserLogin") check.wait_element( "x,//android.widget.TextView[@resource-id='com.xingjiabi.shengsheng:id/tvXjbLoginTab']" ) oper.click( "x,//android.widget.TextView[@resource-id='com.xingjiabi.shengsheng:id/tvXjbLoginTab']" ) check.wait_element("i,com.xingjiabi.shengsheng:id/xjb_login_name") oper.clear("i,com.xingjiabi.shengsheng:id/xjb_login_name") oper.send_keys("i,com.xingjiabi.shengsheng:id/xjb_login_name", phone) oper.clear("i,com.xingjiabi.shengsheng:id/xjb_login_psd") oper.send_keys("i,com.xingjiabi.shengsheng:id/xjb_login_psd", password) oper.click("i,com.xingjiabi.shengsheng:id/xjb_login_but") check.wait_element("n,我") check.assert_exist_element("n,我")
def share_download(): """ 打开共享文件夹,从服务器下载apk文件 """ path = helper.read_config_item("apk", "path") try: oper.log("开始下载共享文件夹,读取文件...") get_file_dir_download(path) except Exception as e: oper.log("打开共享文件夹,从服务器下载文件失败:" + str(e), 2)
def initial_appium(self, port=None, device_name=None): if self.base_info['device_type'] == "1": self.desired_caps['platformVersion'] = helper.read_config_item( "emulator", "device_version") if device_name: self.desired_caps['deviceName'] = device_name else: self.desired_caps['deviceName'] = helper.read_config_item( "emulator", "device_name") if port: # 设定并发多进程的时候端口 self.desired_caps['systemPort'] = 5287 + int(port) # 是使用unicode编码方式发送字符串 self.desired_caps['unicodeKeyboard'] = True # 将键盘隐藏起来 self.desired_caps['resetKeyboard'] = False # 设备为模拟器时可以这么用 dirs = device_name.split(":")[1] else: self.desired_caps['platformVersion'] = self.base_info[ 'device_version'] self.desired_caps['deviceName'] = self.base_info['device_name'] # 是使用unicode编码方式发送字符串 self.desired_caps['unicodeKeyboard'] = True # 将键盘隐藏起来 self.desired_caps['resetKeyboard'] = False if port: # 设定并发多进程的时候端口 self.desired_caps['systemPort'] = 5287 + int(port) # 设备为真机时可以这么用 dirs = device_name # 更新共享apk与本地一致 read_files.exist_apk(str(device_name) + '/' + self.base_info['app']) self.desired_caps['app'] = helper.srcPath + '/common/' + str( dirs) + '/' + self.base_info['app'] helper.initial_appium(desired_caps=self.desired_caps, port=port) # 设置输入搜狗输入法 command = "adb -s %s shell ime set com.sohu.inputmethod.sogou/.SogouIME" % device_name os.system(command)
def read_http(file_path): """ 通过http读取xml文件 :param file_path: 读取的文件路径,必填项 :return: 读取后的xml文件 """ url = helper.read_config_item("http", "url") path = url + str(file_path).replace("\\", "") res = requests.get(path, stream=True) if res.status_code == 200: return res.raw else: oper.log("【%s】读取xml失败,失败原因:%s" % (file_path, res.text), 2) raise check.TouchException("【%s】读取xml失败" % file_path)
def send_mail(sender=helper.read_config_item("send_msg", "sender"), password=helper.read_config_item("send_msg", "password"), receivers=helper.read_config_item("send_msg", "receivers"), result=None): if result: content = """ <p style="color:#000;font-family:'微软雅黑',Helvetica,Arial,sans-serif;">尊敬的开发者:</p> <p style="color:#000;font-family:'微软雅黑',Helvetica,Arial,sans-serif;">UI自动化测试执行结果:</p> <p style="color:#000;font-family:'微软雅黑',Helvetica,Arial,sans-serif;">%s</p> <p><a href="http://j9.test.k8s.taqu.cn/tq-qtp/index" style="color: red; font-family:'微软雅黑',Helvetica,Arial,sans-serif;">请前往平台查看结果!</a></p> """ % (str(result)) else: content = """ <p style="color:#000;font-family:'微软雅黑',Helvetica,Arial,sans-serif;">尊敬的开发者:</p> <p style="color:#000;font-family:'微软雅黑',Helvetica,Arial,sans-serif;">UI自动化测试执行结果:</p> <p><a href="http://j9.test.k8s.taqu.cn/tq-qtp/index" style="color: red; font-family:'微软雅黑',Helvetica,Arial,sans-serif;">请前往平台查看结果!</a></p> """ subject = "UI自动化测试" message = MIMEText(content, 'html', 'utf-8') message['Subject'] = Header(subject, "utf-8").encode() message['From'] = _format_addr('UI自动化测试系统 <%s>' % sender) message['To'] = ';'.join(receivers) obj = smtplib.SMTP(helper.read_config_item("email", "host"), helper.read_config_item("email", "port")) obj.login(sender, password) try: obj.sendmail(sender, receivers, message.as_string()) oper.log("邮件发送成功") except smtplib.SMTPException as e: oper.log("邮件无法发送!" + str(e), 2) except Exception as e: oper.log(e, 2) finally: obj.quit()
def exist_apk(apk_name): """ 判断本地是否存在apk,如果不存在下载 :param apk_name: apk名称 :return: 更新apk """ dirs = str(apk_name).split("/")[0].split(":")[1] apk = str(apk_name).split("/")[1] local_path = helper.srcPath + '/common/' + dirs share_path = helper.read_config_item("apk", "path") if not os.path.exists(local_path): # 创建目录 os.makedirs(local_path) oper.log("创建apk目录:" + str(local_path)) file_list = os.listdir(local_path) if apk not in file_list: file_path = os.path.join(share_path, apk) copy_file_down(file_path, local_path)
def __new__(cls, log_config): mutex = threading.Lock() mutex.acquire() # 上锁,防止多线程下出问题 if not hasattr(cls, 'instance'): cls.instance = super(LogSignleton, cls).__new__(cls) helper.read_config_item("LOGGING", "max_bytes_each", log_config) # config = configparser.ConfigParser() # config.read(log_config, encoding="utf-8-sig") cls.log_filename = os.path.join(srcPath, 'log', 'touch.log') cls.instance.max_bytes_each = int( helper.read_config_item("LOGGING", "max_bytes_each", log_config)) cls.instance.backup_count = int( helper.read_config_item("LOGGING", "backup_count", log_config)) cls.instance.log_level_in_console = int( helper.read_config_item("LOGGING", "log_level_in_console", log_config)) cls.instance.log_level_in_logfile = int( helper.read_config_item("LOGGING", "log_level_in_logfile", log_config)) cls.instance.logger_name = helper.read_config_item( "LOGGING", "logger_name", log_config) cls.instance.console_log_on = int( helper.read_config_item("LOGGING", "console_log_on", log_config)) cls.instance.logfile_log_on = int( helper.read_config_item("LOGGING", "logfile_log_on", log_config)) cls.instance.level_name = helper.read_config_item( "LOG_DETAIL", "level_name", log_config) cls.instance.asc_time = helper.read_config_item( "LOG_DETAIL", "asc_time", log_config) cls.instance.project_name = helper.read_config_item( "LOG_DETAIL", "project_name", log_config) cls.instance.file_name = helper.read_config_item( "LOG_DETAIL", "file_name", log_config) cls.instance.line_no = helper.read_config_item( "LOG_DETAIL", "line_no", log_config) cls.instance.message = helper.read_config_item( "LOG_DETAIL", "message", log_config) cls.instance.logger = logging.getLogger(cls.instance.logger_name) cls.instance.__config_logger() mutex.release() return cls.instance
def case_check(): flag = helper.read_config_item("flag", "compare_flag") assert flag == "0", "用例执行失败"
#!/usr/bin/env python # encoding:utf-8 from urllib.parse import urlencode import urllib.request import json import base64 import uuid import helper import oper ENV = helper.read_config_item("soa_config", "env") SOA_CLIENT = helper.read_config_item("soa_config", "soa_client") SOA_SYSTEM = helper.read_config_item("soa_config", "soa_system") class SoaClient(object): _host_name = "" _host = "" _service = "" _method = "" @staticmethod def get_soa(host_name, service): # soa_client = json.loads(str(SOA_CLIENT), encoding='utf-8') host = SOA_CLIENT.get(host_name) return SoaClient(host_name, host, service) def __init__(self, host_name, host, service): self._host_name = host_name
def start_process(server_process, driver_process): for t in server_process: t.start() for i in driver_process: i.start() def servers(port, device_id): """ 终端命令开启appium服务 :param port: appium端口 :param device_id: 设备id """ log_path = helper.srcPath + "/log/" + str(port) + ".log" cmd = "appium --address 127.0.0.1 " + " --port " + str( port) + " --bootstrap-port " + str(int(port) - 2000) + " -U " + str( device_id) + " --session-override --log " + str(log_path) os.system(cmd) if __name__ == "__main__": devices = [] device_list = helper.read_config_item("devices") for j in range(len(device_list)): tup = (device_list[j]["port"], device_list[j]["device"]) devices.append(tup) threads = create_process(devices, oper_action.test_run) start_process(threads[0], threads[1])