How do I perform an action on a field for Swift UI Tests based on the contents/state of the field? - swift

I have a usernameField. On initial state, the field is empty. If I log in successfully, and log back out, it remembers my username for me.
Trying to create a test case for iOS (in swift) that will clear out the field (use the clearText button) if the field has content and then enter a desired string. If it's empty, it needs to skip the clearText button action (since it doesn't exist when the field value is nil) and go straight to entering the username.
It always skips the if statement, even when it's true. Looks for the clearText button, and fails. Works if there's a value in the field, though.
Tried lots of different approaches, but here's my best current working code. Open to any suggestions, as I have no one to really help me learn this stuff. I'm sure I'm just missing something fundamental:
let staffusernameloginfield = app.scrollViews.otherElements.textFields["staffUsernameLoginField"]
staffusernameloginfield.tap()
func checkUsernameFieldContents() {
if staffusernameloginfield == (Content:nil) {
staffusernameloginfield.typeText("owner")
}
else {
elementsQuery.buttons["Clear text"].tap()
staffusernameloginfield.typeText("owner")
}
}
checkUsernameFieldContents()
Have also tried:
if staffusernameloginfield == ("") {
staffusernameloginfield.typeText("owner")
}
else {
elementsQuery.buttons["Clear text"].tap()
staffusernameloginfield.typeText("owner")
}
}
I know that I could hack it by having it always enter a value into the field and then clear it out and enter the desired value, but in this test case, I'm not trying to test for that.

I would compare the value (raw attribute of the element). This type can vary so I always do it as a string:
if staffusernameloginfield.value as! String == "" {
staffusernameloginfield.typeText("owner")
}
else {
elementsQuery.buttons["Clear text"].tap()
staffusernameloginfield.typeText("owner")
}
Now it should enter 'owner' if it sees there is no value in staffusernameloginfield.

to check if the textfield is empty:
app.textFields["signInEmailAddressTextFieldLabel"].value as? String == ""
if the textfield is empty, then that's true
I usually use an assert to check if the value is empty or not, in this example the value is equal to a variable incorrectPassword:
XCTAssertEqual(app.textFields["\(passwordSecureTextFieldLabel)"].value as? String, incorrectPassword)

Related

Powershell - Verify If a indexof is empty

if (($signmail.IndexOf('#')) -ne $null)
{
$signname = $signmail.Remove($signmail.IndexOf('#'))
}
Else{
$signname = $signmail
}
Hi, basicaly, I try to see if the user enter his mail address completely, or just the first part. I try here to ask If the value after a # in the mail address is not empty, to erase it and put it in the new variable. If not, the variable give directly his name to the other variable. But it not work. I always receive the StartIndex can't be below 0 error.
Anyone think of a way to make this code work for that part ?
Thanks
From the String.IndexOf() documentation:
Reports the zero-based index of the first occurrence of a specified Unicode character or string within this instance. The method returns -1 if the character or string is not found in this instance.
So you'll want to test whether the return value is 0 or greater:
if ($signmail.IndexOf('#') -ge 0) {
$signname = $signmail.Remove($signmail.IndexOf('#'))
}
else {
$signname = $signmail
}

empty form text filed

I'm using a FormTextField in a Flutter app
To update a certain column value, the user types in the FormTextField, otherwise leaves the field empty.
I tried this code, but it was adding a null value to the column, deleting the existing value. I'm not happy with this behavior.
String _getProd5Name() {
if ((_prod5Controller.text).isNotEmpty == true) {
_prod5String = _prod5Controller.text;
}
return _prod5String;
}
Is there a way to do it?
I found similar questions, but they are relevant to other languages and their solutions don't solve my case.
String _getProd5Name() {
// Actually you don't have to make it private
// since this is a local variable inside a function
String _prod5String = variableContainingInitialValue;
if (_prod5Controller.text.isNotEmpty) {
_prod5String = _prod5Controller.text;
}
return _prod5String;
}
Here is my advice, since I love wrapping everything on 1 line. You can change the "" part with your result expectation. It's the same with your logic but it's shorter and instead of returning null I make it returning the empty string "". And also (_prod5Controller.text).isNotEmpty == true you can just shorten it to (_prod5Controller.text).isNotEmpty because .isNotEmpty always returning boolean true/false and if-else consuming boolean
String _getProd5Name() {
return ((_prod5Controller.text).isNotEmpty) ? _prod5String = _prod5Controller.text : "";
}

Access ID does not work when testing a textfield with `isSecureTextEntry = true`

So i am trying to access and confirm the presence of a password textfield in my uitests but as soon as assign their isSecureTextEntry = true the tests can no longer find them by the accessID. Does any one know why? and if there is a way around this so that i can find them and use them in the tests? Preferably so that i can still use the access ID to locate them in the tests.
My test is very simple:
func testHasPasswordTextField() {
let passwordTextField = app.textFields[AccesID.passwordTextField]
XCTAssertTrue(passwordTextField.isHittable)
}
I get the following error when running the test:
"No matches found for Find: Elements matching predicate '"Password" IN identifiers' from input {(
TextField, 0x600000193e80, traits: 146029150208, identifier: 'Email', placeholderValue: 'Email'
)}".
the test passes happily if isSecureTextEntry = false(or just not set)
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)
}

Angular 2 - Removing a Validator error

I wrote a function to update Validator rules on an input if a certain option was selected, using this method (the forms are built using FormGroup):
onValueChanged(data : any) {
let appVIP1 = this.vip1TabForm.get('option1');
let appVIP2 = this.vip2TabForm.get('option2');
let appVIP3 = this.vip3TabForm.get('option3');
//Set required validation if data is 'option3'
if(data != 'option3') {
//Due to initialization errors in UI, need to start with the case
//That there are validations, check to remove them
appVIP1.setValidators([]);
appVIP2.setValidators([]);
appVIP3.setValidators([]);
}
else {
appVIP1.setValidators([Validators.required]);
appVIP2.setValidators([Validators.required]);
appVIP3.setValidators([Validators.required]);
}
}
And I bind that function call to a click event on radio buttons (I initially used the guide from this answer, but the onChange function didn't bind correctly).
This works great, and if the user selects option 1 or 2, the validations are empty, and won't be triggered. If they select option 3, the validations are shown and submission is stopped. However, I run into the problem where the user submits, sees the error, and goes back to change to option 1 or 2. While the validator is cleared, my form still reads as invalid. I have multiple input fields I am validating, so I can't just set the form to valid if the validator is removed this way. How would I go about doing this? Can I remove the has-error for one particular field in the formgroup?
If the correct validators are in place, you can manually call AbstractControl#updateValueAndValidity after they select an option:
this.formBuilder.updateValueAndValidity();
(Where, of course, this.formBuilder is your FormBuilder instance.)
You can also call it on FormElements directly.
This is commonly used to trigger validation after a form element's value has been programmatically changed.
Instead of removing and adding validations. It is more simple to enable and disable fields. You need to add the Validators.required for all required fields. And disable the fields which are not required.
onValueChanged(data : any) {
let appVIP1 = this.vip1TabForm.get('option1');
let appVIP2 = this.vip2TabForm.get('option2');
let appVIP3 = this.vip3TabForm.get('option3');
if(data != 'option3') {
appVIP1.disable();
appVIP2.disable();
appVIP3.disable();
}
else {
appVIP1.enable();
appVIP2.enable();
appVIP3.enable();
}
}

Find out whether function returns true or false

I am struggling to make form validation work and it looks like the main problem is placeOrder function (other function which sit inside this functions work just fine). The function behaves in a strange way (gives alert only if the first input field (message) does not confirm to the test conditions, while if I leave the others blank it does not show alert). I was trying to find the reason for that by putting each part of the main if statement (which are divided by AND operators) in this test:
if(validateLenghtData(5, 32, form["message"], form["message_help"])) {
console.log("validation passed");
} else {
console.log("validation failed");
}
This gave me nothing because it only returns false ("validation failed") to console, while it outputs nothing if I enter valid text to the input. Where can be the bug and how can I test each part of big if statement ( like validatedZipCode(form["zipcode"], form["zipcode_help"])) in order to find out which part gives me false.
function placeOrder(form) {
// first check whether all fields are filled with valid data
// form["message"] and other simular arguments are part of form object and are passed from submit form (look it)
if (validateLenghtData(5, 32, form["message"], form["message_help"]) &&
validatedZipCode(form["zipcode"], form["zipcode_help"]) &&
validateNonEmptyField(form["date"], form["date_help"]) &&
validateNonEmptyField(form["name"], form["name_help"]) &&
validateNonEmptyField(form["phone"], form["phone_help"]) &&
validateNonEmptyField(form["email"], form["email_help"])) {
// everything is ok, submit the order to the server
form.submit();
} else {
alert ("You need to feel all fields correctly");
}
if(validateLenghtData(5, 32, form["message"], form["message_help"])) {
console.log("validation passed");
} else {
console.log("validation failed");
}
}