How to use wait_for_element_visible method in SeleniumBase

Best Python code snippet using SeleniumBase

start.py

Source:start.py Github

copy

Full Screen

...193 element.send_keys(value)194 @staticmethod195 def get_text(element):196 return element.text197 def wait_for_element_visible(self, locator):198 return self.webdriver_wait.until(EC.visibility_of_element_located(locator))199 def wait_for_element_presence(self, locator):200 return self.webdriver_wait.until(EC.presence_of_element_located(locator))201 def is_element_visible(self, locator):202 try:203 return self.driver.find_element(locator[0], locator[1])204 except Exception as err:205 return False206 def find_elements(self, locator):207 return self.driver.find_elements(locator[0], locator[1])208 @staticmethod209 def get_rect(element):210 return element.rect211 def right_or_left(self, element):212 pass213 def tap(self, x, y):214 touch_action = TouchAction(self.driver)215 touch_action.tap(x=int(x), y=int(y)).release().perform()216 def double_tap(self, x, y):217 action1 = TouchAction(self.driver).long_press(x=x, y=y, duration=500).release()218 action2 = TouchAction(self.driver).long_press(x=x, y=y, duration=500).release()219 m_action = MultiAction(self.driver)220 m_action.add(action1, action2)221 m_action.perform()222 def double_tap_ele_to_get_details_message(self, x, y, try_count=60):223 message = None224 touch_action = TouchAction(self.driver)225 logger.info("The details_message coordinate is {} {}".format(x, y))226 current = time.time()227 while time.time() - current < try_count:228 touch_action.tap(x=x, y=y, count=2).release().perform()229 time.sleep(0.5)230 ele = self.is_element_visible(AndroidMobilePageObject.details_message())231 if ele:232 logger.info("Try to get details message")233 message = self.get_text(ele)234 logger.info(message)235 self.click(ele)236 break237 if message:238 logger.info("Success to get the details message")239 else:240 logger.error(241 "Failed to get the details message after 60s, since cannot get the messgae by double click element")242 return message243 def get_details_message_by_copy_past_item(self, x, y, copy_item_path, time_out=60):244 message = None245 touch_action = TouchAction(self.driver)246 current = time.time()247 logger.info("信息的坐标是 x = {}, y = {}".format(x, y))248 while time.time() - current < time_out:249 touch_action.long_press(x=int(x), y=int(y), duration=500).release().perform()250 time.sleep(1)251 current_screenshot = os.path.join(copy_item_path, "current_screenshot_copy.png")252 self.driver.save_screenshot(current_screenshot)253 time.sleep(1)254 loc = Utils.match_image_by_match_template_func(current_screenshot, os.path.join(255 copy_item_path,256 "copy_item.png"257 ))258 if loc:259 self.tap(loc["x"], loc["y"])260 time.sleep(1)261 message = self.driver.get_clipboard_text()262 break263 if message:264 logger.info("成功获取到信息: {}".format(message))265 else:266 logger.error("在60s内获取信息失败")267 return message268 def save_screenshot_as_png(self, file_name):269 self.driver.save_screenshot(filename=file_name)270 logger.info("Capture the screen: {}".format(file_name))271class AndroidMobilePageObject:272 def __init__(self):273 pass274 @staticmethod275 def search_btn_in_home_page():276 return (MobileBy.ID, "com.tencent.mm:id/f8y")277 @staticmethod278 def address_book_in_home_page():279 return (MobileBy.XPATH, "(//*[@resource-id='com.tencent.mm:id/cn_'])[2]")280 @staticmethod281 def gongzong_number_item():282 return (MobileBy.XPATH, "(//*[@resource-id='com.tencent.mm:id/b3b'])[4]")283 @staticmethod284 def search_in_gongzong_page():285 return (MobileBy.ACCESSIBILITY_ID, "搜索")286 @staticmethod287 def search_input_in_gongzong_page():288 return (MobileBy.ID, "com.tencent.mm:id/bhn")289 @staticmethod290 def target_item():291 return (MobileBy.XPATH, "//android.widget.TextView[@text='大众汽车金融中国测试号']")292 @staticmethod293 def title_in_chat():294 return (MobileBy.ID, "com.tencent.mm:id/gas")295 @staticmethod296 def message_btn():297 return (MobileBy.ID, "com.tencent.mm:id/aly")298 @staticmethod299 def message_input():300 return (MobileBy.ID, "com.tencent.mm:id/al_")301 @staticmethod302 def message_send_btn():303 return (MobileBy.ID, "com.tencent.mm:id/anv")304 # 服务按钮 com.tencent.mm:id/g33305 @staticmethod306 def menu_btn():307 return (MobileBy.ID, "com.tencent.mm:id/g33")308 @staticmethod309 def latest_message():310 return (MobileBy.ID, "com.tencent.mm:id/ala")311 @staticmethod312 def details_message():313 return (MobileBy.ID, "com.tencent.mm:id/c9a")314 @staticmethod315 def contact_photo():316 return (MobileBy.ID, "com.tencent.mm:id/aku")317 @staticmethod318 def back_btn():319 return (MobileBy.ID, "com.tencent.mm:id/dn")320class IOSMobilePageObject:321 def __init__(self):322 pass323 @staticmethod324 def address_book_in_home_page():325 return (MobileBy.ACCESSIBILITY_ID, "通讯录")326 @staticmethod327 def gongzong_number_item():328 return (MobileBy.ACCESSIBILITY_ID, "公众号")329 @staticmethod330 def search_in_gongzong_page():331 return (MobileBy.ACCESSIBILITY_ID, "搜索")332 @staticmethod333 def search_input_in_gongzong_page():334 return (MobileBy.ACCESSIBILITY_ID, "搜索")335 @staticmethod336 def target_item():337 return (MobileBy.XPATH, "(//XCUIElementTypeStaticText[@name='大众汽车金融中国测试号'])[3]")338 @staticmethod339 def title_in_chat():340 return (MobileBy.ACCESSIBILITY_ID, "聊天详情")341 @staticmethod342 def message_btn():343 return (MobileBy.ACCESSIBILITY_ID, "切换到文本输入")344 @staticmethod345 def message_input():346 return (MobileBy.XPATH, "//XCUIElementTypeTextView")347 @staticmethod348 def message_send_btn():349 return (MobileBy.ACCESSIBILITY_ID, "Send")350 # 切换到菜单351 @staticmethod352 def menu_btn():353 return (MobileBy.ACCESSIBILITY_ID, "切换到菜单")354 @staticmethod355 def latest_message():356 return (MobileBy.XPATH, "//XCUIElementTypeTable//XCUIElementTypeCell[last()]//XCUIElementTypeOther")357 @staticmethod358 def all_message():359 return (MobileBy.XPATH, "//XCUIElementTypeTable//XCUIElementTypeCell//XCUIElementTypeOther")360 @staticmethod361 def back_btn():362 return (MobileBy.ACCESSIBILITY_ID, "关闭")363class AndroidProcess:364 def __init__(self, webdriver):365 self.driver = webdriver366 self.send_message_time = None367 self.get_reply_time = None368 self.mobile_function = MobileFunction(self.driver)369 def go_into_volkswagen_official_account(self, count_name):370 ele_address_book_btn = self.mobile_function.wait_for_element_visible(371 AndroidMobilePageObject.address_book_in_home_page()372 )373 self.mobile_function.click(ele_address_book_btn)374 ele_gongzong_number_item = self.mobile_function.wait_for_element_presence(375 AndroidMobilePageObject.gongzong_number_item()376 )377 self.mobile_function.click(ele_gongzong_number_item)378 ele_search_in_gongzong_page = self.mobile_function.wait_for_element_visible(379 AndroidMobilePageObject.search_in_gongzong_page()380 )381 self.mobile_function.click(ele_search_in_gongzong_page)382 ele_search_input_in_gongzong_page = self.mobile_function.wait_for_element_visible(383 AndroidMobilePageObject.search_input_in_gongzong_page()384 )385 # "大众汽车金融中国测试号"386 self.mobile_function.send_text(ele_search_input_in_gongzong_page, count_name.decode("utf-8"))387 ele_target_item = self.mobile_function.wait_for_element_visible(388 AndroidMobilePageObject.target_item()389 )390 self.mobile_function.click(ele_target_item)391 # ele_title_in_chat = self.mobile_function.wait_for_element_visible(AndroidMobilePageObject.title_in_chat())392 def send_message_then_calculating_time_taken_to_reply(self, value):393 value = value.decode("utf-8")394 logger.info("发送信息: " + value)395 # self.mobile_function.wait_for_element_visible(AndroidMobilePageObject.title_in_chat())396 time.sleep(1)397 ele_message = self.mobile_function.is_element_visible(AndroidMobilePageObject.message_btn())398 if ele_message:399 self.mobile_function.click(ele_message)400 ele_message_input = self.mobile_function.wait_for_element_visible(AndroidMobilePageObject.message_input())401 self.mobile_function.send_text(ele_message_input, value)402 ele_message_send_btn = self.mobile_function.wait_for_element_visible(AndroidMobilePageObject.message_send_btn())403 # 初始化时间404 self.get_reply_time = None405 self.mobile_function.click(ele_message_send_btn)406 time.sleep(0.1)407 self.send_message_time = time.time()408 while time.time() - self.send_message_time < 30:409 try:410 contact_photos = self.mobile_function.find_elements(AndroidMobilePageObject.contact_photo())411 if len(contact_photos) > 0 and self.mobile_function.get_rect(contact_photos[-1])["x"] < 500:412 self.get_reply_time = time.time()413 break414 except Exception as e:415 pass416 if self.get_reply_time:417 cost_time = str(self.get_reply_time - self.send_message_time)418 logger.info("回复信息反应时间: " + cost_time)419 return cost_time420 else:421 logger.error("在30s内获取回复信息反应时间失败")422 return None423 def deal_with_test_data(self, data):424 """425 :param data:426 :return:427 """428 logger.info("开始处理case: " + data.case_no)429 # 发送的信息并获取回复时间430 result_reply = self.send_message_then_calculating_time_taken_to_reply(data.send_message)431 if result_reply:432 # J 给reply_cost_time_from_script赋值433 data.reply_cost_time_from_script = result_reply434 else:435 data.reply_cost_time_from_script = "在30s内获取回复信息反应时间失败,请参考截图"436 # 隐藏键盘437 ele_menu_btn = self.mobile_function.is_element_visible(AndroidMobilePageObject.menu_btn())438 if ele_menu_btn:439 self.mobile_function.click(ele_menu_btn)440 time.sleep(2)441 # 不管获取消息成功与否,都进行截图442 current_screenshot = PATH(os.path.join("screenshot", "case" + str(data.case_no) + ".png"))443 self.mobile_function.save_screenshot_as_png(current_screenshot)444 # H 给screenshot_from_script赋值445 data.screenshot_from_script = os.path.join("screenshot", "case" + str(data.case_no) + ".png")446 folder_name = data.link_template_screenshot_folder447 # 获取回复的信息448 if result_reply:449 latest_message = self.mobile_function.find_elements(AndroidMobilePageObject.latest_message())450 # 最多取五个消息451 flag_count = len(latest_message) if len(latest_message) < 5 else 5452 message_list = []453 folder_path = PATH(os.path.join("template", folder_name, "copy_item"))454 for i in range(len(latest_message) - 1, -1, -1):455 if flag_count > 0:456 flag_count = flag_count - 1457 rect = self.mobile_function.get_rect(latest_message[i])458 x = rect["x"] + rect["width"] / 3459 y = rect["y"] + rect["height"] / 3460 message = self.mobile_function.get_details_message_by_copy_past_item(x=x, y=y,461 copy_item_path=folder_path,462 time_out=60)463 if message:464 if message == data.send_message:465 break466 message_list.append(message)467 time.sleep(1.5)468 latest_message = self.mobile_function.find_elements(AndroidMobilePageObject.latest_message())469 else:470 break471 message_all = ""472 if len(message_list) > 0:473 message_list.reverse()474 message_all = "\n".join(message_list)475 # F 给reply_from_script赋值476 pattern = re.compile(r'<a.*?href="(.*?)".*?>(.*?)</a>')477 find_content_list = re.findall(pattern, message_all)478 href_link_list = []479 for find_content in find_content_list:480 # print(find_content[0], find_content[1])481 href_link_list.append(unquote(find_content[0]))482 message_all = message_all.replace(find_content[0], "")483 if len(href_link_list) > 0:484 message_all = message_all.replace('<a href="">', "").replace("</a>", "")485 # link_from_script486 data.link_from_script = "\n".join(href_link_list)487 data.reply_from_script = message_all488 # 处理回复消息中的link489 link_template_screenshot_list = data.link_template_screenshot.split("\n")490 link_screenshot_flag = 1491 link_screenshot_list = []492 if len(link_template_screenshot_list) > 0 and len(href_link_list) > 0:493 for link_template_screenshot in link_template_screenshot_list:494 # 获取 link样例图片在原图中的坐标495 loc = Utils.match_image_by_match_template_func(current_screenshot,496 PATH(497 os.path.join("template", folder_name,498 link_template_screenshot)))499 current_link_screenshot = os.path.join("screenshot", "case{}_link{}.png".format(data.case_no,500 link_screenshot_flag))501 # 点击匹配到的link502 if loc:503 try:504 self.mobile_function.tap(loc["x"], loc["y"])505 # 等待页面加载506 time.sleep(7)507 self.mobile_function.save_screenshot_as_png(PATH(current_link_screenshot))508 # 从link页面返回到消息界面509 back_btn = self.mobile_function.wait_for_element_visible(AndroidMobilePageObject.back_btn())510 self.mobile_function.click(back_btn)511 self.mobile_function.wait_for_element_visible(AndroidMobilePageObject.title_in_chat())512 except Exception as e:513 logger.error(e)514 logger.error("No link template matched for: " + current_link_screenshot)515 current_link_screenshot = "No link template matched for: " + current_link_screenshot516 else:517 logger.error("No link template matched for: " + current_link_screenshot)518 current_link_screenshot = "No link template matched for: " + current_link_screenshot519 link_screenshot_flag = link_screenshot_flag + 1520 link_screenshot_list.append(current_link_screenshot)521 if len(link_screenshot_list) > 0:522 # I 给link_screenshot_from_script赋值523 data.link_screenshot_from_script = "\n".join(link_screenshot_list)524 elif len(link_template_screenshot_list) > 0 and len(href_link_list) == 0:525 logger.error("回复消息中没有link")526 # I 给link_screenshot_from_script赋值527 data.link_screenshot_from_script = "回复消息中没有link"528 def _init_test_data(self, data):529 pass530class IOSProcess:531 def __init__(self, webdriver):532 self.driver = webdriver533 self.send_message_time = None534 self.get_reply_time = None535 self.mobile_function = MobileFunction(self.driver)536 def go_into_volkswagen_official_account(self, count_name):537 ele_address_book_btn = self.mobile_function.wait_for_element_visible(538 IOSMobilePageObject.address_book_in_home_page()539 )540 self.mobile_function.click(ele_address_book_btn)541 ele_gongzong_number_item = self.mobile_function.wait_for_element_presence(542 IOSMobilePageObject.gongzong_number_item()543 )544 rect = self.mobile_function.get_rect(ele_gongzong_number_item)545 self.mobile_function.tap(rect["x"] + rect["width"] / 2, rect["y"] + rect["height"] / 2)546 ele_search_in_gongzong_page = self.mobile_function.wait_for_element_visible(547 IOSMobilePageObject.search_in_gongzong_page()548 )549 self.mobile_function.click(ele_search_in_gongzong_page)550 ele_search_input_in_gongzong_page = self.mobile_function.wait_for_element_visible(551 IOSMobilePageObject.search_input_in_gongzong_page()552 )553 # "大众汽车金融中国测试号"554 self.mobile_function.send_text(ele_search_input_in_gongzong_page, count_name.decode("utf-8"))555 ele_target_item = self.mobile_function.wait_for_element_presence(556 IOSMobilePageObject.target_item()557 )558 rect = self.mobile_function.get_rect(ele_target_item)559 self.mobile_function.tap(rect["x"] + rect["width"] / 2, rect["y"] + rect["height"] / 2)560 # ele_title_in_chat = self.mobile_function.wait_for_element_visible(IOSMobilePageObject.title_in_chat())561 def send_message_then_calculating_time_taken_to_reply(self, value, wechat_name):562 value = value.decode("utf-8")563 logger.info("发送信息: " + value)564 # self.mobile_function.wait_for_element_visible(AndroidMobilePageObject.title_in_chat())565 time.sleep(1)566 ele_message = self.mobile_function.is_element_visible(IOSMobilePageObject.message_btn())567 if ele_message:568 self.mobile_function.click(ele_message)569 ele_message_input = self.mobile_function.wait_for_element_visible(IOSMobilePageObject.message_input())570 self.mobile_function.send_text(ele_message_input, value)571 ele_message_send_btn = self.mobile_function.wait_for_element_visible(IOSMobilePageObject.message_send_btn())572 # 初始化时间573 self.get_reply_time = None574 self.mobile_function.click(ele_message_send_btn)575 time.sleep(0.1)576 self.send_message_time = time.time()577 while time.time() - self.send_message_time < 30:578 try:579 latest_message = self.mobile_function.wait_for_element_presence(IOSMobilePageObject.latest_message())580 text = self.mobile_function.get_text(latest_message)581 if text.startswith(wechat_name) or text.startswith("该公众号提供的服务出现故障".decode("utf-8")):582 self.get_reply_time = time.time()583 break584 except Exception as e:585 pass586 if self.get_reply_time:587 cost_time = str(self.get_reply_time - self.send_message_time)588 logger.info("取回复信息反应时间: " + cost_time)589 return cost_time590 else:591 logger.error("在30s内获取回复信息反应时间失败")592 return None593 def deal_with_test_data(self, data, wechat_name):594 """595 :param data:596 :return:597 """598 logger.info("开始处理case: " + data.case_no)599 # 发送的信息并获取回复时间600 result_reply = self.send_message_then_calculating_time_taken_to_reply(data.send_message, wechat_name)601 if result_reply:602 # J 给reply_cost_time_from_script赋值603 data.reply_cost_time_from_script = result_reply604 else:605 data.reply_cost_time_from_script = "在30s内获取回复信息反应时间失败, 请参考截图"606 # 隐藏键盘607 ele_menu_btn = self.mobile_function.is_element_visible(IOSMobilePageObject.menu_btn())608 if ele_menu_btn:609 self.mobile_function.click(ele_menu_btn)610 time.sleep(2)611 # 不管获取消息成功与否,都进行截图612 current_screenshot = PATH(os.path.join("screenshot", "case" + str(data.case_no) + ".png"))613 self.mobile_function.save_screenshot_as_png(current_screenshot)614 # H 给screenshot_from_script赋值615 data.screenshot_from_script = os.path.join("screenshot", "case" + str(data.case_no) + ".png")616 folder_name = data.link_template_screenshot_folder617 # 获取回复的信息618 if result_reply:619 self.mobile_function.wait_for_element_presence(IOSMobilePageObject.latest_message())620 all_message_eles = self.mobile_function.find_elements(IOSMobilePageObject.all_message())621 flag_count = len(all_message_eles) if len(all_message_eles) < 8 else 8622 message_list = []623 for i in range(len(all_message_eles) - 1, -1, -1):624 if flag_count > 0:625 flag_count = flag_count - 1626 message = self.mobile_function.get_text(all_message_eles[i])627 logger.info("Get the message: " + message)628 if message.endswith(data.send_message):629 break630 message = message.replace(wechat_name + "说".decode("utf-8"), "")631 if message not in message_list:632 message_list.append(message)633 message_list.reverse()634 message = "\n".join(message_list)635 # F 给reply_from_script赋值636 pattern = re.compile(r'<a.*?href="(.*?)".*?>(.*?)</a>')637 find_content_list = re.findall(pattern, message)638 href_link_list = []639 for find_content in find_content_list:640 # print(find_content[0], find_content[1])641 href_link_list.append(unquote(find_content[0]))642 message = message.replace(find_content[0], "")643 if len(href_link_list) > 0:644 message = message.replace('<a href="">', "").replace("</a>", "")645 # link_from_script646 data.link_from_script = "\n".join(href_link_list)647 data.reply_from_script = message648 # 处理回复消息中的link649 link_template_screenshot_list = data.link_template_screenshot.split("\n")650 link_screenshot_flag = 1651 link_screenshot_list = []652 if len(link_template_screenshot_list) > 0 and len(href_link_list) > 0:653 for link_template_screenshot in link_template_screenshot_list:654 # 获取 link样例图片在原图中的坐标655 loc = Utils.match_image_by_match_template_func(current_screenshot,656 PATH(os.path.join("template", folder_name,657 link_template_screenshot))658 , self.driver.get_window_rect())659 current_link_screenshot = os.path.join("screenshot", "case{}_link{}.png".format(data.case_no,660 link_screenshot_flag))661 # 点击匹配到的link662 if loc:663 try:664 self.mobile_function.tap(loc["x"], loc["y"])665 # todo: 等待页面加载666 time.sleep(7)667 self.mobile_function.save_screenshot_as_png(PATH(current_link_screenshot))668 # 从link页面返回到消息界面669 back_btn = self.mobile_function.wait_for_element_visible(IOSMobilePageObject.back_btn())670 self.mobile_function.click(back_btn)671 self.mobile_function.wait_for_element_visible(IOSMobilePageObject.title_in_chat())672 except Exception as e:673 logger.error(e)674 logger.error("No link template matched for: " + current_link_screenshot)675 current_link_screenshot = "No link template matched for: " + current_link_screenshot676 else:677 logger.error("No link template matched for: " + current_link_screenshot)678 current_link_screenshot = "No link template matched for: " + current_link_screenshot679 link_screenshot_flag = link_screenshot_flag + 1680 link_screenshot_list.append(current_link_screenshot)681 if len(link_screenshot_list) > 0:682 # I 给link_screenshot_from_script赋值683 data.link_screenshot_from_script = "\n".join(link_screenshot_list)684 elif len(link_template_screenshot_list) > 0 and len(href_link_list) == 0:685 logger.error("回复消息中没有link")686 # I 给link_screenshot_from_script赋值687 data.link_screenshot_from_script = "回复消息中没有link"688class CaseDataModel:689 def __init__(self, case_no, send_message, reply, link_template_screenshot_folder, link_template_screenshot,690 reply_from_script, link_from_script, screenshot_from_script, link_screenshot_from_script,691 reply_cost_time_from_script, result):692 self.case_no = str(case_no)693 self.send_message = send_message694 self.reply = reply695 self.link_template_screenshot_folder = link_template_screenshot_folder696 self.link_template_screenshot = link_template_screenshot697 self.reply_from_script = reply_from_script698 self.link_from_script = link_from_script699 self.screenshot_from_script = screenshot_from_script700 self.link_screenshot_from_script = link_screenshot_from_script701 self.reply_cost_time_from_script = reply_cost_time_from_script702 self.result = result703def clean_data():704 del_list = os.listdir(PATH("screenshot"))705 for f in del_list:706 file_path = os.path.join(PATH("screenshot"), f)707 os.remove(file_path)708def android_steps(test_data_list, wechat_name):709 desired_caps_android_wechat = {710 "platformName": "Android",711 "platformVersion": os.getenv("APPIUM_DEVICE_VERSION", "10"),712 "automationName": os.getenv("APPIUM_AUTOMATION_NAME", "UiAutomator2"),713 "appActivity": os.getenv("APPIUM_APP_ACTIVITY", "com.tencent.mm.ui.LauncherUI"),714 "appPackage": os.getenv("APPIUM_APP_PACKAGE", "com.tencent.mm"),715 "deviceName": os.getenv("APPIUM_DEVICE_NAME", "AKC7N18907000186"),716 "newCommandTimeout": 7200,717 "noReset": True,718 "unicodeKeyboard": True,719 'resetKeyboard': True,720 'chromeOptions': {'androidProcess': 'com.tencent.mm:tools'}721 }722 # 1. 从excel读取数据723 test_data_list_copy = test_data_list[1:]724 logger.info("Android case 统计: " + str(len(test_data_list_copy)))725 driver = webdriver.Remote(os.getenv("APPIUM_URL", 'http://localhost:4723/wd/hub'), desired_caps_android_wechat)726 android_process = AndroidProcess(driver)727 # 2. 进入公众号728 android_process.go_into_volkswagen_official_account(wechat_name)729 # 3. 处理消息730 for test_data in test_data_list_copy:731 try:732 # 需要判断是否回到主界面733 try:734 android_process.mobile_function.wait_for_element_visible(AndroidMobilePageObject.title_in_chat())735 except Exception as e:736 try:737 driver.close_app()738 time.sleep(2)739 driver.launch_app()740 android_process.go_into_volkswagen_official_account(wechat_name)741 except Exception as e:742 logger.warn(e)743 retry_count = 5744 while retry_count > 0:745 logger.info("需要等待10s,然后重启driver")746 time.sleep(10)747 try:748 try:749 driver.quit()750 except Exception:751 pass752 time.sleep(2)753 driver = webdriver.Remote(os.getenv("APPIUM_URL", 'http://localhost:4723/wd/hub'), desired_caps_android_wechat)754 android_process = AndroidProcess(driver)755 android_process.go_into_volkswagen_official_account(wechat_name)756 break757 except Exception as e:758 logger.warn(e)759 retry_count = retry_count - 1760 android_process.deal_with_test_data(test_data)761 except Exception as e:762 logger.warn(e)763 # 数据写进excel764 Utils.write_data_into_excel(PATH("test_case_example.xlsx"), test_data_list_copy)765 logger.info("write_data_into_excel")766def ios_steps(test_data_list, wechat_name):767 desired_caps_ios_wechat = {768 "platformName": "iOS",769 "PlatformVersion": os.getenv('APP_DEVICE_VERSION', "12.2"),770 "deviceName": os.getenv('APP_DEVICE_NAME', "iPhone"),771 "automationName": "XCUITest",772 "udid": os.getenv("APP_UDID", "029d553ea04ba899509dc0630fda19bdac61231a"),773 "bundleId": os.getenv("APP_BUNDLEIDENTIFIER", "com.tencent.xin"),774 "newCommandTimeout": 7200,775 "startIWDP": True,776 # "webDriverAgentUrl": os.getenv("WEBDRIVERAGENT_URL", "http://localhost:8100")777 }778 # 1. 从excel读取数据779 test_data_list_copy = test_data_list[1:]780 logger.info("IOS case 统计: " + str(len(test_data_list_copy)))781 driver = webdriver.Remote(os.getenv("APPIUM_URL", 'http://localhost:4723/wd/hub'), desired_caps_ios_wechat)782 ios_process = IOSProcess(driver)783 # 2. 进入公众号784 retry_count = 5785 while retry_count > 0:786 retry_count = retry_count - 1787 try:788 ios_process.go_into_volkswagen_official_account(wechat_name)789 break790 except Exception as e:791 driver.close_app()792 time.sleep(2)793 driver.launch_app()794 flag_count = 1795 # 3. 处理消息796 for test_data in test_data_list_copy:797 if flag_count % 10 == 0:798 logger.info("对于IOS,每执行十次,重启一次App")799 try:800 driver.close_app()801 time.sleep(2)802 driver.launch_app()803 ios_process.go_into_volkswagen_official_account(wechat_name)804 except Exception as e:805 pass806 flag_count = flag_count + 1807 try:808 # 需要判断是否回到主界面809 try:810 ios_process.mobile_function.wait_for_element_visible(IOSMobilePageObject.title_in_chat())811 except Exception as e:812 try:813 driver.close_app()814 time.sleep(2)815 driver.launch_app()816 ios_process.go_into_volkswagen_official_account(wechat_name)817 except Exception as e:818 logger.warn(e)819 retry_count = 5820 while retry_count > 0:821 logger.info("需要等待10s,然后重启driver")822 time.sleep(10)823 try:824 try:...

Full Screen

Full Screen

auth0.py

Source:auth0.py Github

copy

Full Screen

...73 def autologin_message(self):74 return self.selenium.find_element(*self._autologin_message_locator).text75 @property76 def passwordless_login_confirmation_message(self):77 self.wait_for_element_visible(*self._passwordless_login_confirmation_message)78 return self.selenium.find_element(*self._passwordless_login_confirmation_message).text79 @property80 def ldap_error_message(self):81 return self.selenium.find_element(*self._ldap_error_message).text82 @property83 def is_authorize_github_button_shown(self):84 return self.is_element_enabled(*self._authorize_github_locator)85 def wait_for_spinner(self):86 WebDriverWait(self.selenium, self.timeout).until(lambda s: not self.is_spinner_shown)87 def wait_for_message(self, message):88 WebDriverWait(self.selenium, self.timeout).until(lambda s: self.autologin_message == message)89 def wait_for_error_message_shown(self):90 self.wait_for_element_visible(*self._ldap_error_message)91 def enter_email(self, ldap_email):92 self.selenium.find_element(*self._email_locator).send_keys(ldap_email)93 def click_email_enter(self):94 self.selenium.find_element(*self._enter_email_button_locator).click()95 def click_send_email(self):96 self.wait_for_element_visible(*self._send_email_locator)97 self.selenium.find_element(*self._send_email_locator).click()98 def enter_ldap_password(self, ldap_password):99 self.wait_for_element_visible(*self._password_locator)100 self.selenium.find_element(*self._password_locator).send_keys(ldap_password)101 def click_enter_button(self):102 self.selenium.find_element(*self._enter_button_locator).click()103 def enter_ldap_passcode(self, secret):104 self.selenium.switch_to.frame('duo_iframe')105 self.wait_for_element_visible(*self._login_form_locator)106 self.wait_for_element_visible(*self._enter_passcode_button)107 self.selenium.find_element(*self._enter_passcode_button).click()108 tries = 0109 while tries < 2:110 tries += 1111 passcode = pyotp.TOTP(secret).now()112 self.selenium.find_element(*self._passcode_field_locator).clear()113 self.selenium.find_element(*self._passcode_field_locator).send_keys(passcode)114 self.selenium.find_element(*self._enter_passcode_button).click()115 try:116 self.wait_for_element_visible(*self._duo_response)117 if self.is_element_visible(*self._correct_duo_passcode):118 logger.info('detected duo success')119 break120 if self.is_element_visible(*self._incorrect_duo_passcode):121 logger.info('Detected Duo incorrect OTP. Sleeping for 30 seconds')122 time.sleep(30) # wait until we're guaranteed to have a new TOTP123 except WebDriverException as e:124 if "TypeError: can't access dead object" in str(e):125 logger.info(126 'duo iframe has been closed before we could look at '127 'it indicating successful login')128 break129 self.selenium.switch_to.default_content()130 def click_login_with_github(self):131 self.wait_for_element_visible(*self._login_with_github_button_locator)132 self.selenium.find_element(*self._login_with_github_button_locator).click()133 def enter_github_email(self, email):134 self.selenium.find_element(*self._github_email_field_locator).send_keys(email)135 def enter_github_password(self, password):136 self.selenium.find_element(*self._github_password_field_locator).send_keys(password)137 def click_github_sign_in(self):138 self.wait_for_element_visible(*self._github_sign_in_button_locator)139 self.selenium.find_element(*self._github_sign_in_button_locator).click()140 def enter_github_passcode(self, secret):141 passcode = pyotp.TOTP(secret).now()142 self.selenium.find_element(*self._github_passcode_field_locator).send_keys(passcode)143 self.selenium.find_element(*self._github_enter_passcode_button_locator).click()144 if self.is_authorize_github_button_shown:145 self.selenium.find_element(*self._authorize_github_locator).click()146 def click_login_with_google(self):147 self.wait_for_element_visible(*self._login_with_google_button_locator)148 self.selenium.find_element(*self._login_with_google_button_locator).click()149 def enter_google_email(self, email):150 self.wait_for_element_visible(*self._google_email_field_locator)151 self.selenium.find_element(*self._google_email_field_locator).send_keys(email)152 def click_email_next(self):153 self.selenium.find_element(*self._email_next_button_locator).click()154 def enter_google_password(self, password):155 self.wait_for_element_visible(*self._google_password_field_locator)156 self.selenium.find_element(*self._google_password_field_locator).send_keys(password)157 def click_password_next(self):158 self.selenium.find_element(*self._password_next_button_locator).click()159 def enter_google_passcode(self, secret):160 self.wait_for_element_visible(*self._google_passcode_field_locator)161 passcode = pyotp.TOTP(secret).now()162 self.selenium.find_element(*self._google_passcode_field_locator).send_keys(passcode)163 def click_google_passcode_next(self):164 self.selenium.find_element(*self._google_passcode_next_button_locator).click()165 def click_login_with_firefox_accounts(self):166 self.wait_for_element_visible(*self._login_with_firefox_accounts_locator)167 self.selenium.find_element(*self._login_with_firefox_accounts_locator).click()168 def enter_fxa_email(self, email):169 self.wait_for_element_visible(*self._fxa_email_locator)170 self.selenium.find_element(*self._fxa_email_locator).send_keys(email)171 def enter_fxa_password(self, password):172 self.wait_for_element_visible(*self._password_fxa_locator)173 self.selenium.find_element(*self._fxa_password_locator).send_keys(password)174 def click_firefox_accounts_sign_in(self):175 self.selenium.find_element(*self._fxa_sign_in_button_locator).click()176 def enter_fxa_passcode(self, secret):177 passcode = pyotp.TOTP(secret).now()178 self.selenium.find_element(*self._fxa_passcode_field_locator).send_keys(passcode)179 self.wait_for_element_visible(*self._fxa_passcode_field_locator)180 self.selenium.find_element(*self._fxa_verify_passcode_button_locator).click()181 # def login_with_fxa_staging(self, email, password, secret):182 # self.wait_for_element_visible(*self._login_with_firefox_accounts_locator)183 # self.selenium.find_element(*self._login_with_firefox_accounts_locator).click()184 # self.wait_for_element_visible(*self._email_fxa_locator)185 # self.selenium.find_element(*self._email_fxa_locator).send_keys(email)186 # self.selenium.find_element(*self._fxa_continue_button_locator).click()187 # self.wait_for_element_visible(*self._password_fxa_locator)188 # self.selenium.find_element(*self._password_fxa_locator).send_keys(password)189 # self.selenium.find_element(*self._fxa_continue_button_locator).click()190 #191 # passcode = pyotp.TOTP(secret).now()192 # self.selenium.find_element(*self._fxa_passcode_field_locator).send_keys(passcode)193 # self.wait_for_element_visible(*self._fxa_passcode_field_locator)...

Full Screen

Full Screen

pageObjects.py

Source:pageObjects.py Github

copy

Full Screen

...6 def __init__(self, driver, url):7 self.locator = DemoPageLocators()8 self.driver = driver9 self.root_url = url10 def wait_for_element_visible(self, locator):11 element = WebDriverWait(self.driver, 10).until(12 EC.visibility_of_element_located(locator))13 return element14 def verify_invalid_email(self, email_address):15 self.driver.get(self.root_url)16 self.wait_for_element_visible(self.locator.LOGIN_BUTTON).click()17 time.sleep(1)18 email_box = self.wait_for_element_visible(self.locator.CREATE_ACCOUNT_EMAIL_INPUT)19 email_box.clear()20 email_box.send_keys(email_address)21 self.wait_for_element_visible(self.locator.CREATE_ACCOUNT_SUBMIT_BUTTON).click()22 element = self.wait_for_element_visible(self.locator.CREATE_ACCOUNT_NOTIFICATION_MESSAGE)23 return element.text24 def verify_register_mandatroy_fields(self, email_address):25 email_box = self.wait_for_element_visible(self.locator.CREATE_ACCOUNT_EMAIL_INPUT)26 email_box.clear()27 email_box.send_keys(email_address)28 self.wait_for_element_visible(self.locator.CREATE_ACCOUNT_SUBMIT_BUTTON).click()29 time.sleep(3)30 register_btn = self.wait_for_element_visible(self.locator.REGISTER_BUTTON)31 self.driver.execute_script('arguments[0].scrollIntoView({block: "center", inline: "center"})', register_btn)32 register_btn.click()33 time.sleep(1)34 element = self.wait_for_element_visible(self.locator.MANDATROY_FIELDS_ERROR)35 return element.text36 def verify_invalid_values_entered(self):37 first_name = self.wait_for_element_visible(self.locator.REGISTER_FIRST_NAME)38 first_name.send_keys(12345)39 last_name = self.wait_for_element_visible(self.locator.REGISTER_LAST_NAME)40 last_name.send_keys(12345)41 password = self.wait_for_element_visible(self.locator.REGISTER_PASSWORD)42 password.send_keys(12345)43 city = self.wait_for_element_visible(self.locator.REGISTER_CITY)44 city.send_keys(12345)45 zip_code = self.wait_for_element_visible(self.locator.REGISTER_ZIP_CODE)46 zip_code.send_keys(12345)47 address1 = self.wait_for_element_visible(self.locator.REGISTER_ADDRESS_1)48 address1.send_keys(12345)49 phone = self.wait_for_element_visible(self.locator.REGISTER_PHONE)50 phone.send_keys("abcd")51 mobile = self.wait_for_element_visible(self.locator.REGISTER_MOBILE_PHONE)52 mobile.send_keys("abcd")53 register_btn = self.wait_for_element_visible(self.locator.REGISTER_BUTTON)54 register_btn.click()55 time.sleep(1)56 element = self.wait_for_element_visible(self.locator.MANDATROY_FIELDS_ERROR)...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run SeleniumBase automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful