why can't I compare a string to a string literal? - objective-c

I have some code that's downloading some strings from a JSON source. One of these is a login success message:
{
login = SUCCESS;
msgSuccess = "User has been logged-in successfully.";
}
So to test this, I did...
if ([[theDictionary objectForKey:#"login"] isEqualToString: #"SUCCESS"]) {
That always fails. But if I do this...
#define kLoginSuccessMessage #"SUCCESS"
if ([[theDictionary objectForKey:#"login"] isEqualToString: kLoginSuccessMessage]) {
it works perfectly.
I've never understood why. Especially when you consider that the objectForKey doesn't seem to have any problem. Can someone explain this so I don't pull out what little is left of my hair?

Related

XCode UITesting check if a text field exists

I can't find any way to check if a text field exists without trying to get it which then fails the tests and shows an error if it can't be found. No matches found for TextField
Current code
XCUIElement *usernameTextField = app.textFields[#"username"];
Reason/detail
I've got a Objective C UITest in XCode which logs into my app in setUp and logs out in tearDown however sometimes my app is already logged in when the test starts (if the simulator has been used for anything else in the meantime). I'd like to be able to check to see if the username textfield exists in my setUp and then if it doesn't I can skip the login or call my logout function and continue as normal.
Not sure about Obj-C but here's how it would work in Swift.
let usernameTextField = app.textFields["username"]
if usernameTextField.exists {
do something
} else {
do something else
}
Here is the code in Swift, which can easily be converted to Obj-C:
// given:
// usernameTextField exists
// The username that is possibly entered there is "username".
// then:
if usernameTextField.value as! String == "username" {
// logged in
} else {
// not logged in
}

selenium webdriver sendkeys intermittent issue

I have a web automation framework set up that works pretty well. I have a constant issue though that when using SendKeys to write to textboxes, quite often a letter gets missed out. So for example, if my dataset is "TestUserName", something like "TestUerName" gets sent example with a missing letter.
This is a big issue for me, as after the web tests concludes successfully I further check if the database was updated properly. So in the above example I would go to the UserName column and expect to find TestUserName, but the test would fail because TestUerName is found instead.
Any ideas please? I am using selenium 2.53.0.
My code below.
public void inputValue (Object [][] valuesFromExcel)
{
for (int rowNow = 0; rowNow < (valuesFromExcel.length); rowNow++)
{
String newValue = valuesFromExcel[rowNow][0].toString();
if (!newValue.equals(""))
{
WebElement currentElement = driver.findElement(By.id(valuesFromExcel[rowNow][1].toString()));
if (currentElement.getTagName().equals("input"))
{
currentElement.sendKeys(newValue);
}
else if (currentElement.getTagName().equals("select"))
{
new Select(currentElement).selectByVisibleText(newValue);
}
}
}
}
Thanks.
Instead of sending as a string, send it as char...
Convert the string to char and send each char one by one to the text box. Yes there will be a performance issue, but it works fine. It will not skip any of the letters

Assert with string argument not working as expected

EDIT: The issue was with the assert as people pointed out below. Thanks for the help!
I have a enum set that i'm trying equate, but for some reason its not working.
Its declared like so:
typedef NS_ENUM(NSUInteger, ExUnitTypes) {
kuNilWorkUnit,
kuDistanceInMeters,
//end
kuUndefined
};
And i'm using it here:
+(NSString*) ExUnitDescription: (ExUnitTypes) exUnit
{
if (exUnit == kuNilWorkUnit)
{
assert("error with units");
}
///.... more stuff
}
Xcode isnt triggering my assert. EDIT: the assert is just for testing. i've used NSLog as well. The conditional isn't evaluating to true even though the value is clearly kuNilWorkUnit.
Does anyone have any suggestions or ideas of what i'm doing wrong?
You want to do this:
+(NSString*) ExUnitDescription: (ExUnitTypes) exUnit
{
assert(exUnit != kuNilWorkUnit);
///.... more stuff
}
This is because, assert only stops execution if the expression you pass to it is false. Since a string literal is always non-zero, it will never stop execution.
Now, since you are using Objective C and it also looks like you want to have a message associated with your assert, NSAssert would be preferable.
+(NSString*) ExUnitDescription: (ExUnitTypes) exUnit
{
NSAssert(exUnit != kuNilWorkUnit, #"error with units");
///.... more stuff
}

indexOfObject does not match correctly

I've been stuck with this problem from a couple of days and I can't get myself out of it.
I've searched all over the net, but I couldn't find anything useful to solve my issue.
this is the scenario.
I've got an array of strings containing a bunch of ids fetched from a coredata sqlite db and
I'd like to know the index of a certain element into this array.
My first solution would have been as easy as using indexOfObject
-(NSInteger) getPageId:(NSString *)symbol_id {
NSInteger refId = [myIds indexOfObject:symbol_id];
// .. stuff ..
return refId;
}
now, I don't know why, but the returning value of the function is always NSNotFound.
If I print out the values via NSLog
NSLog(#"%#\n%#", myIds, symbol_id);
I can clearly see that the value I'm searching for figures out into the elements of the array.
I've even tried a dumbest solution, like probing the match via isEqual function into a for loop:
int idx = 0;
for(NSString *tok in myIds) {
if([tok isEqual:synmbol_id])
{
NSLog(#"yay, a match was encountered!!");
return idx;
}
idx++;
}
but the execution never gets into the NSLog.
I dunno where to knock my head.
hope that some of you already figured this out and could explain this to me.
thx in advance
k
Try printing all the elements on the array like this:
for(NSString *tok in myIds) {
NSLog(#"On the array [%#]", tok);
}
Maybe there is a TAB \t, an ENTER \n or something weird in your NSString preventing isEqual message to run as expected. Usually these characters are hard to find on a regular debugger. That's why I'am suggesting to enclose the string in [].

Get String from TextBox and compare

I'm trying something like my first comparison App in Obj-C and i'm already running into trouble.
Well, there is a textBox with unamebox:(id)unb and a textfield NSTextField* myOut;
Well, here was my first try:
if ([unb stringValue] == #"hello") {
[myOut setStringValue:(NSString *)#"hello dude"];
}
else {
[myOut setStringValue:(NSString *)#"What?"];
}
To my shame, this always setzt the text field to "What?"
When I try the isEqualtoString, it doesn't even do anything:
if ([unb isEqualToString:(NSString*)#"hello"]) {
[myOut setStringValue:(NSString *)#"hello dude"];
}
else {
[myOut setStringValue:(NSString *)#"What?"];
}
So, what shall I do to compare it?
by the way, I already read the links which were suggested above. If I missed anything important, I'm sorry
-isEqualToString: is a method on an NSString, not on an NSTextField. You should be getting an error from sending that message.
You want this:
[[unb stringValue] isEqualToString:#"hello"]