Access ID does not work when testing a textfield with `isSecureTextEntry = true`
Ok, so a colleague just found out for me that when testing and finding a textfield by its accessabilityidentifier
and isSecureTextEntry
is set to true the textfield is now found in tests as secureTextFields
so my test passes as before but now looks like this:
func testHasPasswordTextField() {
let passwordTextField = app.secureTextFields[AccesID.passwordTextField]
XCTAssertTrue(passwordTextField.isHittable)
}
UITextField with secure entry, always getting cleared before editing
Set,
textField.clearsOnBeginEditing = NO;
Note: This won't work if secureTextEntry = YES. It seems, by default, iOS clears the text of secure entry text fields before editing, no matter clearsOnBeginEditing is YES or NO.
Xcode UITest sometimes does not find property of XCUIElement
I contacted Apple, and they found my bug:
The view of my main view controller had its accessibility
property set to true
. This was wrong; it must be set to false
:
The explanation is found in the docs to isAccessibilityElement
:
The default value for this property is false unless the receiver is a standard UIKit control, in which case the value is true.
Assistive applications can get information only about objects that are represented by accessibility elements. Therefore, if you implement a custom control or view that should be accessible to users with disabilities, set this property to true. The only exception to this practice is a view that merely serves as a container for other items that should be accessible. Such a view should implement the UIAccessibilityContainer protocol and set this property to false.
As soon as I set accessibility of the main view to false, the UI test succeeded.
How to toggle a UITextField secure text entry (hide password) in Swift?
Use this code,
iconClick is bool variable, or you need other condition check it,
var iconClick = true
eye Action method:
@IBAction func iconAction(sender: AnyObject) {
if iconClick {
passwordTF.secureTextEntry = false
} else {
passwordTF.secureTextEntry = true
}
iconClick = !iconClick
}
hope its helpful
Only allow button click if username and password text fields are filled in - IOS (Login page)
You should be handling this in editingChanged
. You want it the changes to be reflected ideally every time the user makes a change in the textField
. So change your UITextField
definition like this:
let emailTF: UITextField = {
let e = UITextField()
let attributedPlaceholder = NSAttributedString(string: "Email", attributes: [NSAttributedStringKey.foregroundColor: UIColor.white])
e.attributedPlaceholder = attributedPlaceholder
e.textColor = .white
e.keyboardType = UIKeyboardType.emailAddress
e.setBottomBorder(backgroundColor: ORANGE_COLOR, borderColor: .white)
e.addTarget(self, action: #selector(editingChanged(_:)), for: .editingChanged) // <- Add this line
return e
}()
let passwordTF: UITextField = {
let p = UITextField()
let attributedPlaceholder = NSAttributedString(string: "Password", attributes: [NSAttributedStringKey.foregroundColor: UIColor.white])
p.attributedPlaceholder = attributedPlaceholder
p.textColor = .white
p.setBottomBorder(backgroundColor: ORANGE_COLOR, borderColor: .white)
p.addTarget(self, action: #selector(editingChanged(_:)), for: .editingChanged) // <- Add this line
p.isSecureTextEntry = true
return p
}()
//Handle button changes in this method
@objc func editingChanged(_ sender: UITextField) {
if !emailTF.text?.isEmpty || !passwordTF.text?.isEmpty {
button.isEnabled = false
//Do highlighting changes to show disabled state
} else {
button.isEnabled = true
//Undo highlighting changes
}
}
Edit: Since both textFields' would be empty in the begnning, i also suggest you disable the button in viewDidLoad
.
func viewDidLoad() {
super.viewDidLoad()
//Do other viewDidLoad stuff
button.isEnabled = false
}
Or you could implement the didBeginEditing
delegate and set emailTF
as first responder as soon as the view is loaded.
func viewDidLoad() {
super.viewDidLoad()
//Do other viewDidLoad stuff
emailTF.becomeFirstResponder()
}
func textFieldDidBeginEditing(_ textField: UITextField) {
if !emailTF.text?.isEmpty || !passwordTF.text?.isEmpty {
button.isEnabled = false
//Do highlighting changes to show disabled state
} else {
button.isEnabled = true
//Undo highlighting changes
}
}
Issue with viewModel and TextField
Your issue is here, that you did not create a StateObject in main View, and every time you pressed the key on keyboard you created a new model which it was empty as default!
import SwiftUI
struct ContentView: View {
@State var showNew = false
@StateObject var viewModel: CreateNewCardViewModel = CreateNewCardViewModel() // <<: Here
var body: some View {
Button(action: { showNew = true }, label: { Text("Create") })
.sheet(isPresented: $showNew, content: {
CreateNewCard(viewModel: viewModel)
})
}
}
struct CreateNewCard: View {
@ObservedObject var viewModel: CreateNewCardViewModel
var body: some View {
TextField("placeholder...", text: $viewModel.definition)
.foregroundColor(.black)
}
}
class CreateNewCardViewModel: ObservableObject {
@Published var definition: String = ""
}
Related Topics
Custom Radix Columns (+Special Characters)
How to Override Setter in Swift
How to Print Http Request to Console
"'Init' Is Deprecated" Warning After Swift4 Convert
Error "[Sharesheet] Connection Invalidated" Error iOS13+ But Not on iOS 11.4
Set Multiple Arrow Directions on UIpopovercontroller in Swift
Can't Get Throws to Work with Function with Completion Handler
How to Update UIviewrepresentable with Observableobject
What Was The Reason for Swift Assignment Evaluation to Void
When How to Start Submitting Apps to The iOS App Store Written Using The Swift Programming Language
Edit Templates Authentication Firebase
Bundle.Main.Path(Forresource... Always Returns Nil When Looking for Xml File
Load Viewcontroller Swift - Black Screen
Swift Cannot Convert The Expression's Type 'Void' to Type 'string!'
Different UIfont Sizes for Different iOS Devices in Swift