2017-02-07 20 views
0

這個想法很簡單。我有自定義UIButtonUIButton不能在UITests下使用

@IBDesignable class RadioButton: AttributedButton { 

    private let attributedView = AttributedView(frame: CGRect(x: 0, y: 0, width: 16, height: 16)) 

    @IBInspectable var isActive: Bool = false { 

     didSet { 

      attributedView.removeFromSuperview() 

      attributedView.center = CGPoint(x: frame.size.width/2, y: frame.size.height/2) 
      attributedView.layer.borderWidth = 1 
      attributedView.cornerRadius = 8 

      attributedView.backgroundColor = isActive ? UIColor.scooter : UIColor.white 
      attributedView.layer.borderColor = isActive ? UIColor.scooter.cgColor : UIColor.silver.cgColor 
      addSubview(attributedView) 
     } 
    } 
} 

這是它的外觀時isActive = false

enter image description here

isActive = true

enter image description here

一切工作的應用程序運行時如預期。但是我需要在UITests下做同樣的事情。我所做的只是點擊那個按鈕......但它並未點擊。爲什麼?

如何執行水龍頭?

let termsRadioButton = app.buttons["termsRadioButton"] 
termsRadioButton.tap() 

一旦我點擊我檢查,如果isActive = true,如果沒有,那麼我顯示一個警告...警告不應該下uitests因爲我真的輕按該按鈕。這裏有什麼問題?

enter image description here

回答

0

如果按鈕是細胞或其他容器視圖中,你將不得不小區設置到無法訪問您的按鈕訪問。

見我回答這個問題:https://stackoverflow.com/a/42038145/5052894

如果您希望能夠從UI測試檢查按鈕的狀態,我建議上的按鈕,這是UIControl部分設置isSelected,它的UIButton繼承。然後您可以使用XCUIElement.isSelected訪問按鈕的狀態。