class BusinessApi(HttpHandler): def __init__(self): super(BusinessApi, self).__init__() self.http = HttpHandler() self.config = Config() self.read = ExcelData self.mysql = MysqlData() self.url = self.config.get_config('HTTP', 'baseurl') # 4.0PC网站地址 self.appapi_url = self.config.get_config('HTTP', 'appapi') # 4.0-appapi接口地址 self.webapi_url = self.config.get_config('HTTP', 'webapi') # 4.0-webapi接口地址 self.username = self.config.get_config('DATABASE', 'username') self.password = self.config.get_config('DATABASE', 'password') self.login_path = self.config.get_config('DATABASE', 'login_path') # 登录接口API self.data_address = self.config.get_config('DATABASE', 'data_address') self.port = self.config.get_config('HTTP', 'port') self.post_data = { "cmd": "login", "params": { "login_type": "a7d3bbc5-dcb7-53ff-2df1-af203a832b52", "account": self.username, "password": self.password, "openid": "" } } def get_token(self): ''' 获取登录的token 并更新配置文件config.ini token和loginuid数据''' # print(self.post_data) # print(self.url_4 + self.login_path) post_data = json.dumps(self.post_data) resp = json.loads( self.http.post(self.webapi_url + self.login_path, data=post_data)) # print(resp) if self.get_value(resp, 'info') == '登录成功!': token_data = str(self.http.get_value(resp, 'logintoken')) loginuid_data = str(self.http.get_value(resp, 'loginuid')) self.config.set_config('DATABASE', 'token', token_data) self.config.set_config('DATABASE', 'loginuid', loginuid_data) return loginuid_data, token_data else: print('登录失败或接口错误') def create_activity_haixuan(self): ''' :return: 用户登录 ''' data = self.read.get_config('create_appapi') print(data) select_data = "SELECT tc002_name, tc002_login_token from gt002_user_auth where tc002_name = %s" % ''
class AppBusiness(Client): def __init__(self): super(Client, self).__init__() self.config = Config() self.client = Client() self.username = self.config.get_config('DATABASE', 'username') self.password = self.config.get_config('DATABASE', 'password') # self.driver = self.client.driver def login(self): self.client.send_keys('com.greattone.greattone:id/et_name', u'艾丹特纳') self.client.send_keys('com.greattone.greattone:id/et_password', '123456') self.client.click('com.greattone.greattone:id/btn_sign_in')
class ExcelData(Config): def __init__(self, table_name): super(ExcelData, self).__init__() self.config = Config() self.data_address = self.config.get_config('DATABASE', 'data_address') self.workbook = xlrd.open_workbook(self.data_address, encoding_override='utf-8') self.table = self.workbook.sheet_by_name(table_name) # def rowNum(self): # self.rowNum = self.table.nrows # 获取行数量 def readData(self): """ :param table_name: 工作表名称 :return: 以list返回每个工作表中的所有数据 """ self.row = self.table.row_values(0) # 获取行title self.rowNum = self.table.nrows # 获取行数量 self.colNum = self.table.ncols # 获取列数量 self.curRowNo = 1 # the current column r = [] while self.hasNext(): s = {} col = self.table.row_values(self.curRowNo) i = self.colNum for x in range(i): s[self.row[x]] = col[x] r.append(s) self.curRowNo += 1 # print(r) # print('\n') return r def hasNext(self): if self.rowNum == 0 or self.rowNum <= self.curRowNo: return False else: return True # if __name__ == '__main__': # f = ExcelData('create_appapi') # f.readData()
class MysqlData(Config): def __init__(self): super(MysqlData, self).__init__() self.config = Config() self.mysql_config = { 'host': self.config.get_config('MYSQL', 'hostname'), 'user': self.config.get_config('MYSQL', 'user'), 'passwd': self.config.get_config('MYSQL', 'password'), 'db': self.config.get_config('MYSQL', 'db'), 'port': int(self.config.get_config('MYSQL', 'port')), 'charset': self.config.get_config('MYSQL', 'charset'), } def connect(self): try: self.conn = pymysql.connect(**self.mysql_config) return self.conn except pymysql.Error: logging.error('connect to ' + self.mysql_config.get('host') + ' Failed') logging.exception('exception message:') return False def selectOne(self, sql): logging.info('Execute sql: ' + sql) self.conn = self.connect() self.cur = self.conn.cursor() try: self.cur.execute(sql) self.cur.close() self.conn.commit() self.result = self.cur.fetchone() # print(self.result) return self.result except Exception as err: self.conn.rollback() raise err finally: self.cur.close() def selectAll(self, sql): logging.info('Execute sql: ' + sql) self.conn = self.connect() self.cur = self.conn.cursor() try: self.cur.execute(sql) self.cur.close() self.conn.commit() self.result = self.cur.fetchall() # print(self.result) return self.result except Exception as err: self.conn.rollback() raise err finally: self.cur.close() def delete(self, sql): logging.info('Execute sql: ' + sql) self.conn = self.connect() self.cur = self.conn.cursor() try: self.cur.execute(sql) self.cur.close() self.conn.commit() self.result = self.cur.fetchone() # print(self.result) return self.result except Exception as err: self.conn.rollback() raise err finally: self.cur.close()
class BusinessApi(HttpHandler): def __init__(self): super(BusinessApi, self).__init__() self.http = HttpHandler() self.config = Config() self.read = ExcelData self.mysql = MysqlData() self.url = self.config.get_config('HTTP', 'baseurl') # 4.0PC网站地址 self.appapi_url = self.config.get_config('HTTP', 'appapi') # 4.0-appapi接口地址 self.webapi_url = self.config.get_config('HTTP', 'webapi') # 4.0-appapi接口地址 self.username = self.config.get_config('DATABASE', 'username') self.password = self.config.get_config('DATABASE', 'password') self.login_path = self.config.get_config('DATABASE', 'login_path') # 登录接口API self.data_address = self.config.get_config('DATABASE', 'data_address') self.port = self.config.get_config('HTTP', 'port') self.post_data = { "cmd": "login", "params": { "login_type": "a7d3bbc5-dcb7-53ff-2df1-af203a832b52", "account": self.username, "password": self.password, "openid": "" } } def get_token(self): ''' 获取登录的token 并更新配置文件config.ini token和loginuid数据''' # print(self.post_data) # print(self.url_4 + self.login_path) post_data = json.dumps(self.post_data) resp = json.loads( self.http.post(self.webapi_url + self.login_path, data=post_data)) # print(resp) if self.get_value(resp, 'info') == '登录成功!': token_data = str(self.http.get_value(resp, 'logintoken')) loginuid_data = str(self.http.get_value(resp, 'loginuid')) self.config.set_config('DATABASE', 'token', token_data) self.config.set_config('DATABASE', 'loginuid', loginuid_data) return loginuid_data, token_data else: print('登录失败或接口错误') def login(self, userid, password): ''' :return: 用户登录 ''' data = { "cmd": "login", "params": { "login_type": "a7d3bbc5-dcb7-53ff-2df1-af203a832b52", "account": "Yilia", "password": "******", "openid": "" } } select_data = "SELECT tc002_name, tc002_login_token from gt002_user_auth where tc001_user_id = %s" % userid select_data = self.change_list(select_data) data['params']['account'] = select_data[0] data['params']['password'] = password data = json.dumps(data) result = json.loads( self.http.post(self.webapi_url + self.login_path, data=data)) return self.http.get_value(result, 'logintoken') def write_user(self, num): ''' :param num: 写入第几个工作簿 :return: ''' # print(len(data_user)) # print(data_user[0]) self.data_user = ExcelData('user').readData() self.write0 = WriteData(num) i = 2 j = 3 l = 4 k = 1 for data in self.data_user: data = data.get('body') # print(data) data = ast.literal_eval(data) resp = json.loads(self.http.post(self.url, data=data)) # print(resp ) if self.http.get_value(resp, 'err_msg') == 'success': self.write0.write(k, j, self.http.get_value(resp, 'userid')) # print(k,j,self.http.get_value(resp, 'userid')) self.write0.write(k, i, self.http.get_value(resp, 'token')) # print(k,i,self.http.get_value(resp, 'token')) self.write0.write(k, l, self.http.get_value(resp, 'err_msg')) k += 1 bookname = random.randint(0, 100) self.write0.save('{}.xls'.format(bookname)) def write_body(self, params, num): ''' 将body写入excel中 :param params: 需要传入参数,比如礼物列表活动类型 :param num: 写入工作簿的第几个表格 :return: ''' self.get_user = self.read('user').readData() write2 = WriteData(num) dict_body = collections.OrderedDict() k = 1 i = 2 for data in self.get_user: self.loginuid = data.get('loginuid') self.logintoken = data.get('token') if self.loginuid != '': self.loginuid = int(self.loginuid) dict_body['loginuid'] = self.loginuid dict_body['logintoken'] = self.logintoken dict_body['params'] = params body = json.dumps(dict_body) # body = str(dict_body) write2.write(k, i, body) # print(body) k += 1 bookname = random.randint(0, 100) write2.save('{}.xls'.format(bookname)) # return dict_body def repeat_test(self, items): ''' 判断列表中是否有重复的数据 :param items: 列表 :return: ''' if len(items) > 0: for item in items: if items.count(item) != 1: print('detail_id重复的数据有{}'.format(item)) return False else: print('detail_id没有重复的数据') return True else: return "列表没有数据" def data_contrast(self, sql, result, data=None): ''' 对比数据库的数据和接口请求数据是否相同 :param sql: 查询数据库语句 :param result: 调用接口数据 :param data: 对比数据对象 :return: ''' i = 0 select_data = self.mysql.selectAll(sql) # print(select_data) if type(result) == list: for item in result: if item == select_data[0][i]: return True else: print('接口获取数据{},和查询数据{} 不相等'.format(item, select_data)[0][i]) return False else: print(select_data) for item in self.http.get_value(result, 'data'): print(item, select_data[0][i]) if item.get(data) == select_data[0][i]: return True else: print('接口获取数据{},和查询数据{} 不相等'.format(item, select_data)[0][i]) return False i += 1 def change_list(self, sql): ''' :param sql: sql语句 :return: 将tuple转为list ''' select_data = self.mysql.selectAll(sql) list_datas = [] if len(select_data) == 1: for item in select_data[0]: list_datas.append(item) return list_datas else: for item in select_data: list_datas.append(item[0]) return list_datas def api_status_check(self, resp): ''' :param resp: 接口请求返回数据 :return: ''' err_msg = self.http.get_value(resp, 'err_msg') if resp != 200: if err_msg == 'success': return True elif err_msg == 'error' and self.http.get(resp, 'info') == '没有更多信息': return True elif err_msg == 'error' and self.http.get(resp, 'info') == '没有更多信息': return True else: print(resp) return False
class UserData(HttpHandler): def __init__(self): super(UserData, self).__init__() self.mysql = MysqlData() self.http = HttpHandler() self.config = Config() self.url_4 = self.config.get_config('HTTP', 'baseurl_dev') # 4.0接口地址 self.login_path = self.config.get_config('DATABASE', 'login_path') # 登录接口API def normal(self): ''' 爱乐人 :return: 用户id和token ''' post_data = { "cmd": "login", "params": { "login_type": "a7d3bbc5-dcb7-53ff-2df1-af203a832b52", "account": "qingqing", "password": "******", "openid": "" } } post_data = json.dumps(post_data) result = json.loads(self.http.post(self.url_4 + self.login_path, data=post_data)) return self.http.get_value(result, 'loginuid'),self.http.get_value(result, 'logintoken') def unteacher(self): ''' 未认证老师 :return: 用户id和token ''' post_data = { "cmd": "login", "params": { "login_type": "a7d3bbc5-dcb7-53ff-2df1-af203a832b52", "account": "苏丹", "password": "******", "openid": "" } } post_data = json.dumps(post_data) result = json.loads(self.http.post(self.url_4 + self.login_path, data=post_data)) return self.http.get_value(result, 'loginuid'), self.http.get_value(result, 'logintoken') def teacher(self): ''' 已认证老师 :return: 用户id和token ''' post_data = { "cmd": "login", "params": { "login_type": "a7d3bbc5-dcb7-53ff-2df1-af203a832b52", "account": "张芳", "password": "******", "openid": "" } } post_data = json.dumps(post_data) result = json.loads(self.http.post(self.url_4 + self.login_path, data=post_data)) return self.http.get_value(result, 'loginuid'), self.http.get_value(result, 'logintoken') def unstore(self): ''' 未认证琴行 :return: 用户id和token ''' post_data = { "cmd": "login", "params": { "login_type": "a7d3bbc5-dcb7-53ff-2df1-af203a832b52", "account": "维也纳钢琴", "password": "******", "openid": "" } } post_data = json.dumps(post_data) result = json.loads(self.http.post(self.url_4 + self.login_path, data=post_data)) return self.http.get_value(result, 'loginuid'), self.http.get_value(result, 'logintoken') def checkstore(self): ''' 已认证琴行 :return: 用户id和token ''' post_data = { "cmd": "login", "params": { "login_type": "a7d3bbc5-dcb7-53ff-2df1-af203a832b52", "account": "正悦琴行", "password": "******", "openid": "" } } post_data = json.dumps(post_data) result = json.loads(self.http.post(self.url_4 + self.login_path, data=post_data)) return self.http.get_value(result, 'loginuid'), self.http.get_value(result, 'logintoken') def vipstore(self): ''' vip琴行 :return: 用户id和token ''' post_data = { "cmd": "login", "params": { "login_type": "a7d3bbc5-dcb7-53ff-2df1-af203a832b52", "account": "伽音琴行", "password": "******", "openid": "" } } post_data = json.dumps(post_data) result = json.loads(self.http.post(self.url_4 + self.login_path, data=post_data)) return self.http.get_value(result, 'loginuid'), self.http.get_value(result, 'logintoken')
class Client: def __init__(self): super(Client, self).__init__() self.config = Config() self.desired_caps = { 'platformName': self.config.get_config('DESIRED_CAPS', 'platformName'), 'platformVersion': self.config.get_config('DESIRED_CAPS', 'platformVersion'), 'deviceName': self.config.get_config('DESIRED_CAPS', 'deviceName'), 'appPackage': self.config.get_config('DESIRED_CAPS', 'appPackage'), 'appActivity': self.config.get_config('DESIRED_CAPS', 'appActivity'), # 'udid': '127.0.0.1:4723', 'unicodeKeyboard': True, 'resetKeyboard': True, 'autoGrantPermissions': True, 'automationName': 'uiautomator2', 'app': PATH("packages/apps-release.apk") } self.driver_url = self.config.get_config('APP_DATABASE', 'driver_url') # self.driver = Remote("http://127.0.0.1:4723/wd/hub", self.desired_caps) self.driver = Remote('{}'.format(self.driver_url), self.desired_caps) self.driver.implicitly_wait(15) self.wait = WebDriverWait def find(self, xpath): result = self.driver.find_element_by_xpath(xpath).text return result def click(self, res): try: self.driver.find_element_by_id(res).click() except: self.driver.find_element_by_xpath(res).click() def xpath(self, xpath): self.driver.find_element_by_xpath(xpath).click() def send_keys(self, res, keywords): try: self.driver.find_element_by_id(res).send_keys(keywords) except: self.driver.find_element_by_xpath(res).send_keys(keywords) def swipe(self, res): width = self.driver.get_window_rect()['width'] height = self.driver.get_window_rect()['height'] i = 0 while i < 10: try: self.driver.find_element_by_id(res).is_enabled() break except Exception as e: self.driver.swipe(width / 2, height * 0.8, width / 2, height * 0.2) i = i + 1