[英]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. 背景:我正在使日历应用程序自动化,该应用程序包含一个UICollectionView,其中有许多表示时隙的单元格。
I have a CalendarView class with the Login class as its super class. 我有一个CalendarView类,其中Login类是其超类。 This CalendarView class contains all the methods I use in the calendar so that my tests look something like this
CalendarView类包含我在日历中使用的所有方法,因此我的测试看起来像这样
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. 在我的
findEmptyCalendarCell()
我使用一个for循环来定位日历上可命中的第一个可用单元格。 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. (这可以在
tapLatestAppointment()
方法下完成。
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. 如果我能以某种方式在
findEmptyCalendarCell()
的for循环中重用i
变量来点击用于创建约会的同一单元格,那就太好了。 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
:( 我尝试使
i
成为UInt类型的类变量,但是由于我在每个方法之后都返回一个CalendarView()的新实例,因此该值始终为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. 当然,更好的模型是如果在每次方法调用之后都没有返回新的
CalendarView
,但是如果您要避免重写实现,则可以使i
成为全局变量,在任何类外部声明。 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. 在
for
循环中,该值仅在循环内部可用。 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. 如果不是这种情况下,我们将有数以千计的变量漂浮在每个命名空间是使用了大量的应用
for
S,内存会得到敲定,这将是一个痛苦的代码,很坦率地说。
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. 但是,根据您的代码,您真正想要的不是引用
i
,后者在每次迭代时都for
变化,而是一种识别最后一个分接索引的方法。
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: 在全局范围内,在模块内以及任何方括号之外的任何文件中定义
selectedIndex
,如下所示:
var selectedIndex: Int? = nil
Then under any situation where you want the selected index to go back to nil
, just set it as such: 然后,在希望所选索引返回
nil
任何情况下,只需将其设置为:
selectedIndex = nil
In tapLatestAppointment
, then, you would need to fall back when selectedIndex
is nil
. 然后,在
tapLatestAppointment
,当selectedIndex
为nil
时,您将需要后退。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.