Thursday, 4 June 2015

swift functional tests -- KIF, get UIVIew by kif



Verify the order of data in a table view by kif.



In the table view controller:\


    tableView.accessibilityLabel = l_PARCEL_TABLEVIEW
    tableView.accessibilityIdentifier = l_PARCEL_TABLEVIEW

In the test class,  the test code is:


  func testOverdueViewCustomersListIsAlphabetisedOrder(){
    var indexPath0 = NSIndexPath(forRow: 0, inSection: 0)
    var isAlphabetisedOrder = true
    var lastCustomerLastName = ""
    var lastCustomerFirstName = ""
    var nameList = ""
    tester().tapViewWithAccessibilityLabel(l_OVERDUE)
    var tableView = tester().waitForViewWithAccessibilityLabel(l_PARCEL_TABLEVIEW) as? UITableView
    tester().waitForCellAtIndexPath(indexPath0,inTableViewWithAccessibilityIdentifier: l_PARCEL_TABLEVIEW)
    var sections = tableView?.numberOfSections() ?? 0
    XCTAssert(sections>0, "Expect customers list table has more than 0 sections, actually it has \(sections) sections.")
    for sectionIndex in 0...sections-1{
      var cells = tableView?.numberOfRowsInSection(sectionIndex) ?? 0
      XCTAssert(cells>0, "Expect #\(sectionIndex) section of customers list table has more than 0 cells, actually it has \(cells) cells.")
      for cellIndex in 0...cells-1{
        var indexPath = NSIndexPath(forRow: cellIndex, inSection: sectionIndex)
        var nameLabel = "\(l_PARCELCELL_NAMELABEL_PREFIX)\(indexPath.section)_\(indexPath.row)"
        var name = (tester().waitForViewWithAccessibilityLabel(nameLabel) as! UILabel).text!;
        nameList = nameList + name + "; "
        var lastName = name.componentsSeparatedByString(CUSTOMERNAME_SPLITER)[0]
        var firstName = name.componentsSeparatedByString(CUSTOMERNAME_SPLITER)[1]
        if(lastCustomerLastName != "" && lastCustomerFirstName != ""){
          isAlphabetisedOrder = isAlphabetisedOrder && lastCustomerLastName == lastName ? lastCustomerFirstName < firstName : lastCustomerLastName < lastName
        }
        lastCustomerLastName = lastName
        lastCustomerFirstName = firstName
      }
    }
    XCTAssert(isAlphabetisedOrder, "Expect customers list is in alphabetised order, actually it is \(nameList) ")
  }



Rubbish KIF 1

some api use "AccessibilityLabel" for example: "waitForViewWithAccessibilityLabel"
some api use "AccessibilityIdentifier" for example :"inTableViewWithAccessibilityIdentifier"

so is better to set both:

    tableView.accessibilityLabel = l_PARCEL_TABLEVIEW

    tableView.accessibilityIdentifier = l_PARCEL_TABLEVIEW

Rubbish KIF 2

Most kif apis can return you a ui component. However, if the component is a your created customised component, you can't cast it to your type.  Because of Classpath issues. For example, you customized a cell and it extends from UITableViewCell, let's say it is YouCell.

However,  what kif returns you is YouCell, but it is from your development scheme, but if you cast the instance to YouCell, it is going to cast the instance to test scheme's YouCell. Then you get nil.

But if kif returns your a swift build-in class, you will have no problems.

So, If you want to get cells in a tableview, the best ways to do this is assign acccessibilitylabel to fields in cell. Set accessibiltiylabel on run-time. for exmpale:



cell.nameText.accessibilityLabel = "\(l_PARCELCELL_NAMELABEL_PREFIX)\(indexPath.section)_\(indexPath.row)"


No comments:

Post a comment