简体   繁体   中英

How can I reuse a variable derived from a for-loop in a method on a different method in the same class?

Background: I'm working on automating a calendar app which contains a UICollectionView with many cells representing time slots.
I have a CalendarView class with the Login class as its super class. This CalendarView class contains all the methods I use in the calendar so that my tests look something like this

func testMakeAppointment() {
    CalendarView()
        .moveToCorrectSchedule()
        .findEmptyCalendarCell()
        .tapNewAppointmentCell()
        .enterRegAppointmentInfo()
        .tapLatestAppointment()

On my findEmptyCalendarCell() I use a for-loop to locate the first available cell that's hittable on the calendar. The method looks like this:

func findEmptyCalendarCell() ->CalendarView {
    let partialCellPath = XCUIApplication().collectionViews.childrenMatchingType(.Other)
    let start: UInt = 10
    let totalAmountOfCells = partialCellPath.count
    for i in start...totalAmountOfCells {
        if partialCellPath.elementBoundByIndex(i).otherElements["CalendarCell"].exists == true {
            if partialCellPath.elementBoundByIndex(i).otherElements["CalendarCell"].hittable == true {
                partialCellPath.elementBoundByIndex(i).otherElements["CalendarCell"].tap()
            xctc.pauseForSeconds(1)
            }
        }
    }

    return CalendarView()
}

On the two following methods, I tap the cell I found and enter the appointment information and create the appointment.

Problem: Up to this point everything is good. However, I now need to tap the appointment I just created in order to assert the appointment object now exists on the calendar. (This would be done under the tapLatestAppointment() method.

If I could somehow reuse the i variable from the for-loop in findEmptyCalendarCell() to tap the same cell I used to create the appointment, that would be great. But unfortunately I can't seem to find a way to save this variable for later use.

Could anyone please help me find a possible solution to this? I tried making i a class variable of type UInt?, but since I return a new instance of CalendarView() after each method, this value is always nil :(

Thank you in advance

Certainly a far better model would be if a new CalendarView were not returned after each method call, but if you want to avoid rewriting your implementation you could just make i a global variable, declared outside any class. Alternatively you could create a class just for this purpose that would have a static property.

In a for loop, the value will only be available inside the loop. If this were not the case we would have thousands of variables floating around at every namespace for apps that used a lot of for s, memory would get hammered, and it would be a pain to code, quite frankly.

But based on your code what you really want is not a way to refer to i , which is changing on each for iteration, but a way to identify the last tapped index.

So how about something like:

func findEmptyCalendarCell() ->CalendarView {
    let partialCellPath = XCUIApplication().collectionViews.childrenMatchingType(.Other)
    let start: UInt = 10
    let totalAmountOfCells = partialCellPath.count
    for i in start...totalAmountOfCells {
        if partialCellPath.elementBoundByIndex(i).otherElements["CalendarCell"].exists == true {
            if partialCellPath.elementBoundByIndex(i).otherElements["CalendarCell"].hittable == true {
                partialCellPath.elementBoundByIndex(i).otherElements["CalendarCell"].tap()
                selectedIndex = i  // See below
                xctc.pauseForSeconds(1)
            }
        }
    }

    return CalendarView()
}

Where selectedIndex would be defined at a global scope, at any file within your module and outside of any brackets, like so:

var selectedIndex: Int? = nil

Then under any situation where you want the selected index to go back to nil , just set it as such:

selectedIndex = nil

In tapLatestAppointment , then, you would need to fall back when selectedIndex is nil .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM