Remote Logging
TestFairy allows developers to log items with a session, without logging to the console output. In some cases, there are workarounds that allow you to wrap the TestFairy remote logging method in a way that will both log to the console and to the session.
Syntax
TestFairy.log("<tag>", "<message>");
Code Example
// Be sure to import TestFairy import com.testfairy.TestFairy; TestFairy.log("Tag", "Hello, TestFairy!");
Syntax
TFLog(@"<message with format>", <arguments>);
[TestFairy log:@"<message>"];
Code Example
// Be sure to import TestFairy #import "TestFairy.h" TFLog(@"Hello, %@", @"TestFairy!"); [TestFairy log:@"Hello, TestFairy!"];
We recommend sending all calls to NSLog
to TestFairy so you can continue to use NSLog
and see all your log statements in your session.
To enable sending logs to TestFairy, you will have to redefine NSLog
using a macro with the following lines tThis macro allows you to continue using NSLog
in your code, while also adding the logs to the matching session in TestFairy).
Changing Your Prefix Header
#import "TestFairy.h"
#define NSLog(s, ...) do { NSLog(s, ##__VA_ARGS__); TFLog(s, ##__VA_ARGS__); } while (0)
- Add the above line to a global header in your project, accessible to every class file.
- Update or create a Prefix Header (.pch) for your project. If you do not have a PCH file in your project, you can follow the steps in the next section.
Creating a New Prefix Header
If your project doesn’t already include a Prefix Header (.pch):
- Create a new file under iOS > Other > PCH File.
- Name your file “PCH file”.
- Add these lines of code to the file:
#import "TestFairy.h"
#define NSLog(s, ...) do { NSLog(s, ##__VA_ARGS__); TFLog(s, ##__VA_ARGS__); } while (0) From the Project Navigator, select your project and the corresponding target.
Project > Build Settings > Search: "Prefix Header".
Under Apple LLVM 7.0" you will get the Prefix Header key.
Type the path of the file, for example: "$(SRCROOT)/$(PROJECT_NAME)/ProjectName-Prefix.pch". Please note that your file may be at a different location.
Make sure the option "Precompile Prefix Header" is set to YES.
Clean your project, and rebuild.
Syntax
TestFairy.log("<message>")
Code Example
TestFairy.log("Hello, TestFairy!")
We recommend wrapping all print
statements with a custom method, which will output to both the console and to TestFairy sessions. One suggestion we have is to create a new file named TestFairyLog.swift
in your source path, and add the following to the contents of the file:
//
// TestFairyLog.swift
//
// Copyright © TestFairy. All rights reserved.
//
import Foundation
public func print(_ items: Any..., separator: String = " ", terminator: String = "\n") {
let output = items.map { "\($0)" }.joined(separator: separator)
TestFairy.log(output)
Swift.print(output, terminator: terminator)
}
This will print any output to both the Xcode console, and to the active session on TestFairy.
Syntax
Code Example
TestFairy.log("Hello, TestFairy!");
We recommend wrapping all log statements with a custom method, which will output to both the console and to TestFairy sessions. One suggestion we have is to add a method that looks like this:
var testfairyConsoleLog = console.log; console.log = function(message) { testfairyConsoleLog(message); TestFairy.log(message); }
Syntax
TestFairy.log("<message>");
Code Example
// Be sure to import TestFairy const TestFairy = require('react-native-testfairy'); TestFairy.log("Hello, TestFairy!");
We recommend wrapping all log
statements with a custom method, which will output to both the console and to TestFairy sessions. One suggestion we have is to add a method that looks like this:
var testfairyConsoleLog = console.log; console.log = function(message) { testfairyConsoleLog(message); TestFairy.log(message); }
Syntax
TestFairySDK.log("<message>");
Code Example
// Be sure to import TestFairy import { TestFairySDK } from 'nativescript-testfairy'; TestFairySDK.log("Hello, TestFairy!");
We recommend wrapping all log
statements with a custom method, which will output to both the console and to TestFairy sessions. One suggestion we have is to add a method that looks like this:
var testfairyConsoleLog = console.log; console.log = function(message) { testfairyConsoleLog(message); TestFairySDK.log(message); }
We recommend wrapping all TFLog
statements with a custom method, which will output to both the console and to TestFairy sessions. One suggestion we have is to add a method that looks like this:
Code Example
// Be sure to import TestFairy using TestFairyLib; public static void Log(string format, params object[] arg) { using (var nsFormat = new NSString(string.Format(format, arg))) { CFunctions.TFLog(nsFormat.Handle, ""); Console.WriteLine(string.Format(format, arg)); } }
Now, you can log statements using this call:
Log("Hello {0}", "World");
Syntax
TestFairy.log("<message>");
Code Example
// Be sure to import TestFairy using TestFairyUnity; TestFairy.log("Hello, TestFairy!");
Syntax
AirTestFairy.log("<message>");
Code Example
// Be sure to import TestFairy import com.testfairy.AirTestFairy; AirTestFairy.log("Hello, TestFairy!");
Syntax
TiTestFairy.log("<message>");
Code Example
// Be sure to import TestFairy var TiTestFairy = require('com.testfairy.titestfairy'); TiTestFairy.log("Hello, TestFairy!");
Last updated on 2023-03-23