Swift User Creation Not Detecting UIText Length - swift

working on user creation. Pretty simple idea, the email can't be nil or empty, password and confirm password have to be equal to be each other, and password has to be over 6 characters. For whatever reason, this last condition is always passing true. Because of this, users are able to register with a 1 letter or number password. Why would this be happening?
else if(emailTxt.text != "" && passwordTxt.text == confirmPassTxt.text && passwordTxt.text?.characters.count > 6){}
Also, something interesting, I can obviously compare the values because this code to get an password mismatch works perfectly fine.
else if(emailTxt.text != "" && passwordTxt.text != confirmPassTxt.text)
This will return true or false correctly. So the problems come from counting the characters, but I just can't see it right now. Simple mistake?

Interesting way I solved this. Because I never gave an option for Swift to choose if the password WASN'T above 6 characters, it just assumed it was? Not sure why, but adding another condition like this worked.
lse if(emailTxt.text != "" && passwordTxt.text == confirmPassTxt.text && passwordTxt.text?.characters.count < 6 && confirmPassTxt.text?.characters.count < 6){
As you can see it is essentially the same except I changed the > symbols to <. Working perfectly fine now

Related

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)

JES f==None not working

I'm using JES version 5.010. I'm trying to do this assignment for school:
For the first part of the assignment, you are going to write a
function that will select, using pickAFile(), a picture file to be
opened. If cancel is pressed in the pickAFile() dialogue box, then you
must ask the user if it was a mistake. If it was a mistake, open the
pickAFile() dialogue again, and repeat until it is either not a
mistake, or a picture file was selected. If a picture file is
selected, return the 'made' picture, otherwise (i.e. cancel was pushed
and the user indicated that it was NOT a mistake) return an error
message. NOTE: the JES function requestString (see JES
Functions-->Input/Output) should be used when asking the user if the
pressing of 'cancel' was a mistake.
For some reason my if statement, if f==None, isn't working properly and I have no clue why. My code is below
def assign3PartA():
noFile = True
while noFile:
f = pickAFile()
if f==None: #This is what isn't working
noFile=mistake()
noFile = False
print "Invalid option, you must select a picture to continue!"
else: #Working fine
pic = makePicture(f)
show(pic)
break
def mistake():
ask = requestString("Did you press cancel by mistake? Enter yes or no. ")
if ask == "yes" or ask == "Yes":
return True
elif ask == "No" or ask == "no":
return False
else:
print "Try again. Please enter either yes or no."
mistake()
I have also tried if f is None as well and that didn't work.
Any help you can give is appreciated.
I found my error sigh. I get needed to put None in quotation marks.
if j == "None":

iphone NSInteger issues

This seems so basic but for some reason I can't get it to work - the 2 variables are both defined as NSIntegers:
if ([AScore == "100"] && [BScore == "100"]) {
...
}
That doesn't work - nor does it work when I take away the parentheses - nor does it work if I try to implement the 'isEqualToString' command. I'm sure this is a very basic mistake that i am making.
NSIntegers aren't objects, nor are strings integers.
Use if(AScore == 100 && BScore == 100) { instead.

Boolean logic failure

I am having a strange problem with boolean logic. I must be doing something daft, but I can't figure it out.
In the below code firstMeasure.isInvisibleArea is true and measureBuffer1 is nil.
Even though test1 is evaluating to NO for some reason it is still dropping into my if statement.
It works ok if I use the commented out line.
Any idea why this happens?
BOOL firstVisible = firstMeasure.isInVisibleArea;
BOOL notFirstVisible = !(firstMeasure.isInVisibleArea);
BOOL measureBufferNil = measureBuffer1 == nil;
BOOL test1 = measureBuffer1 == nil && !firstMeasure.isInVisibleArea;
BOOL test2 = measureBufferNil && !firstVisible;
if (measureBuffer1 == nil && !firstMeasure.isInVisibleArea)
//if (measureBufferNil && !firstVisible)
{
//do some action
}
Update 1:
I isolated the problem to !firstMeasure.isInVisibleArea as I've entirely taken on the measureBuffer bit.
Inside isInVisible area is a small calculation (it doesn't modify anything though), but the calculation is using self.view.frame. I am going take this out of the equation as well and see what happens. My hunch is that self.view.frame is changing between the two calls to isInVisibleArea.
Update 2:
This is indeed the problem. I have added the answer in more detail below
When in doubt, you should fully parenthesize. Without looking up the precedence rules, what I think what is happening is that = is getting higher precedence than == or &&. So try:
BOOL test1 = ((measureBuffer1 == nil) && !firstMeasure.isInVisibleArea);
While you certainly can parenthesize, you should also know that nil objects evaluate to boolean NO and non-nil objects evaluate to boolean YES. So you could just as easily write this:
BOOL firstVisible = firstMeasure.isInVisibleArea;
BOOL notFirstVisible = !(firstMeasure.isInVisibleArea);
BOOL measureBufferNil = measureBuffer1;
BOOL test1 = !measureBuffer1 && !firstMeasure.isInVisibleArea;
BOOL test2 = measureBufferNil && !firstVisible;
if (measureBuffer1 && !firstMeasure.isInVisibleArea) {
//do some action
}
You would end up with the same results. I agree with GoatRider, though. It's always far better to parenthesize your conditional expressions to clarify what you really want to happen than it is to rely on the language's operator precedence to do it for you.
If test1 is evaluating to NO as you say, then drop test1 into the if statement:
if(test1){
//see if this executes?
}
See what that does.
My hunch was correct, it is related to the view frame changing between calls to firstMeasure.isInVisible area.
This whole routine is called in response to the view moving. I think I need to grab the value of firstMeasure.isInVisibleArea at the start of the method and use that value throughout.
Phew. Boolean logic isn't broken. All is right with the world.
Thanks for all your input