Find out whether function returns true or false - forms

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

Related

Coroutine Exits while loop, although condition is still met

I have a coroutine function written for Unity which is as below:
IEnumerator initialize_reference_att()
{
Quaternion zero = new Quaternion(0, 0, 0, 0);
while (Input.gyro.attitude == zero)
Debug.Log("This line is required otherwise exits while without looping");
yield return null;
reference_attitude = Input.gyro.attitude;
Debug.Log("Reference attitude set to " + reference_attitude);
yield return null;
}
In this way it works as expected however when I write the while loop as below:
while (Input.gyro.attitude == zero)
yield return null;
the execution of while loop ends although Input.gyro.attitude == zero condition is still true. Apparently it has something to do with returning from coroutine immediately after while loop without doing any other thing. Because when I add an debug log line(as in the first code snippet), it works as expected. Does anybody have any idea why it behaves in that way? My issue is solved but I only want to understand why it happens. Thanks everyone.
This is basically the good old missing curly braces story:
while (Input.gyro.attitude == zero)
Debug.Log("This line is required otherwise exits while without looping");
yield return null;
basically equals writing
while (Input.gyro.attitude == zero)
Debug.Log("This line is required otherwise exits while without looping");
yield return null;
=> without the { } for enclosing a scope the while only applies to the first following expression after it
what you want is
while (Input.gyro.attitude == zero)
{
Debug.Log("This line is required otherwise exits while without looping");
yield return null;
}
Because when I add an debug log line(as in the first code snippet), it works as expected.
Let me doubt that. Your first snippet is an endless loop that the way it is written should crash your entire app as soon as the condition is true once.
In contrary actually I would expect
while (Input.gyro.attitude == zero)
yield return null;
which basically equals
while (Input.gyro.attitude == zero)
{
yield return null;
}
or also simply
yield return new WaitWhile(() => Input.gyro.attitude == zero);
would allow to yield once a frame => continue in the next frame and check again. Except something happened we all waited for and Unity suddenly made the API multi-threading ready you would always get the same value for Input.gyro.attitude during the same frame

Why breakpoint breaks at else block in 'if else' condition

I am trying to figure out, why, if I put breakpoints on if and on else line, why my if else {} condition breaks on else if the condition was true in if block?
I am using realm but I do not think, that is an issue.
//Check if Object already exists in database
if !RealmService.shared.ifPortfolioExists(name: portfolio.name){//Breakpoint on this line which is true
//Create portfolio
RealmService.shared.create(portfolio)
//Assign portfolio to transaction
transaction.portfolio = portfolio
//Create transaction
RealmService.shared.create(transaction)
}else{//Breakpoint on this line
//Assign portfolio to transaction
transaction.portfolio = portfolio
//Create transaction
RealmService.shared.create(transaction)
}
Am I working out of my mind or am I just stupid? Can please someone explain me this.
The compiler reads them to see if it should proceed into the following block of code. A breakpoint inside of the if will trigger when true and else when false
Do this to better understand where your breakpoints take effect:
if x != y {
print("if breakpoint")//set breakpoint here
} else {
print("else breakpoint")//set breakpoint here
// you should see that the breakpoint doesn't fire here
}

What approach should be made to successfully use a specific variable?

I have a function were in I need to update multiple dropdown lists.
Since I want to remove manual interventions, I want the test to automatically decide which data to be used.
I have 2 sets of array/variable on my main tests.
arrDefinition1 = ['Section;Sec1', 'Domain;test1', 'Process layer;test1', 'Skill;test1'];
arrDefinition2 = ['Section;Sec2', 'Domain;test2', 'Process layer;test2', 'Skill;test2'];
If the current value of the dropdown list is "Sec1" then arrDefinition2 will be used and vise-versa.
But due to promises, my IF condition was executed first before the IT and the variable that contains the value of getText contains "undefined", which means arrDefinition2 will always be used.
it('should click the Edit icon of the first row of Activity table', function() {
waitForElement(5, elmFirstRow, true);
editFirstRow.click();
waitForElement(5, element(by.id('section')), true);
element(by.id('section')).getText().then(function (tmpText) {
tempObject.strSection = tmpText;
});
});
if (tempObject.strSection == 'Sec1') {
console.log('first');
fillUpForm(0, tempObject.arrDefinition2, 'UpdateMDef');
} else {
console.log('second');
fillUpForm(0, tempObject.arrDefinition1, 'UpdateMDef');
}
Note: The function fillUpForm consists of IT functions.
Any suggestions are very much appreciated.

Can i have 2 conditions in protractor expect function? Also give me expect statement to verify whether the getText() has the expected value?

Here i want to combine the below 2 expectations into one.
expect(button.getText()).toEqual('Process Successful');
expect(button.getText().indexOf('- code 3001')).toBeGreaterThan(0);
Also whether the below statement is correct or not. I am trying to verify in the getText() whether expected value is present in the text.
expect(button.getText().indexOf('- code 3001')).toBeGreaterThan(0);
You can use expect().toContain() to verify text contained in string.
expect(button.getText()).toContain('Process Successful');
expect(button.getText()).toContain('- code 3001');
You can also do it in another way,
var buttonContainsText = button.getText().then(function(text){
return (text.indexOf('Process Successful') > -1) && (text.indexOf('- code 3001') > -1)
})
expect(buttonContainsText).toBeTruthy();

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)