【问题标题】:Get color of text string in UI testing在 UI 测试中获取文本字符串的颜色
【发布时间】:2016-10-09 08:31:16
【问题描述】:

我试图断言我的文本颜色是它应该的颜色,并且正在努力做到这一点。 This 是我应用程序中表格的图片。我只是想获得“请求”(灰色)或“草稿”(橙色)的颜色。我试图访问作为“请求”或“草稿”的实际字符串的标签,然后获取它的颜色,但找不到任何颜色选项。

var timeSheetStatusColor = app.tables.element.cells.elementBoundByIndex(0).staticTexts.elementBoundByIndex(1).label

任何指导将不胜感激,谢谢!

【问题讨论】:

  • 您想要UIColor 值还是对应UIColor 值的英文单词?
  • 谁会让断言(timeSheetColor == 橙色)为真?我对此完全陌生,不太确定。到目前为止,我的断言看起来像 XCTAssertTrue(timeSheetStatusColor == UIColor.orangeColor())

标签: ios swift user-interface


【解决方案1】:
func checkLabelColor(label: UILabel){
  let color = label.textColor
  if color == requested{
    print("Grey (requested)")
  }else if color == draft{
    print("Orange (draft)")
  }else{
    print("\nFailed to recognize color:\n", color)
  }
}

并称它为checkLabelColor(timeSheetStatusColor)

【讨论】:

  • 如何获取文本的UILabel?我原来问题中的“app.tables.element.cells.elementBoundByIndex(0).staticTexts.elementBoundByIndex(1).label”是字符串类型而不是 UILabel
  • @user3613290 等等,您在 tableview 中使用的是什么数据类型?
  • 没关系,我不使用 UITests,只是假设您从该表达式中获得了 UILabel,从它的尾部判断
  • 哦,我明白了,很抱歉没有指定——我刚开始了解这个。不过谢谢!
猜你喜欢
  • 1970-01-01
  • 2014-09-27
  • 2013-04-19
  • 1970-01-01
  • 2014-02-02
  • 2017-05-28
  • 1970-01-01
  • 2016-12-28
  • 2019-03-22
相关资源
最近更新 更多