UI Testing
Accessibility Identifier
Section titled “Accessibility Identifier”When Accessibility enabled in Utilities
Section titled “When Accessibility enabled in Utilities”- Select
storyboard. - Expand
the Utilities - Select
Identity Inspector - Select your element on storyboard
- Add new Accessibility Identifier (in example
addButton)
When Accessibility disabled in Utilities
Section titled “When Accessibility disabled in Utilities”- Select
storyboard. - Expand
the Utilities - Select
Identity Inspector - Select your element on storyboard
- Add attribute in
User Defined Runtime Attributes - For
Key Pathtype -accessibilityIdentifier - For
Type- `String - For
Value- new accessibility identifier for your element (in exampleview)
Setting up in UITest file
Section titled “Setting up in UITest file”import XCTest
class StackOverFlowUITests: XCTestCase {
private let app = XCUIApplication()
//Views
private var view: XCUIElement!
//Buttons
private var addButton: XCUIElement!
override func setUp() { super.setUp()
app.launch()
//Views
view = app.otherElements["view"]
//Buttons
addButton = app.buttons["addButton"] }
func testMyApp() {
addButton.tap() view.tap() }}In [ ] add Accessibility Identifier for element.
UIView, UIImageView, UIScrollView
Section titled “UIView, UIImageView, UIScrollView”let imageView = app.images["imageView"]let scrollView = app.scrollViews["scrollView"]let view = app.otherElements["view"]UILabel
Section titled “UILabel”let label = app.staticTexts["label"]UIStackView
Section titled “UIStackView”let stackView = app.otherElements["stackView"]UITableView
Section titled “UITableView”let tableView = app.tables["tableView"]UITableViewCell
Section titled “UITableViewCell”let tableViewCell = tableView.cells["tableViewCell"]UITableViewCell elements
Section titled “UITableViewCell elements”let tableViewCellButton = tableView.cells.element(boundBy: 0).buttons["button"]UICollectionView
Section titled “UICollectionView”let collectionView = app.collectionViews["collectionView"]UIButton, UIBarButtonItem
Section titled “UIButton, UIBarButtonItem”let button = app.buttons["button"]let barButtonItem = app.buttons["barButtonItem"]UITextField
Section titled “UITextField”- normal UITextField
let textField = app.textFields["textField"]- password UITextField
let passwordTextField = app.secureTextFields["passwordTextField"]UITextView
Section titled “UITextView”let textView = app.textViews["textView"]UISwitch
Section titled “UISwitch”let switch = app.switches["switch"]Alerts
Section titled “Alerts”let alert = app.alerts["About yourself"] // Title of presented alertAdding Test Files to Xcode Project
Section titled “Adding Test Files to Xcode Project”When creating the project
Section titled “When creating the project”You should check “Include UI Tests” in the project creation dialog.
After creating the project
Section titled “After creating the project”If you missed checking UI target while creating project, you could always add test target later.
Setps:
- While project open go to
File->New->Target - Find
iOS UI Testing Bundle
Disable animations during UI Testing
Section titled “Disable animations during UI Testing”In a test you can disable animations by adding in setUp:
app.launchEnvironment = ["animations": "0"]Where app is instance of XCUIApplication.
Lunch and Terminate application while executing
Section titled “Lunch and Terminate application while executing”Lunch application for testing
Section titled “Lunch application for testing”override func setUp() { super.setUp()
let app = XCUIApplication()
app.launch()}Terminating application
Section titled “Terminating application”func testStacOverFlowApp() {
app.terminate()}Rotate devices
Section titled “Rotate devices”Device can be rotate by changing orientation in XCUIDevice.shared().orientation:
XCUIDevice.shared().orientation = .landscapeLeftXCUIDevice.shared().orientation = .portraitSyntax
Section titled “Syntax”- XCUIApplication() // Proxy for an application. The information identifying the application is specified in the Xcode target settings as the “Target Application”.
- XCUIElement() // A user interface element in an application.



