自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

鴻蒙自動(dòng)化常見(jiàn)踩坑點(diǎn)及解決方法

系統(tǒng) 自動(dòng)化
在做鴻蒙自動(dòng)化測(cè)試時(shí),經(jīng)常會(huì)碰到一些讓同學(xué)頭疼的問(wèn)題,有時(shí)會(huì)阻礙很久,影響編寫自動(dòng)化腳本進(jìn)度,那么碰到這些阻礙點(diǎn),我們?cè)撊绾蜗率帜兀旅媪_列幾個(gè)常見(jiàn)的坑點(diǎn)機(jī)解決方案。

[[422395]]

想了解更多內(nèi)容,請(qǐng)?jiān)L問(wèn):

51CTO和華為官方合作共建的鴻蒙技術(shù)社區(qū)

https://harmonyos.51cto.com

在做鴻蒙自動(dòng)化測(cè)試時(shí),經(jīng)常會(huì)碰到一些讓同學(xué)頭疼的問(wèn)題,有時(shí)會(huì)阻礙很久,影響編寫自動(dòng)化腳本進(jìn)度,那么碰到這些阻礙點(diǎn),我們?cè)撊绾蜗率帜兀旅媪_列幾個(gè)常見(jiàn)的坑點(diǎn)機(jī)解決方案

踩坑點(diǎn)一、在點(diǎn)擊方法使用的定位元素為非坐標(biāo)控件的情況時(shí),獲取圖片方法無(wú)法截取到toast!

如何獲取toast樣式進(jìn)行自動(dòng)化?

1.步驟:

  • 截取toast樣式保存樣式圖片(點(diǎn)擊非元素坐標(biāo),無(wú)法獲取到toast圖片)
  • 踩坑點(diǎn):使用gif錄制工具,計(jì)算點(diǎn)擊到彈出toast的時(shí)間,使用非元素坐標(biāo)點(diǎn)擊,無(wú)法獲取到toast截圖,正常邏輯思維,改變點(diǎn)擊到彈出toast的時(shí)間,始終無(wú)法獲取到toast圖片
  • 截取整個(gè)屏幕的圖片
  • 將樣式圖片與整體圖片進(jìn)行對(duì)比驗(yàn)證,樣式是否存在且一致

2.獲取圖片方法:

  1. def get_image(png_path, xy=None): 
  2.     ""
  3.     截圖,當(dāng)xy入?yún)⒂行r(shí)則進(jìn)行裁剪 
  4.     :param png_path: 截圖的路徑 
  5.     :param xy: (x1,y1,x2,y2) 
  6.     :return
  7.     ""
  8.     conf.driver.get_screenshot_as_file(png_path) 
  9.     logger.debug("截屏成功"
  10.     if xy is not None: 
  11.         if isinstance(xy, tuple): 
  12.             for i in xy: 
  13.                 if isinstance(i, int): 
  14.                     pass 
  15.                 else
  16.                     logger.error("xy的入?yún)⒈仨毝际钦麛?shù)"
  17.                     raise (Exception, "xy的入?yún)⒈仨毝际钦麛?shù)"
  18.             try: 
  19.                 Image.open(png_path).crop(xy).save(png_path) 
  20.                 logger.debug("圖片[%s]裁剪成功,裁剪坐標(biāo)%s" % (png_path,xy)) 
  21.             except Exception as e: 
  22.                 logger.error("截圖失敗"
  23.                 raise e 
  24.         else
  25.             logger.error("xy的入?yún)⒏袷奖仨毷?nbsp;(x1,y1,x2,y2) "
  26.             raise (Exception, "xy的入?yún)⒏袷奖仨毷?nbsp;(x1,y1,x2,y2) "

3.創(chuàng)建文件名稱:

  1. expect_image = os.path.join(self.expect_images, "expect_Toast.png"
  2. assert_image = os.path.join(self.assert_images, "assert_Toast_001.png"
  3. success_image = os.path.join(self.success_images, "success_Toast_001.png"

4.圖片對(duì)比方法:

  1. def image_assert(img1, img2, image_path=None, threshold=0.95,cvformat=1): 
  2.     ""
  3.     斷言圖片img1是否在img2中,若斷言成功則會(huì)將圖片保存至本地 
  4.     !!! 斷言的圖片必須用appium截圖,可根據(jù)需求進(jìn)行裁剪(該類下的get_image方法截圖即可) 
  5.     !!! 直接對(duì)模擬器手動(dòng)截圖然后與appium的自動(dòng)截圖做對(duì)比是無(wú)法匹配的,因?yàn)榉直媛释耆煌。。?nbsp;
  6.     :param img1: 預(yù)期的圖片 
  7.     :param img2: 用例執(zhí)行時(shí)的截圖 
  8.     :param image_path: 判斷后若斷言成功則會(huì)將對(duì)比后的圖片保存至本地,本地路徑,不入?yún)t不會(huì)生成對(duì)比圖 
  9.     :param threshold: 匹配度,建議大于0.9 
  10.     :param cvformat: 圖片轉(zhuǎn)換格式,入?yún)?-轉(zhuǎn)換為灰度圖片,入?yún)⒎?-轉(zhuǎn)換為RGB格式,對(duì)顏色有嚴(yán)格校驗(yàn)需求的要轉(zhuǎn)成RGB格式 
  11.     :returnTrue or False 
  12.     ""
  13.     if not os.path.exists(img1): 
  14.         raise (Exception,"[%s]圖片不存在!" % img1) 
  15.     if not os.path.exists(img2): 
  16.         raise (Exception, "[%s]圖片不存在!" % img2) 
  17.     scale = 1 
  18.     img = cv2.imread(img2)  # 要找的大圖 
  19.     img = cv2.resize(img, (0, 0), fx=scale, fy=scale) 
  20.     template = cv2.imread(img1)  # 圖中的小圖 
  21.     template = cv2.resize(template, (0, 0), fx=scale, fy=scale) 
  22.     template_size = template.shape[:2] 
  23.     if int(cvformat) == 1: 
  24.         img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 
  25.         template_ = cv2.cvtColor(template, cv2.COLOR_BGR2GRAY) 
  26.     else
  27.         img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) 
  28.         template_ = cv2.cvtColor(template, cv2.COLOR_BGR2RGB) 
  29.     result = cv2.matchTemplate(img_gray, template_, cv2.TM_CCOEFF_NORMED) 
  30.     loc = np.where(result >= threshold) 
  31.     # 使用灰度圖像中的坐標(biāo)對(duì)原始RGB圖像進(jìn)行標(biāo)記 
  32.     point = () 
  33.     for pt in zip(*loc[::-1]): 
  34.         cv2.rectangle(img, pt, (pt[0] + template_size[1], pt[1] + template_size[0]), (0, 0, 255), 2) 
  35.         point = pt 
  36.     if point == (): 
  37.         logger.debug("圖片[%s]在圖片[%s]中沒(méi)有匹配到" % (img1, img2)) 
  38.         return False 
  39.     else
  40.         if image_path is not None: 
  41.             cv2.imwrite(image_path, img, [int(cv2.IMWRITE_PNG_COMPRESSION), 3])  # 將圖片保存到本地 
  42.         logger.debug("圖片[%s]在圖片[%s]中成功匹配到" % (img1, img2)) 
  43.         return True 

5.點(diǎn)擊方法:

  1. def ta_tap(self, selector, wait_presence=0, wait_visibility=0, timeout=20): 
  2.     ""
  3.     模擬手指點(diǎn)擊一個(gè)元素或坐標(biāo) 
  4.     :param selector: 點(diǎn)擊的元素,定位器,要求格式為["元素名稱","定位方法","定位表達(dá)式"],例如["XXX按鈕","XPATH","//a[@text=‘測(cè)試定位’]"],["XXX按鈕","ID","username"
  5.     :param wait_presence: 是否需要等待元素加載,0-不需要,1-需要 
  6.     :param wait_visibility: 是否需要等待元素可見(jiàn),0-不需要,1-需要 
  7.     :param timeout: 等待的時(shí)間,默認(rèn)20秒 
  8.     :return
  9.     ""
  10.     if selector: 
  11.         try: 
  12.             locator_name = selector[0] 
  13.             locator_by = str(selector[1]).upper()  # 定位方式 
  14.             locator_value = str(selector[2])  # locator的值 
  15.         except Exception as e: 
  16.             logger.error("ta_tap方法:selector入?yún)⒏袷奖仨毷莑ist<string>,[元素名稱,定位方法,定位表達(dá)式]"
  17.             raise e 
  18.         if str(wait_presence) == "1" and str(wait_visibility) == "1"
  19.             logger.warning("ta_tap方法:請(qǐng)不要同時(shí)使用等待元素加載和等待元素可見(jiàn)?。。?quot;
  20.         if locator_by == "XY"
  21.             try: 
  22.                 x, y = int(locator_value.split(",")[0]), int(locator_value.split(",")[1]) 
  23.             except Exception as e: 
  24.                 logger.error("XY坐標(biāo)值格式錯(cuò)誤,正確格式:x,y"
  25.                 raise e 
  26.             TouchAction(conf.driver).tap(x=x, y=y).release().perform() 
  27.             logger.debug("ta_tap模擬手指點(diǎn)擊元素:(%s,%s)" % (x, y)) 
  28.         elif locator_by == "XY%"
  29.             phonesize = self.get_phone_size() 
  30.             phonewidth = phonesize["width"
  31.             phoneheight = phonesize["height"
  32.             try: 
  33.                 x, y = float(locator_value.split(",")[0]), float(locator_value.split(",")[1]) 
  34.             except Exception as e: 
  35.                 logger.error("XY坐標(biāo)值格式錯(cuò)誤,正確格式:x,y-x和y均是小數(shù)(不要填寫百分比),例如0.8,0.5"
  36.                 raise e 
  37.             TouchAction(conf.driver).tap(x=int(x * phonewidth), y=int(y * phoneheight)).release().perform() 
  38.             logger.debug("ta_tap模擬手指點(diǎn)擊元素:(%s,%s)" % (x * phonewidth, y * phoneheight)) 
  39.         else
  40.             if str(wait_presence) == "1"
  41.                 self.wait_element_presence(selector, timeout) 
  42.             if str(wait_visibility) == "1"
  43.                 self.wait_element_visibility(selector, timeout) 
  44.             try: 
  45.                 el = self.xlsxfind_element(selector) 
  46.                 TouchAction(conf.driver).tap(el).release().perform() 
  47.                 logger.debug("ta_tap模擬手指點(diǎn)擊%s" % locator_name) 
  48.             except Exception as e: 
  49.                 logger.error("tap方法異常,元素名稱%s" % locator_name) 
  50.                 raise e 
  51.     else
  52.         raise Exception("wait_element_presence方法:selector參數(shù)是必須的"

6.示例:

鴻蒙自動(dòng)化常見(jiàn)踩坑點(diǎn)及解決方法-鴻蒙HarmonyOS技術(shù)社區(qū)

踩坑點(diǎn)二、appium版本在1.5以后就不再支持ByName的定位

appium版本在1.5以后就不再支持ByName的定位,在appium1.6.3/1.6.4/1.6.5版本以后如何支持ByName定位,適用于安卓,同樣適用于鴻蒙。在使用appium1.5之后的版本時(shí),當(dāng)我們直接適用ByName方式去查找控件時(shí),一定見(jiàn)過(guò)這個(gè)錯(cuò)誤:

  1. org.openqa.selenium.InvalidSelectorException: Locator Strategy 'name' is not supported for this session 

發(fā)現(xiàn)曾經(jīng)的定位神器居然ByName居然不再支持了,那么怎么解決這個(gè)問(wèn)題呢?以下提供兩種解決方式:

  • 換其他定位方式,比如用xpath代替
  • 使用ByAByAccessibilityId代替,但實(shí)踐證明這個(gè)方法并沒(méi)有取代ByName

其中第一種是可取的,換其他定位方式,下面給大家一個(gè)不用換定位方式,可以無(wú)縫解決ByName在升級(jí)appium版本定位方法

一招修改源碼解決問(wèn)題根源,修改方法如下:

在本地找到Appium路徑下的driver.js文件

  1. Appium\resources\app\node_modules\appium\node_modules\appium-android-driver\build\lib\driver.js 

只需要修改其中一行即可

鴻蒙自動(dòng)化常見(jiàn)踩坑點(diǎn)及解決方法-鴻蒙HarmonyOS技術(shù)社區(qū)

打開(kāi)driver.js文件

鴻蒙自動(dòng)化常見(jiàn)踩坑點(diǎn)及解決方法-鴻蒙HarmonyOS技術(shù)社區(qū)

在代碼行加上“name”屬性

  1. this.locatorStrategies = ['xpath''id''class name''accessibility id''-android uiautomator','name']; 

 修改完成之后,保存,再次重啟appium服務(wù),就可以繼續(xù)使用ByName定位啦

  1. element = conf.driver.find_element_by_name("name").click() 

如果不想用這種方式,也可以使用通用xpath

  1. element = conf.driver.find_element_by_xpath("//*[@text='name']").click() 

踩坑點(diǎn)三、彈窗點(diǎn)擊確認(rèn)或選擇選項(xiàng)后,出現(xiàn)腳本無(wú)法往下執(zhí)行的問(wèn)題

遇到這種問(wèn)題時(shí),可以通過(guò)坐標(biāo)點(diǎn)擊頁(yè)面彈窗,再點(diǎn)擊空白處來(lái)釋放,就可以繼續(xù)往下執(zhí)行

如desCharts組件在彈窗切換圖表類型后,腳本會(huì)卡住,可以通過(guò)坐標(biāo)點(diǎn)擊一個(gè)不影響功能的彈窗,再點(diǎn)擊空白處讓彈窗消失,腳本就可以繼續(xù)往下執(zhí)行:

  1. def exchange(self, elements, chart): 
  2.     ""
  3.     切換圖表選項(xiàng)(彈窗選擇后腳本會(huì)卡住,需要釋放) 
  4.     ""
  5.     self.ta_tap(elements["圖表切換選項(xiàng)"]) 
  6.     if chart in ["堆疊折線圖""柱狀圖""堆疊柱狀圖"]: 
  7.         self.swipe_xy(452, 680, 450, 150) 
  8.         time.sleep(1) 
  9.     self.ta_tap(elements[chart]) 
  10.     time.sleep(1) 
  11.     self.ta_tap(elements["釋放屏幕坐標(biāo)"]) 
  12.     time.sleep(1) 
  13.     self.ta_tap(elements["釋放屏幕坐標(biāo)"]) 
  14.     time.sleep(1) 

踩坑點(diǎn)四、在滾動(dòng)功能無(wú)法使用時(shí),需要滑動(dòng)列表到特定位置。Appium的swipe此時(shí)不好用,drag_and_drop方法無(wú)法使用坐標(biāo)點(diǎn)。

解決方法:

1.重寫drag_and_drop方法,使它可以使用坐標(biāo)點(diǎn)

  1. def drag_and_drop(self, origin_el: Union["WebElement""dict"], destination_el: Union["WebElement""dict"]): 
  2.     action = TouchAction(self) 
  3.     if isinstance(origin_el, dict): 
  4.         action.long_press(**origin_el) 
  5.     else
  6.         action.long_press(origin_el) 
  7.     if isinstance(destination_el, dict): 
  8.         action.move_to(**destination_el) 
  9.     else
  10.         action.move_to(destination_el) 
  11.     action.release().perform() 
  12.     return self 

 2.示例:

  1. @allure.story("MusicBobber"
  2. @allure.title("MusicBobber_015"
  3. @allure.tag("L1"
  4. @allure.severity("normal")  # blocker:阻塞缺陷 critical:嚴(yán)重缺陷 normal:一般缺陷 minor:次要缺陷 trivial:輕微缺陷 
  5. @allure.description("刪除懸浮掛件"
  6. @pytest.mark.flaky(reruns=1, reruns_delay=5)  # reruns:重試次數(shù) reruns_delay:重試的間隔時(shí)間 
  7. def test_MusicBobber_015(self, allow): 
  8.     logger.info("MusicBobber_015"
  9.  
  10.     with allure.step("按返回鍵"): 
  11.         logger.info("按返回鍵"
  12.         self.keyboard(4) 
  13.         time.sleep(1) 
  14.  
  15.     with allure.step("刪除懸浮掛件"): 
  16.         logger.info("刪除懸浮掛件"
  17.         self.drag_and_drop({x:74, y:176}, {x:540, y:2048}) 
  18.         time.sleep(0.5) 
  19.  
  20.     with allure.step("驗(yàn)證"): 
  21.         logger.info("驗(yàn)證"
  22.         expect_image = os.path.join(self.expect_images, f"expect_MusicBobber_015.png"
  23.         assert_image = os.path.join(self.assert_images, f"assert_MusicBobber_015.png"
  24.         contrast_images = os.path.join(self.contrast_images, f"contrast_MusicBobber_015.png"
  25.         self.get_image(contrast_images) 
  26.         flag = self.image_assert(expect_image, contrast_images, assert_image) 
  27.         self.image_in_report(flag, assert_image, expect_image) 
  28.         assert flag is False 

結(jié)語(yǔ)

其實(shí),在UI自動(dòng)化實(shí)踐中我們還會(huì)遇到其他不同的阻礙點(diǎn),Appium本身就存在一些bug,鴻蒙應(yīng)用的自動(dòng)化與安卓原理基本一致,我們都是在

摸索中前行,所以,碰到坑點(diǎn)不要慌,在原生不支持的情況下,換一種解決方式,也會(huì)使你豁然開(kāi)朗,希望以上幾個(gè)問(wèn)題點(diǎn)及解決方案能對(duì)你有所幫助

想了解更多內(nèi)容,請(qǐng)?jiān)L問(wèn):

51CTO和華為官方合作共建的鴻蒙技術(shù)社區(qū)

https://harmonyos.51cto.com

 

責(zé)任編輯:jianghua 來(lái)源: 鴻蒙社區(qū)
相關(guān)推薦

2021-09-06 08:00:00

數(shù)字化轉(zhuǎn)型IT自動(dòng)化

2021-04-20 11:03:26

人工智能AI機(jī)器學(xué)習(xí)

2011-04-29 13:22:48

ThinkPad筆記本故障

2011-05-06 17:25:58

硒鼓

2025-04-07 07:20:35

SQL慢查詢性能

2010-08-31 13:49:12

CSS

2009-03-04 10:38:36

Troubleshoo桌面虛擬化Xendesktop

2010-08-31 09:13:00

margin-top

2011-06-16 10:27:55

.NET內(nèi)存泄漏

2018-11-01 15:26:38

開(kāi)源軟件安全

2010-08-10 10:10:34

Flex內(nèi)存泄露

2009-08-24 10:37:11

Silverlight

2022-04-06 10:09:17

云服務(wù)云計(jì)算

2021-03-26 10:06:42

IT自動(dòng)化自動(dòng)化首席信息官

2021-10-28 19:10:02

Go語(yǔ)言編碼

2016-09-23 20:46:53

2020-05-18 08:58:33

Python開(kāi)發(fā)工具

2012-11-19 11:30:40

PowerShell常見(jiàn)問(wèn)題解決方法

2018-05-29 11:44:22

數(shù)據(jù)庫(kù)MySQL死鎖

2022-03-04 11:24:16

工業(yè)4.0工業(yè)物聯(lián)網(wǎng)
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)