def test_boostRam(self): '''To perform kill-task function''' logger().info("Launch app") self.driver.find_element_by_id('com.avast.android.mobilesecurity:id/eula_accept').click() WebDriverWait(self.driver, self.page_load_time).until(EC.element_to_be_clickable((By.ID, 'com.avast.android.mobilesecurity:id/btn_interstitial_remove_ads_continue'))) self.driver.find_element_by_id('com.avast.android.mobilesecurity:id/btn_interstitial_remove_ads_continue').click() logger().info("Entered main page") self.driver.find_element_by_id('com.avast.android.mobilesecurity:id/matrix_card_1').click() logger().info("Boost RAM function started") wait_time = 0 while True: try: self.driver.find_element_by_id('com.avast.android.mobilesecurity:id/ui_feed_header_icon') logger().info("Boost RAM function completed") self.driver.save_screenshot(self.screenshotDir + 'test_boostRam %s.png' % self.timestr) except: wait_time += 1 if wait_time == self.run_func_time: logger().info("Boost RAM function failed") self.driver.save_screenshot(self.screenshotDir + 'test_boostRam %s.png' % self.timestr) raise Exception('Boost Ram function failed') else: break self.assertIsNotNone(self.driver.find_element_by_id('com.avast.android.mobilesecurity:id/ui_feed_header_title')) self.assertIsNotNone(self.driver.find_element_by_id('com.avast.android.mobilesecurity:id/ui_feed_header_subtitle'))
def test_login(self, username, password, assert_type): se = SeleniumBase(self.driver) se.get(url_index) login(self.driver, username, password) se.add_page_screen_shot() if assert_type == '1': logger().info("断言登陆成功") text = Index_Page(self.driver).login_success() self.assertIn("张维序", text, '登陆成功断言') elif assert_type == "2": text = self.driver.find_element_by_id("submit").text self.assertIn("立即登录", text, '登陆失败断言') elif assert_type == "3": text = self.driver.find_element_by_id("submit").text self.assertIn("立即登录", text, '登陆失败断言') elif assert_type == "4": text = self.driver.find_element_by_id("submit").text self.assertIn("立即登录", text, '登陆失败断言') else: logger().info(f"未知断言类型{assert_type}") self.assertTrue(False, "未知断言类型")
def test_split(self): """测试split""" logger().info("测试split") s = 'hello world' self.assertEqual(s.split(), ['hello', 'world'], "相等") with self.assertRaises(TypeError): s.split(2)
def setUp(self): logger().info(f"appium服务器:{command_executor}") logger().info(f"appium 参数:{desired_capabilities}") self.driver = webdriver.Remote( command_executor=command_executor, desired_capabilities=desired_capabilities) self.driver.implicitly_wait(5) add_image(self.driver)
def remote_driver(self): self.driver = webdriver.Remote( command_executor='http://127.0.0.1:4444/wd/hub', desired_capabilities=DesiredCapabilities.FIREFOX ) # 若hub就部署在本机上,直接用http://127.0.0.1:4444/wd/hub即可,若非部署在本机上,则需填写hub的IP地址 logger().info("打开浏览器") # 在报告中写入流程记录 self.driver.implicitly_wait(40) self.driver.maximize_window() logger().info("浏览器最大化")
def test_error(self): """测试错误""" logger().error("测试错误") with open("baidu.png", 'rb') as f: image = base64.b64encode(f.read()) AddImage(image, "百度1") AddImage(image, "百度12") AddImage(image, "百度123") raise ValueError
def workflow_list_item_len_gt_0(self): elements = self.workflow_list_items() if len(elements) == 0: logger().info('元素列表长度为0') return False for el in self.workflow_list_items(): el_text = el.text print(el_text) if el_text is None or len(el_text) == 0: return False return True
def get_count_of_my_initiate(self): """通过接口获取我拟办的总数量""" headers = { 'Cookie': api.cookie, 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' } data = my_initiate_api_data res = requests.get(my_initiate_api_url, data=data, headers=headers) logger().info(res.text) total = res.json()['Total'] return total
def list_text(self): if api.el_is_display(self.unfinish_workflow_list_has_data): text = api.get_text(self.unfinish_workflow_list_has_data) logger().info('待办任务列表选项标题:%s' % text) if len(text) > 0: return True else: return False elif api.get_text( self.unfinish_workflow_list_no_data) == '您的待办已经全部处理完成': return True else: api.save_page_img('待办列表显示错误') return False
def test_login_4(self): username = "******" password = "" driver = self.driver open_dsc(driver) login(driver, username, password) logger().info("断言 请输入密码") text = driver.find_element_by_class_name("layermcont").text self.assertEqual("请输入密码", text) add_image(driver) pass
def news_list_item_article_len_gt_0(self): """ 判断新闻列表页选项简介长度是否大于0 :return: """ self.news_list_first_item_name_exist() elements = self.news_list_article_els() for el in elements: el_text = el.text logger().info('新闻列表选项简介:%s' % el_text) if el_text is None or len(el_text) == 0: api.save_page_img('新闻列表选项简介长度等于0') return False return True
def login(driver, username, password): """ 登录业务 :param driver:浏览器驱动 :param username:用户名 :param password:密码 :return:None """ logger().info(f"使用用户名:{username},密码:{password}进行登陆") login_page = Login_Page(driver) login_page.send_username(username) login_page.send_password(password) login_page.submit() time.sleep(2)
def setUpClass(cls) -> None: cls.logger = logger() xx = session() cls.rs = GetParams() cls.rs.sessions = xx print(cls.rs.sessions)
def click_my_initiate_link(self): """ 点击我拟办的链接 :return: """ js = '$("ul.navleft >li:eq(3) > a").click()' for i in range(10): try: api.execute_script(js) if api.get_current_url().find( '/app/Workflow/MyInstance') != -1: logger().info('页面跳转成功') break except: pass time.sleep(2)
def add_image(driver: WebDriver): """截图到报告 :param driver: :return: """ if "NATIVE_APP" in driver.context: AddImage(driver.get_screenshot_as_base64()) else: context = driver.context driver.switch_to.context(None) try: AddImage(driver.get_screenshot_as_base64()) except WebDriverException as e: logger().info(f"截图失败:\n{e}") finally: driver.switch_to.context(context)
def click_add_workflow_link(self): """ 点击新建流程链接 :return: """ js = '$("ul.navleft >li:eq(5) > a").click()' for i in range(10): try: api.execute_script(js) if api.get_current_url().find('/app/Workflow/MyWorkflow' ) != -1 and api.el_is_display( self.test_workflow_link): logger().info('页面跳转成功') break except: pass time.sleep(2)
def login_case(driver, url, userName, password): driver.get(url) logger().info('点击首页的登录按钮') driver.find_element_by_link_text(home_page.home_login_link_text).click() logger().info('输入登录用户名 %s' % userName) driver.find_element_by_id(login_page.login_userName_id).send_keys(userName) logger().info('输入登录密码 %s' % password) driver.find_element_by_id(login_page.login_password_id).send_keys(password) logger().info('点击登录按钮') driver.find_element_by_css_selector( login_page.login_submit_css_selector).click() return driver
class ReleasdTopic(unittest2.TestCase): '发帖' url='http://118.31.19.120:3000/' browse='谷歌' logger().info('打开浏览器获取driver') driver=getDriver.driver(browse) imgName=None @classmethod def tearDownClass(self): logger().info('退出浏览器') getDriver.exit_browse(self.driver) def setUp(self): self.imgName=None def tearDown(self): logger().info("截屏") getDriver.screen_shot(self.driver,self.imgName) @data(*utilsApi.get_data_csv(utilsApi.superiorPath()+'/test_data/topicData.csv')) @unpack def test_topic(self,values1,values2,values3): ''' 发帖 :param values1: 板块 plate :param values2: 标题 title :param values3: 内容 content :return: null ''' self.imgName=utilsApi.get_function_name() login.login_case(self.driver,self.url,'testuser3','123456') logger().info('点击首页的发布话题') self.driver.find_element_by_xpath(home_page.home_topic_xpath).click() self.driver.find_element_by_xpath(release_topic_page.topic_select_xpath).click() if '请选择' == values1: self.driver.find_element_by_xpath(release_topic_page.topic_select_option1_xpath).click() elif '分享' == values1: logger().info("选择板块分享") self.driver.find_element_by_xpath(release_topic_page.topic_select_option2_xpath).click() elif '问答' == values1: logger().info("选择板块问答") self.driver.find_element_by_xpath(release_topic_page.topic_select_option3_xpath).click() elif '招聘' == values1: logger().info("选择板块招聘") self.driver.find_element_by_xpath(release_topic_page.topic_select_option4_xpath).click() logger().info("填写标题 %s" % values2) self.driver.find_element_by_xpath(release_topic_page.topic_title_xpath).send_keys(values2) logger().info('输入内容 %s' % values2) el = self.driver.find_element_by_xpath(release_topic_page.topic_content_xpath) ActionChains(self.driver).click(el).send_keys(values3).perform() logger().info('点击发布按钮') self.driver.find_element_by_xpath(release_topic_page.topic_submit_xpath).click() getDriver.assert_case(self.driver, release_topic_page.topic_me_release_details_content_xpath, values3) getDriver.assert_case(self.driver, release_topic_page.topic_me_release_details_title_xpath, values2)
def setUp(self): # command_executor = "http://127.0.0.1:4723/wd/hub" logger().info(f"appium服务器:{command_executor}") # desired_capabilities = { # "platformName": "Android", # "platformVersion": "8", # "deviceName": "android", # "newCommandTimeout": "60000", # "noReset": False, # "unicodeKeyboard": True, # "resetKeyboard": True, # "appPackage": "io.github.liushilive.at", # "appActivity": ".MainActivity" # } logger().info(f"appium 参数:{desired_capabilities}") self.driver = webdriver.Remote( command_executor=command_executor, desired_capabilities=desired_capabilities) self.driver.implicitly_wait(5) AddImage(self.driver.get_screenshot_as_base64())
def testMinus(self): '''减法运算''' logger().info("点击7") self.d(resourceId="com.ibox.calculators:id/digit7").click() logger().info("点击-") self.d(resourceId="com.ibox.calculators:id/minus").click() logger().info("点击9") self.d(resourceId="com.ibox.calculators:id/digit9").click() logger().info("点击=") equal = self.d(resourceId="com.ibox.calculators:id/equal") equal.click() print(equal.get_text()) print(self.d.info) logger().info("断言") # ans = self.d(resourceId="com.ibox.calculators:id/cv", className="android.widget.TextView", instance="2") ans = self.d(resourceId="com.ibox.calculators:id/cv").child_selector\ (className="android.widget.TextView", instance=1) ans_text = ans.get_text() self.assertEqual(ans_text, "16")
def test_scanWifi(self): '''To perform scan Wi-Fi function''' logger().info("Launch app") self.driver.find_element_by_id('com.avast.android.mobilesecurity:id/eula_accept').click() WebDriverWait(self.driver, self.page_load_time).until(EC.element_to_be_clickable((By.ID, 'com.avast.android.mobilesecurity:id/btn_interstitial_remove_ads_continue'))) self.driver.find_element_by_id('com.avast.android.mobilesecurity:id/btn_interstitial_remove_ads_continue').click() logger().info("Entered main page") self.driver.find_element_by_id('com.avast.android.mobilesecurity:id/matrix_card_3').click() logger().info("Started to scan Wi-Fi") wait_time = 0 while True: try: self.driver.find_element_by_id('com.avast.android.mobilesecurity:id/ui_feed_header_title') logger().info("Completed Wi-Fi scanning") self.driver.save_screenshot(self.screenshotDir + 'test_scanWifi %s.png' % self.timestr) except: wait_time += 1 if wait_time == self.run_func_time: logger().info("Wi-Fi scanning function failed") self.driver.save_screenshot(self.screenshotDir + 'test_scanWifi %s.png' % self.timestr) raise Exception('Scan Wi-Fi function failed') else: break
def oa_api_login(cookie): headers = {'Content-Type': 'application/json', 'Cookie': cookie} data = { 'userCode': user_code, 'password': user_password, 'rendom': int(time.time()) } res_raw = requests.post(login_api, json=data, headers=headers, verify=False) res = res_raw.json() if res['Success'] and res['User']['Code'] == user_code: # res_headers = res_raw.headers # if 'Set-Cookie' in res_headers: # new_cookie = res_headers['Set-Cookie'].split('; ')[0] # print(res_headers) logger().info('接口登录成功') else: logger().info('接口登录失败,返回信息:\n%s' % res) error_msg = '接口URL:%s;\n 接口请求参数:%s;\n 接口请求头信息:%s;\n 接口返回数据:%s' % ( login_api, data, headers, res_raw.text) raise LoginError('接口登录失败,返回信息:%s' % error_msg)
def test_002_get_goods(self): """获取商品""" url = 'http://127.0.0.1:8000/goods/?format=json' data = None res = self.s.get(url=url, data=data) expect = 'detail' logger().info('url:' + 'http://127.0.0.1:8000/goods/?format=json') logger().info('data:' + "None") result = res.text self.assertIn(expect, result) logger().info('result:' + result)
def test_001_login(self): """登录认证接口""" url = 'http://127.0.0.1:8000/login/?format=json' data = 获取ShopApi中login参数失败 res = self.s.post(url=url, data=data) expect = 'token' logger().info('url:' + 'http://127.0.0.1:8000/login/?format=json') logger().info('data:' + "获取ShopApi中login参数失败") result = res.text self.assertIn(expect, result) logger().info('result:' + result)
def page_el(self, str_el): logger().info("元素名称" + str_el) eltype = str_el.split('_')[2] if eltype == 'xpath': str_el = "(By.XPATH,'" + self.el.get("pageel")[str_el] + "')" logger().info("查找元素" + str_el) el = self.find_element(*eval(str_el)) if eltype == 'ID': str_el = "(By.ID,'" + self.el.get("pageel")[str_el] + "')" logger().info("查找元素" + str_el) el = self.find_element(*eval(str_el)) return el
def get_browser(self): """获取浏览器 :return: 浏览器对象 """ self.driver = webdriver.Chrome() # self.driver = webdriver.Remote( # # 表示远程服务器的URL(HUB的URL),默认是:http://127.0.0.1:4444/wd/hub # command_executor='http://127.0.0.1:4444/wd/hub', # # 一种请求能力的字典,启动浏览器会话所需 # desired_capabilities=DesiredCapabilities.CHROME # ) logger().info("打开浏览器:{}".format(self.driver.name)) self.driver.implicitly_wait(10) logger().info("设置隐式等待 10 S") self.driver.maximize_window() logger().info("浏览器最大化") return self.driver
def test_login_1(self): username = "******" password = "******" driver = self.driver open_dsc(driver) login(driver, username, password) time.sleep(2) logger().info("断言 进入首页") self.assertEqual("首页", driver.title) logger().info("断言 个人中心") driver.find_element_by_xpath("//*[text()='我']/../a").click() add_image(driver) self.assertEqual("个人中心", driver.title) logger().info("断言 用户名") driver.find_element_by_xpath('//*[@class="header-admin"]/h4').click() add_image(driver) text = driver.find_element_by_xpath("//*[text()='用户名']/..").text self.assertIn(username, text) pass
def test_login_2(self): username = "" password = "******" driver = self.driver logger().info("侧滑进入菜单页") w = driver.get_window_size().get("width") h = driver.get_window_size().get("height") # 侧滑 driver.swipe(1, h / 2, w / 2, h / 2) add_image(driver) logger().info("点击大商创") driver.find_element_by_xpath("//*[@text='大商创']").click() # 获取导航标题的文本 text = driver.find_element_by_xpath( "//*[@resource-id='io.github.liushilive.at:id/toolbar']/android.widget.TextView" ).text logger().info(f"获取 导航标题的文本:{text}") if "设置" in text: logger().info("配置IP") driver.find_element_by_id("edit_ip").clear() driver.find_element_by_id("edit_ip").send_keys(dsc_app_index) add_image(driver) logger().info("点击保存按钮") driver.find_element_by_id("save").click() # 侧滑 driver.swipe(1, h / 2, w / 2, h / 2) add_image(driver) logger().info("点击大商创") driver.find_element_by_xpath("//*[@text='大商创']").click() add_image(driver) logger().info("进入webview") driver.switch_to.context("WEBVIEW_io.github.liushilive.at") time.sleep(2) logger().info("点击 我") driver.find_element_by_xpath("//*[text()='我']/../a").click() add_image(driver) logger().info(f"输入账号:{username}") driver.find_element_by_name("username").send_keys(username) logger().info("输入密码") driver.find_element_by_name("password").send_keys(password) add_image(driver) logger().info("点击立即登录") driver.find_element_by_xpath('//*[@type="submit"]').click() logger().info("断言 请输入用户名") text = driver.find_element_by_class_name("layermcont").text self.assertEqual("请输入用户名", text) add_image(driver) pass
def tearDown(self): logger().info("关闭APP并退出") self.driver.quit()
def test_appVersionNotEmpty(self): ''' Check version is not empty''' logger().info("Launch app") self.driver.find_element_by_id('com.avast.android.mobilesecurity:id/eula_accept').click() WebDriverWait(self.driver, self.page_load_time).until(EC.element_to_be_clickable((By.ID, 'com.avast.android.mobilesecurity:id/btn_interstitial_remove_ads_continue'))) self.driver.find_element_by_id('com.avast.android.mobilesecurity:id/btn_interstitial_remove_ads_continue').click() logger().info("Entered main page") self.driver.find_element_by_accessibility_id('Open menu').click() logger().info("Clicked menu button") WebDriverWait(self.driver, self.page_load_time).until(EC.element_to_be_clickable((By.ID, 'com.avast.android.mobilesecurity:id/drawer_promo_asl'))) x = self.driver.get_window_size()['width'] y = self.driver.get_window_size()['height'] self.driver.swipe(1/4*x, 6/7*y, 1/4*x, 2/7*y, 2000) self.driver.swipe(1/4*x, 6/7*y, 1/4*x, 2/7*y, 2000) logger().info("Scrolled down to the bottom of the list") WebDriverWait(self.driver, self.page_load_time).until(EC.element_to_be_clickable((By.ID, 'com.avast.android.mobilesecurity:id/drawer_settings'))) self.driver.find_element_by_id("com.avast.android.mobilesecurity:id/drawer_settings").click() logger().info("Entered Settings page") self.driver.swipe(1/4*x, 6/7*y, 1/4*x, 2/7*y, 2000) WebDriverWait(self.driver, self.page_load_time).until(EC.element_to_be_clickable((By.XPATH, '//android.widget.TextView[@text="About"]'))) self.driver.find_element_by_xpath("//android.widget.TextView[@text='About']").click() logger().info("Entered About page") self.driver.save_screenshot(self.screenshotDir + 'test_appVersionNotEmpty %s.png' % self.timestr) self.assertIsNotNone(self.driver.find_element_by_id("com.avast.android.mobilesecurity:id/settings_about_version")) logger().info("Checked app version")