Hello, friends! I began to learn the SWIFT language to test the application using it in XCode and faced with such a task. There is an application. When launching, a page opens on which there is horizontal scrolling. In this scrolling there are interactive themes (10 by default) and their number may vary depending on the config that the user uses. I wanted to automate the process of comparing the names of these topics with a list that is stored on a local disk. Please help me understand how to implement the first part of my task - Search through the text in this scrolling bar and compile a list of this text. Thank.

The beginning of the code is so, but I don’t know how to create the list further ((((.

func testExample() { XCUIDevice.shared().orientation = .faceUp let scrollView = XCUIApplication().children(matching: .window) .element(boundBy: 0) .children(matching: .other) .element.children(matching: .other) .element.children(matching: .other) .element.children(matching: .other) .element.children(matching: .other) .element(boundBy: 0) .children(matching: .other) .element(boundBy: 1) .children(matching: .other) .element(boundBy: 0) .children(matching: .scrollView) .element // var lists = ["Annotate a File","Start from Scratch","Annotate a Webpage","Annotate a Video","Annotate Images","Annotate a Map","Annotate a File","Create a Live Stream","News Studio","Talk Show","Lincoln Memorial","The Not Perfect Hat Club","News Cast","BusinessCast","SportsCast","Here's How","Review It","Loading Theme...","Unable to Load Theme :("] scrollView.swipeRight() /* for text in lists { if text == "Loading Theme..." { repeat { sleep (30) scrollView.swipeRight() scrollView.swipeLeft() } } while text != "Loading Theme..." } scrollView.swipeRight() for text in lists { if text == "Unable to Load Theme :(" { XCUIApplication().buttons[text+" is logged"].tap() XCUIApplication().navigationBars[text].buttons["Settings"].tap() XCUIApplication().tables.buttons["Log Out"].tap() XCUIApplication().buttons["Sign In"].tap() } } */ /* let file = "file.txt" //this is the file. we will write to and read from it let text = "_previewTitle" //just a text if let dir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first { let path = dir.appendingPathComponent(file) //writing do { try text.write(to: path, atomically: false, encoding: String.Encoding.utf8) } catch {/* error handling here */} } func writeToDocumentsFile(fileName:String,value:String) { let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as NSString let path = documentsPath.appendingPathComponent(fileName) do{ try value.write(toFile: path, atomically: true, encoding: String.Encoding.utf8) }catch{ } } */ } } 

An example of the text in the picture: so start a scrollbar with themes

  • What is this strip? Normal UIScrollView or UICollectionView / still that? - VAndrJ
  • Xcode shows the following information: <ThemesSelectionScrollView> <PagingScrollView: baseClass = UIScrollView; > - Yehor Tupchienko
  • I also realized that the names of the topics are: UILabel. How do I list these topics? - Yehor Tupchienko
  • On the English-language site I posted the same question but slightly changed the code, it may be easier to solve this task stackoverflow.com/questions/45675798/… according to this scenario - Yehor Tupchienko

1 answer 1

In tests about 0, but try the direction:

 let scrollView = XCUIApplication().scrollViews["Scroll View"] let elementsQuery = scrollView.otherElements let names = ["Label", "Label1", "Label12", "Label121" /*Label123 in program*/] for element in elementsQuery.staticTexts.allElementsBoundByIndex { // Замените на собственную логику XCTAssert(names.contains(element.label), "Not contains in names array") } 

Parsing:

0 Preparation. Slapped UIScrollView with 4 UILabel in which the text "Label", "Label1", "Label12", "Label123", respectively. I specify in the test:

 let names = ["Label", "Label1", "Label12", "Label121"] 

when checking it should fall. If you replace the last element on Label123, it should pass. This is for clarity.

1 Get our UIScrollView , "Scroll View" - this is accessibilityLabel for me:

 let scrollView = XCUIApplication().scrollViews["Scroll View"] 

2 Take the children:

 let elementsQuery = scrollView.otherElements 

3 We cycle through them and get the text:

 for element in elementsQuery.staticTexts.allElementsBoundByIndex { XCTAssert(names.contains(element.label), "Not contains in names array") } 

Here you can replace what you need. Those. you can go through the loop, put everything into an array and then do something with it:

 var labelNames = [String]() for element in elementsQuery.staticTexts.allElementsBoundByIndex { labelNames.append(element.label) } print(labelNames) 
  • Thank you very much! - Yehor Tupchienko