How can I detect if I'm running an EarlGrey test in my application? - earlgrey

For testing purposes, I want to bypass the login screen of my application. I can do that simply by commenting out the code that pushes the view controller when running tests. However, that's not ideal. I came up with a better way of setting a env var in my scheme and then doing:
if ([NSProcessInfo processInfo].environment[#"TEST"]) {
// Running EarlGrey test.
}
I'd like to know if there's any better way to detect if I am running EarlGrey tests?

As from in the faq, you might want to use a build configuration that allows you to add a check like -
#if EARLGREY_ENV
NSLog(#"This is being run in an EarlGrey test.");
#else
NSLog(#"Not an EarlGrey test.");
#endif
You can also add a runtime check like NSClassFromString("EarlGreyImpl") != nil to see if you're running in an EarlGrey environment.

Related

Is there a way to remove the Push Notification Pop Up confirmation message in Fastlane screenshots?

I'm creating snapshots using Fastlane. As my application uses "Push Notifications", when the app is launched it always displays to the user the pop up that requests the authorization to send this kind of messages. There is a method that is called in the AppDelegate UIApplication.shared.registerForRemoteNotifications(), this method is the one that "shows" the pop up to the user.
I have tried something like:
#if !DEBUG
UIApplication.shared.registerForRemoteNotifications()
#endif
#if TARGET_IPHONE_SIMULATOR
UIApplication.shared.registerForRemoteNotifications()
#endif
I also tried to set a Global variable but it hasn't been possible to find a place where to set this variable, because it never works
I always get the same behavior.
I would expect that the first time I run the test in the simulator, it does not display the message.
Thanks.
I found an easy way to avoid this screenshot.
Before the screenshot is taken, I simply press the button "Allow"
let systemAlerts = XCUIApplication(bundleIdentifier: "com.apple.springboard").alerts
if systemAlerts.buttons["Allow"].exists {
systemAlerts.buttons["Allow"].tap()
}
Simple an easy :)

Swift not printing to console with print() command, other code works fine

I am using XCode Version 9.0 beta (9M136h) to write an application with a watchkit extension. I can get the application to communicate with the watch app and send information back and forth using the WatchConnectivity Framework. I also have the app utilizing the AlamoFire framework to communicate with a server.
All my functions are being called and executing, however, i cant seem to get print() to log anything to the console! I have tried placing various print() commands in different areas of the application, but none of them are producing anything to the console, even though the code around them is working.
I am 100% sure that I am looking at the correct area of XCode, I have gone to View->Debug Area->Activate Console and I have also made sure that All output is selected. I even tried adding the OS_ACTIVITY_MODE with its value as disabled but that didn't seem to do anything, either.
Here is an example of how I am using it in my ViewController.swift file:
override func viewDidLoad()
{
super.viewDidLoad()
print("view did load")
initWCSession()
print("Attorney General Jeff Sessions")
}
This code runs and calls the initWCSession() function but I never see any output of "view did load" or "Attorney General Jeff Sessions" anywhere!
Is this is a bug in XCode? Is there something I am forgetting? (I am new to XCode).
Try view.backgroundColor = .red
I think view contoller is not being called. Just a guess. Otherwise printshould work.
I am not sure why this worked but I went into Edit Scheme and checked the checkboxes for logging malloc. I saved these settings and then went back into Edit Scheme and unchecked the boxes and saved.
After that, I started seeing the print() statements log to the console!
Must be a bug in this version of XCode.
On the recent Xcode 11.3.1 I was getting the log lines from CocoaLumberjack, but nothing from my print calls. Restarting Xcode fixed the problem.

Fails a simple test case using KIF

I try to use KIF in swift project. I run test case on iPhone simulator. Probably I did not set up correctly KIF because use it first time. I used this manual
Test fails in this simple code
func testSelectingOrganizerRole() {
tester().tapView(withAccessibilityLabel: "ORGANIZE")
}
with reason:
A button with Accessibility label "ORGANIZE" exists on initial ViewConroller of storyboard.
Why don't you switch to the UI tests framework available since Xcode 7? A quick intro:
UI testing gives you the ability to find and interact with the UI of your app in order to validate the properties and state of the UI elements.
UI testing includes UI recording, which gives you the ability to generate code that exercises your app's UI the same way you do, and which you can expand upon to implement UI tests. This is a great way to quickly get started writing UI tests.
Using this framework, your simple test would look like this:
let app = XCUIApplication()
app.launch()
app["ORGANIZE"].tap()

Write to console synchronously

I need to debug a time-critical crash I have in my App and since NSLog does not immediately write to the console I am wondering if there is a way to synchronously write to the console from my code.
So for example is printf a better choice here or would I have to deal with the same problems.
You can add a breakpoint before the crashing line(s), though I think Xcode by default already hangs the proccess so that you can still access the debugger console ... If it doesn't, you may want to add some additional diagnostics by going to Product -> Edit Scheme .. (in the upper menu), such as Guard Edges, Zombie Objects, etc.
In addition, you can call NSLog from the main thread:
[self performSelectorOnMainThread:#selector(log:) withObject:#"String .." waitUntilDone:YES];
- (void)log:(NSString *)message {
NSLog(message);
}

Eclipse - Blackberry SDK - Debugging on device: "details unavailable - not supported by VM"

I'm having a strange problem while debugging my Blackberry Application on a real device (BB Bold 9700). When I debug the same application within the BB emulator, the app runs fine, but when I run it on the real device, the app behaves differently (custom painting goes completely wrong). What's even worse is that my Eclipse environment seems to be unable to view live objects correctly while being at a break point (debug time).
I've added a screenshot to illustrate the strange behaviour:
As you can see, the app stops at the breakpoint within the IF statement, but the Variables pane says that the variable "methodName" equals null. Moreover, when I want to look at the variable "methodArguments" which is of type org.json.me.JSONArray, it says "details unavailable - not supported by VM".
Does anyone know what's going on here? My app works great on the emulator, but it's currently useless on the real device.
Thanks in advance!
I think I fixed it:
The problem was that I was laying out Fields on a manager that wasn't yet added to the viewstack.
What did the trick for me was overriding onDisplay() in the manager that contained the Fields that were displayed wrong:
protected void onDisplay()
{
//Make sure superclass is called
super.onDisplay();
/*You have to call "this.setDirty(true)" when you perform layout on a
*manager that isn't added to the viewstack. Then you can use
*"this.isDirty()" to determine whether you need to re-layout the fields
*when the manager becomes visible.*/
if(this.isDirty())
{
//I'm not sure if I need to use "invokeAndWait" and not "invokeLater"
UiApplication.getUiApplication().invokeAndWait(new Runnable()
{
public void run()
{
for (int i = 0; i < getFieldCount(); i++)
{
/*This (custom) function makes sure the Field gets its
*size and position*/
layoutItem(getField(i));
}
}
});
//Make sure you set "dirty" to false, to make sure this only happens once
this.setDirty(false);
}
}
If anyone has a better solution, I'd be glad to hear it (and maybe improve my app).
org.json.me.JSONArray, it says "details unavailable - not supported by VM".
the JSON related stuff is not available on device running 4.5 and 4.6 BB OS. Import it into the code.
Download it from here.
https://github.com/douglascrockford/JSON-java
it is available as Open Source and then use it into your applications.