google/EarlGrey

White-box testing CocoaPods

shaneong opened this issue · 6 comments

Hi, does EarlGrey 2.2.0 support white-box testing through Cocoapods installation?

Yes, you can do it via the GREYRemoteClassInApp() API to get app side objects. We're 100% open to suggestions for CocoaPods White boxing workflows.

Thank you, I found this example using GREYRemoteClassInApp() - https://github.com/google/EarlGrey/blob/earlgrey2/Tests/Functional/Sources/Swift/SwiftTests.swift#L236

Just to make sure, the steps detailed here are only for XcodeProject setup and does not work with Cocoapods correct? https://github.com/google/EarlGrey/blob/earlgrey2/docs/white-boxing-setup-guide.md

Hi @tirodkar, I am facing Undefined symbol: _OBJC_CLASS_$_{CustomAppSideObject} when using GREYRemoteClassInApp(), e.g. self.customObject = GREYRemoteClassInApp(classVal: CustomAppSideObject.self).sharedInstance() in a separate class that gets called by the test class.

Example:

// HelperClass.swift

class HelperClass { 
  var customObject: CustomAppSideObject!
  public init() { 
    self.customObject = GREYRemoteClassInApp(classVal: CustomAppSideObject.self).sharedInstance() // undefined for this 
  }
  ...

}

// EG2Tests.swift

class EG2Tests.swift {
   func testExample() { 
     HelperClass()
     let statusBarOrientation = GREYRemoteClassInApp(classVal: UIApplication.self).shared.statusBarOrientation.isPortrait // this is fine.
     XCTAssertTrue(statusBarOrientation)
   } 
}

From the XcodeProject setup tutorial, we had to set up a separate Bundle that is injected to the app, and set up GREYHostApplicationDistantObject extensions, which were visible to the test code in order to make this work. Is there an analogy for this with the Pods setup that we have to do as well?

EDIT:
Turns out same error occurs in the test itself too.

// EG2Tests.swift

class EG2Tests.swift {
   func testExample() { 
     HelperClass()
     let statusBarOrientation = GREYRemoteClassInApp(classVal: UIApplication.self).shared.statusBarOrientation.isPortrait // this is fine.
     XCTAssertTrue(statusBarOrientation)
    GREYRemoteClassInApp(classVal: CustomAppSideObject.self).sharedInstance() // undefined for this 
   } 
}

@shaneong you have to make sure that the search paths of the application class being used is present in the tests. Can you confirm that CustomAppSideObject is exposed to the test side? Also, eDO uses the ObjC runtime underneath. You need to add @objc to class CustomAppSideObject .

@shaneong are you able to figure out "Cocoapods + Whitebox testing"

Screen Shot 2021-03-31 at 2 20 59 PM

@tirodkar is "GREYRemoteClassInApp" got removed in the latest pod versions? I am not able to find in earlGrey source code as well