GTXiLib icon indicating copy to clipboard operation
GTXiLib copied to clipboard

Not getting accessibility errors in unit test

Open aloksinha18 opened this issue 6 years ago • 11 comments

Hi I have tried using this framework in swift project. I have setup the test case like this

  override static func setUp() {
    super.setUp()
    // Put setup code here. This method is called before the invocation of each test method in the class.
   
    var gTXChecking : [GTXChecking] = []
    guard let checkForAXLabelPresent = GTXChecksCollection.checkForAXLabelPresent() else {
        fatalError("Fatal Error")
    }
    guard let checkForAXLabelNotPunctuated = GTXChecksCollection.checkForAXLabelNotPunctuated() else {
        fatalError("Fatal Error")
    }
    gTXChecking.append(checkForAXLabelPresent)
    let newCheck = GTXCheckBlock.gtxCheck(withName: "label don't have accessibility") { (element, error) -> Bool in
        
        if (element as AnyObject).accessibilityLabel == nil {
            return false
        }
        return true
    }
    gTXChecking.append(checkForAXLabelNotPunctuated)
    
    gTXChecking.append(newCheck)

   GTXiLib.install(on: GTXTestSuite(allTestsIn: self), checks: gTXChecking, elementBlacklists: [])
}

When I am running test cases it is not throwing any error(even if I have not set accessibility labels and text to UILable or UIButton.)

aloksinha18 avatar Aug 13 '19 15:08 aloksinha18

Hey @sishuiOS ! Can you find any solution for this? I have similar case with UI testing. And I do not get any errors for my UI tests.

selin194 avatar Aug 30 '19 10:08 selin194

@sishuiOS is this an XCTest or XCUITest? see this comment for more details

niksawtschuk avatar Aug 30 '19 15:08 niksawtschuk

@niksawtschuk this is Unit test(XCTest) .

aloksinha18 avatar Aug 31 '19 08:08 aloksinha18

class testGtxiLibTests: XCTestCase {
 private var sut: ViewController!
 
 override static func setUp() {
     super.setUp()
     var checkArr: [GTXChecking] = []
     guard let checkFirst = GTXChecksCollection.checkForAXLabelPresent() else {
         fatalError()
     }
     guard let checkSecond = GTXChecksCollection.checkForAXLabelNotPunctuated() else {
         fatalError()
     }
     guard let checkThird = GTXChecksCollection.checkForMinimumTappableArea() else {
         fatalError()
     }
     checkArr.append(checkFirst)
     checkArr.append(checkSecond)
     checkArr.append(checkThird)
     GTXiLib.install(on: GTXTestSuite(allTestsIn: self), checks: checkArr, elementBlacklists: [])
 }
 
 override func setUp() {
     // Put setup code here. This method is called before the invocation of each test method in the class.
     let storyBoard = UIStoryboard(name: "Main", bundle: nil)
     if let sut = storyBoard.instantiateViewController(withIdentifier: "ViewController") as? ViewController {
         self.sut = sut
         _ = self.sut.view
     }
 }
 
 func testController() {
     XCTAssertNotNil(sut.view)
 }
 
 override func tearDown() {
     // Put teardown code here. This method is called after the invocation of each test method in the class.
     sut = nil
 }
}

This is my sample code for Unit test and following is the code for viewController

class ViewController: UIViewController {
    private let button = UIButton(frame: CGRect(x: 100, y: 100, width: 10, height: 10))
    private let label = UILabel(frame: CGRect(x: 100, y: 300, width: 100, height: 50))
    
    @IBOutlet weak private var buttonInterface: UIButton!
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        
        button.backgroundColor = .green
        button.isAccessibilityElement = true
        button.setTitle("Test Button", for: .normal)
        self.view.addSubview(button)
        button.accessibilityLabel = "button"
        
        label.backgroundColor = .orange
        label.isAccessibilityElement = true
        label.text = "Lable text"
        self.view.addSubview(button)
        self.view.addSubview(label)
        label.accessibilityLabel = "label."
        
        buttonInterface.isAccessibilityElement = true
    }
}


You can see clearly in ViewController label has incorrect accessibility identifier(as . is appended in the end) and button has incorrect size (10*10), But still this class pass the test cases. Am i missing something???

aloksinha18 avatar Aug 31 '19 08:08 aloksinha18

@sishuiOS What do you see on the simulator screen when you run this test? Does that view controller actually appear? Try setting the window's view controller to be the one you want to test before each test.

niksawtschuk avatar Sep 03 '19 17:09 niksawtschuk

Yes view controller appears on simulator on running the tests and tests are passed. 😔 If a sample app is provided with the framework with explanation how to implement unit test would be a great help 🙏

aloksinha18 avatar Sep 08 '19 10:09 aloksinha18

Hey @sishuiOS, could you please try : 'override class func setUp()' (which ensures to call setUp() only once!) Thank you!

selin194 avatar Sep 09 '19 06:09 selin194

@sishuiOS do you mind trying to use EarlGrey to perform a simple action on an element in the view? e.g. assert that your button is visible

I have not tried using GTXi in a test that didn't use EarlGrey, so I don't know what is expected here

niksawtschuk avatar Sep 09 '19 15:09 niksawtschuk

@selin194 override class func setUp() is already implemented in my code see here

aloksinha18 avatar Sep 15 '19 10:09 aloksinha18

I am not getting any accessibility errors in the unit test class as above. Any idea ?

deepthinil avatar Feb 15 '21 21:02 deepthinil

super.tearDown() is missing in your test tearDown, GTXiLib expects that the test tearDowns are properly invoked to execute checks. Could you please add it and give it a try.

On Mon, Feb 15, 2021 at 1:55 PM deepthinil [email protected] wrote:

I am not getting any accessibility errors in the unit test class as above. Any idea ?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/google/GTXiLib/issues/12#issuecomment-779464989, or unsubscribe https://github.com/notifications/unsubscribe-auth/AB4R5XA3HXSZMGYXRGM44KTS7GJ4ZANCNFSM4ILL2KUQ .

j-sid avatar Mar 04 '21 02:03 j-sid