使用 by.text 进行排毒子串匹配

问题描述

我正在尝试通过文本选择一个元素,但实际文本发生了变化,因为其中包含当前时间戳。有没有办法在不对 getAttributes() 进行子字符串搜索的情况下选择它?我无法使用 getAttributes,因为我们还将在 android 上进行测试。

我正在使用的选择:

await expect(element(by.text("James has initiated the chatbot: ShallowFaqchatbot"))).toBeVisible();

元素:

  {
      hittable: true,activationPoint: { x: 153.5,y: 14.75 },normalizedActivationPoint: { x: 0.5,y: 0.5 },elementFrame: { y: 9,x: 9,width: 307,height: 29.5 },enabled: true,elementBounds: { y: 0,x: 0,layer: '<CALayer: 0x600002b907c0>',safeAreaInsets: { right: 0,top: 0,left: 0,bottom: 0 },visible: true,elementSafeBounds: { y: 0,label: 'James has initiated the chatbot: ShallowFaqchatbot - 11:08 am',className: 'RCTTextView',frame: { y: 473,x: 34,text: 'James has initiated the chatbot: ShallowFaqchatbot - 11:08 am'
  }

您可以看到它具有我正在查找的文本的属性文本,但是由于“- 11:08 am”,detox 无法抓取它。

解决方法

不幸的是,Detox 不支持正则表达式匹配器:https://github.com/wix/Detox/issues/1441

此处最简单的解决方法是向元素添加 from math import sqrt string = "hello" vowel = "aeiouAEIOU" # https://www.geeksforgeeks.org/sum-of-all-the-prime-numbers-in-a-given-range/ def checkPrime(numberToCheck): if numberToCheck == 1: return False for i in range(2,int(sqrt(numberToCheck)) + 1): if numberToCheck % i == 0: return False return True def primeSum(from_no,to): sum = 0 for i in range(to,(from_no - 1),-1): # Check for prime isPrime = checkPrime(i) if isPrime: # Sum the prime number sum += i return sum for index,element in enumerate(string): if element in vowel: print(primeSum(1,index * 100)) 并对其进行匹配。

testID

您不需要使用 await expect(element(by.id("James has initiated the chatbot: ShallowFaqChatbot"))).toBeVisible(); 来进行此匹配。不是理想的解决方案,但由于 .and 会被隐藏起来,这应该是适合您的情况的有效解决方案。

,

您可以尝试使用 detox "and" matcher

为此,您必须设置元素的 testID。 现在,让我们假设您元素的 testIDsuper_text_field

如果您想检查此元素是否包含特定文本,例如“某事”,您可以使用以下解决方法:

await expect(element(by.id('super_text_field').and(by.text('something')))).toBeVisible()

当然,这种解决方法有其局限性和假设,但我希望它适用于您的情况!