def __init__(self): dr = BaseDriver() self.driver = dr.android_driver(0) #这里参数本来是i,因为不知道怎么传,暂时0代替 self.gd = GetData() self.gb = GetByLocator(self.driver) self.ex = OperExcel( excel_path=r'D:\Job\python\Script\Lipei_app\data\KW_Mode.xlsx')
class SinaSendChapterBusiness(object): ''' 处理新浪发布文章流程 ''' def __init__(self, i): self.driver = BaseDriver().android_driver( i) # driver都从business生产并传递,底层不重复生产 self.sch = SinaChapterHandle(self.driver) self.shh = SinaHomeHandle(self.driver) self.oe = OperExcel() self.ss = SysSdk(self.driver) self.cp = SinaChoosePicsBusiness(self.driver) def send_chapter(self, title, content): ''' 发布文章流程 :return: ''' self.shh.click_increate() self.shh.click_chapter() self.sch.input_title(title) self.sch.input_content(content) time.sleep(10) self.sch.click_pic() time.sleep(1) self.sch.choose_pic(self.ss.get_sys_pic_activity()) self.sch.click_next() time.sleep(1) self.sch.save_chapter() time.sleep(5) self.sch.send_chapter() time.sleep(8) #self.shh.driver_close() def circl_send_sina(self): ''' 根据excel中数据行数,循环发帖 :return: ''' line = self.oe.excel_get_lines() j = 0 for i in range(1, line): #过滤掉第一行 if j < 10: # 限制数目 title = self.oe.excel_get_cell(i, 0) conte = self.oe.excel_get_cell(i, 1) content = conte + "\n联系电话(微信):19981203720" run = self.oe.excel_get_cell(i, 3) if run == 'Y': self.send_chapter(title, content) time.sleep(12) #增加每条发布时间间隔,太长了,会是去连接的 j = +1 def set_input(self): ''' 设置输入法为第一个默认输入法 :return: ''' input_list = self.driver.available_ime_engines #注意这里是不用(),源代码:return self.execute(Command.GET_AVAILABLE_IME_ENGINES, {})['value'] self.driver.activate_ime_engine(input_list[0]) #设置为第一个输入法
def __init__(self, i): self.driver = BaseDriver().android_driver( i) # driver都从business生产并传递,底层不重复生产 self.sch = SinaChapterHandle(self.driver) self.shh = SinaHomeHandle(self.driver) self.oe = OperExcel() self.ss = SysSdk(self.driver) self.cp = SinaChoosePicsBusiness(self.driver)
def __init__(self): self.oe = OperExcel( excel_path=r'D:\Job\python\Script\Lipei_app\data\KW_Mode.xlsx')
class GetData(object): def __init__(self): self.oe = OperExcel( excel_path=r'D:\Job\python\Script\Lipei_app\data\KW_Mode.xlsx') def key_word(self, row): ''' row--行这个数据需要单独传入 关键字数据获取封装 :return: ''' return self.oe.excel_get_cell(row, 4) def key_element(self, row): ''' row--行这个数据需要单独传入 关键字操作对象元素数据获取封装 :param row: :return: ''' temp = self.oe.excel_get_cell(row, 5) if temp == '': # 处理可能出现的空值 return None else: return temp def key_element_value(self, row): ''' row--行这个数据需要单独传入 关键字操作对象元素的值数据获取封装 :param row: :return: ''' temp = self.oe.excel_get_cell(row, 6) if temp == '': return None else: return temp def Expected_Key_Word(self, row): ''' row--行这个数据需要单独传入 期望关键字获取封装 :param row: :return: ''' temp = self.oe.excel_get_cell(row, 7) if temp == '': return None else: return temp def Expected_Key_Element(self, row): ''' row--行这个数据需要单独传入 期望关键字操作对象元素数据获取封装 :param row: :return: ''' temp = self.oe.excel_get_cell(row, 8) if temp == '': return None else: return temp def Expected_Key_value(self, row): ''' row--行这个数据需要单独传入 期望关键字获取封装 :param row: :return: ''' temp = self.oe.excel_get_cell(row, 9) if temp == '': return None else: return temp def exec_status(self, row): ''' 用例执行状态 :return: ''' temp = self.oe.excel_get_cell(row, 10) if temp == '': return None else: return temp def get_lines(self): ''' 获取当前sheet的行数 :return: ''' return self.oe.excel_get_lines()
class KWRunMain(object): def __init__(self): dr = BaseDriver() self.driver = dr.android_driver(0) #这里参数本来是i,因为不知道怎么传,暂时0代替 self.gd = GetData() self.gb = GetByLocator(self.driver) self.ex = OperExcel( excel_path=r'D:\Job\python\Script\Lipei_app\data\KW_Mode.xlsx') def run_main(self, i): ''' 执行单个用例 :return: ''' ak = AlwinKeyWord(self.driver) key_words = self.gd.key_word(i) key_element = self.gd.key_element(i) key_element_value = self.gd.key_element_value(i) expect_key_word = self.gd.Expected_Key_Word(i) expect_key_element = self.gd.Expected_Key_Element(i) expect_key_value = self.gd.Expected_Key_value(i) if key_element != None: #关键字操作对象不为空 key_ele0, key_ele1 = list(key_element.split(",")) # tuple转list run_key_word = getattr(ak, key_words) #反射 if key_element_value != None: # 关键字操作对象的值不为空 run_key_word(key_ele0, key_ele1, key_element_value) else: run_key_word(key_ele0, key_ele1) elif key_element_value != None: #仅有关键字和值,如alwin_sleep run_key_word = getattr(ak, key_words) run_key_word(key_element_value) if expect_key_word != None: expect_ele0, expect_ele1 = list(expect_key_element.split(",")) expect_kw = getattr(ak, expect_key_word) if expect_key_value != None: # 既然expect_key_word不为空,那么expect_key_element肯定不会为空,只有expect_key_value可能为空 result = expect_kw(expect_ele0, expect_ele1, expect_key_value) else: result = expect_kw(expect_ele0, expect_ele1) self.write_status(i, result) def write_status(self, row, result): ''' 写入用例执行状态 row:行 col:列 :return: ''' # if result: #假如result有值,这里需要写入pass # self.ex.excel_write_data(row,10,'pass') #这里需要写入pass # else: # self.ex.excel_write_data(row, 10, 'fail') #这里需要写入fail self.ex.excel_write_data( row, 10, 'pass') if result else self.ex.excel_write_data( row, 10, 'fail') #三目运算符 def for_run(self): ''' 循环执行用例 :return: ''' row_count = self.gd.get_lines() for i in range(1, row_count): self.run_main(i) time.sleep(2) self.end_run() def end_run(self): ''' 关闭驱动,结束测试 :return: ''' self.driver.quit()
def __init__(self): self.oe = OperExcel(self.excel_path())
class Driver(object): def __init__(self): self.oe = OperExcel(self.excel_path()) def get_driver(self): capabilities = { "platformName": "Android", "unicodeKeyboard": 'true', "deviceName": "3XU9X17324W10646", "noReset": 'true', "platformVersion": "6.0", "appPackage": "com.sina.weibo", "appActivity": "com.sina.weibo.SplashActivity" # appium1.7版本之后就应该不需要改配置了 } # 对应的cmd中需先执行:appium -p 4723 -bp 4724 -U 3XU9X17324W10646 driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", capabilities) sleep(8) # 登录后进入发布界面 return driver def send_new(self, driver, title, content): tel = '\n李老师电话:19981203720' content = content + tel self.gl = GetByLocator(driver) #注意这里不要放入init,会导致重复执行 self.gl.get_locator('sina_home', 'increase').click() self.gl.get_locator('sina_home', 'chapter').click() sleep(8) self.gl.get_locator('sina_editchapter', 'title').clear() self.gl.get_locator('sina_editchapter', 'title').send_keys(title) self.gl.get_locator('sina_editchapter', 'content').clear() self.gl.get_locator('sina_editchapter', 'content').send_keys(content) sleep(8) self.gl.get_locator('sina_editchapter', 'next').click() #这里有问题 sleep(2) self.gl.get_locator('sina_editchapter', 'save').click() sleep(2) self.gl.get_locator('sina_editchapter', 'titleSave').click() sleep(8) def excel_path(self): ''' 通过相对路径获取目标excel 通过join来拼接绝对地址 :return: ''' excel_dir = os.path.abspath( os.path.join(os.path.dirname(__file__), '../data/Tiezi.xlsx')) return excel_dir def circl_send(self): ''' 根据excel中数据行数,循环发帖 :return: ''' driver = self.get_driver() line = self.oe.excel_get_lines() j = 0 for i in range(1, line): if j < 10: # 限制数目 title = self.oe.excel_get_cell(i, 0) conte = self.oe.excel_get_cell(i, 1) run = self.oe.excel_get_cell(i, 3) if run == 'Y': self.send_new(driver, title, conte) j = +1 def set_input(self): ''' 设置输入法为第一个默认输入法 :return: ''' driver = self.get_driver() input_list = driver.available_ime_engines #注意这里是不用(),源代码:return self.execute(Command.GET_AVAILABLE_IME_ENGINES, {})['value'] driver.activate_ime_engine(input_list[0]) #设置为第一个输入法
def __init__(self): self.oe = OperExcel() self.cmd = Cmd()
class Driver(object): def __init__(self): self.oe = OperExcel() self.cmd = Cmd() def start_appium(self): self.cmd.appium_start() def get_driver(self): capabilities = { "platformName": "Android", "resetKeyboard": "true", "unicodeKeyboard": 'true', "deviceName": "3XU9X17324W10646", "noReset": 'true', "appPackage": "com.ss.android.article.news", "appActivity": "com.ss.android.article.news.activity.SplashBadgeActivity" # appium1.7版本之后就应该不需要改配置了 } # 对应的cmd中需先执行:appium -p 4723 -bp 4724 -U 3XU9X17324W10646 driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", capabilities) sleep(8) driver.find_element_by_xpath( "//android.widget.RelativeLayout[@index='4']").click() sleep(3) driver.find_element_by_xpath( "//android.widget.LinearLayout[@index='6']").click() sleep(3) return driver def new_send(self, driver, title, conten): driver.find_element_by_xpath( "//android.widget.ImageView[@index='0']/../following-sibling::android.view.ViewGroup/android.widget.ImageView" ).click() sleep(10) driver.find_element_by_xpath( "//android.widget.EditText[@index='0']").clear() driver.find_element_by_xpath( "//android.widget.EditText[@index='0']").send_keys(title) sleep(5) tel = '\n李老师电话:19981203720' conten = conten + tel driver.find_element_by_xpath( "//android.widget.EditText[@index='0']/../following-sibling::android.view.View/android.view.View/android.view.View" ).clear() driver.find_element_by_xpath( "//android.widget.EditText[@index='0']/../following-sibling::android.view.View/android.view.View/android.view.View" ).send_keys(conten) sleep(10) # driver.find_element_by_xpath("//android.widget.TextView[@index='0']/../../following-sibling::android.widget.LinearLayout/android.widget.RelativeLayout/android.widget.RelativeLayout/android.widget.LinearLayout/android.widget.ImageButton[@index='1']").send_keys(r"D:\Job\python\Script\Robot\Lipei_auto\data\pic\baoming.jpg") # 上面这个元素定位已验证没问题 但是appium中只能通过相册选择图片附件 # driver.find_element_by_xpath( # "//android.widget.TextView[@index='0']/../../following-sibling::android.widget.LinearLayout/android.widget.RelativeLayout/android.widget.RelativeLayout/android.widget.LinearLayout/android.widget.ImageButton[@index='1']").click() # driver.wait_activity("com.test.camera", 2) # # 下方发图暂时未成功 # driver.find_element_by_xpath("//com.ss.android.article.news:id/ccr[@index='1']").click() # driver.find_element_by_xpath("//com.ss.android.article.news:id/ba3[@index='1']").click() # # pic_list = driver.find_elements_by_xpath("//com.ss.android.article.news:id/ba3") # sleep(10) driver.find_element_by_xpath( "//android.widget.TextView[@index='2']").click() sleep(3) def circl_send(self): ''' 根据excel中数据行数,循环发帖 :return: ''' driver = self.get_driver() line = self.oe.excel_get_lines() for i in range(1, line): title = self.oe.excel_get_cell(i, 0) conte = self.oe.excel_get_cell(i, 1) run = self.oe.excel_get_cell(i, 3) if run == 'Y': self.new_send(driver, title, conte) def excel_path(self): ''' 通过相对路径获取目标excel 通过join来拼接绝对地址 :return: ''' excel_dir = os.path.abspath( os.path.join(os.path.dirname(__file__), '../data/Tiezi.xlsx')) return excel_dir