Xamarin.UITest-How to verify element is enabled or disabled - ui-automation

I am new to xamarin uitest, can any one please help me out how to verify the element is enabled or disabled with an example.
Thanks in advance.

Assuming that you are using Xamarin's Repl then you can use the following
app.Query(c => c.Id("ElementID")).FirstOrDefault().Enabled
Repl will then return whether the element's enabled property is either true or false
From this, then you can then assign that line to a variable and assert against it
var elementEnabled = app.Query(c => c.Id("ElementID")).FirstOrDefault().Enabled;
assert.AreEqual(true, elementEnabled);

I didn't really understand your question, but if what you're asking for is how to check if a toggle button is enabled, then you can go that way :
internal void SetToggle(string id, bool setToggled)
{
if (setToggled != IsToggled(id))
app.Tap(e => e.Property("id").Like(id));
}
internal bool IsToggle(string id)
{
return app.Query(e => e.Property("id").Like(id).Invoke("isChecked").Value<bool>())[0];
}

Related

Avoid "value can be null" warning for extension method

I am using an extension on Iterable that adds the following two methods
extension MyIterableExt<T> on Iterable<T> {
bool get isEmptyOrNull => this == null || this.isEmpty;
bool get isNotEmptyOrNull => this != null && this.isNotEmpty;
}
This works as I expected, but when I try to use it in statements where the receiver can be null, I get a warning in IntelliJ. For example, the following code works and prints true:
List<String> x;
print('${x?.reversed.isEmptyOrNull}');
Is there any way I can make the Dart Analyzer understand that the extension checks if this == null and thus not show the warning?
Please note that I do not want to have to add suppression directives to every file or line where I use this extension!
I know I can add // ignore_for_file: can_be_null_after_null_aware or // ignore: can_be_null_after_null_aware to make the warning go away, but what I'm looking for is a way to make the Dart Analyzer understand that the warning for this particular extension method is not needed.
This is in a non null-safe project, if that matters.
you can make Iterable Nullable
extension MyIterableExt<T> on Iterable<T>? {
bool get isEmptyOrNull => this == null || this!.isEmpty;
bool get isNotEmptyOrNull => this != null && this!.isNotEmpty;
}
Make extension base on Iterable? is the way, if you have to find something to disable the warning but not want to change the code then im sorry cause i never do that before.
void main() {
Iterable? a = [];
Iterable b = ['1', '2', '3'];
print('a.isEmptyOrNull: ${a.isEmptyOrNull}');
print('b.isEmptyOrNull: ${b.isEmptyOrNull}');
print('a.isNotEmptyOrNull: ${a.isNotEmptyOrNull}');
print('b.isNotEmptyOrNull: ${b.isNotEmptyOrNull}');
}
extension on Iterable? {
bool get isEmptyOrNull => this == null || this!.isEmpty;
bool get isNotEmptyOrNull => this != null && this!.isNotEmpty;
}
result
a.isEmptyOrNull: true
b.isEmptyOrNull: false
a.isNotEmptyOrNull: false
b.isNotEmptyOrNull: true
Based on the Flutter docs, you should be able to add a comment at the top of your code that will essentially disable null safety checks:
// #dart=2.9
import 'src/my_app.dart';
main() {
//...
}
You should be able to put that comment at the top of any specific file you want this behavior. It doesn't have to be in your "main" file.

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 : "";
}

Codeigniter form validation callback rule issue

I am using Codeigniter 3.x form validation callback method in combination trim and required to validate a field.
The problem is, when I pipe them: trim|required|callback_some_method, the callback method seems to take precedence over trim and required and shows its error message.
Any ideas on this?
EDIT:
This is the rule:
$this->form_validation->set_rules('new_password', 'New Password', 'trim|required|min_length[8]|callback_password_check');
And this is the password_check method:
function password_check($pwd) {
$containsLetterUC = preg_match('/[A-Z]/', $pwd);
$containsLetterLC = preg_match('/[a-z]/', $pwd);
$containsDigit = preg_match('/\d/', $pwd);
$containsSpecial = preg_match('/[^a-zA-Z\d]/', $pwd);
if ( !($containsLetterUC && $containsLetterLC && $containsDigit && $containsSpecial) ) {
$this->form_validation->set_message('password_check', '{field} must contain UPPERCASE and lowercase letters, digits, and special characters.');
return FALSE;
}
return TRUE;
}
The method should return FALSE, but as long as required is before my custom rule and the field is empty, it should stop there with Required field message, NOT the custom method message.
Okay guys, I've managed to solve it by extending the Form_validation library, putting my callback method there and piping as the other rules (without callback_ prefix).
Unfortunately, as described in the code from CI, callbacks validation rules are always verified first, prior to ‘required’ for instance.
There is an official issue opened at CI : https://github.com/bcit-ci/CodeIgniter/issues/5077

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

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)

How to check if the edit mode in jstree is on? Is it possible to check it?

I am using jsTree and I want to get the name/value/text of the node I just created so that I can pass it and store it in the database.
My problem is that after enabling the edit mode, I have no wayof getting the value entered by the user.
My idea is that if I can only determine if the edit mode is on or off, then I can kinda run a function that will now get the user's input. I included here the function for creating the node.
Any other way to solve this problem is much appreciated. Thanks in advance.
function demo_create(){
var ref = $('#data').jstree(true),
p_id = sel = ref.get_selected();
console.log("Parent Id: "+p_id);
if(!sel.length) { return false; }
sel = sel[0];
id = sel = ref.create_node(sel, {"type":"file"});
console.log("Newly Created Id: "+id);
if(sel) {
ref.edit(sel);
}
};
edit will fire the rename_node.jstree once the node name is changed.
You can also use the callback of edit:
ref.edit(sel, null, function (node, status) {
console.log(node.text); // the new node title
})