izik461
izik461

Reputation: 1173

Screen scale factor in XCTest

I am running UITest in which it's crucial to get the screen scale factor. Usually I've used UIScreen.main.scalem but XCUIScreen.main does not seem to have scale property. Is it possible to access device scale factor in UITests?

Upvotes: 1

Views: 216

Answers (1)

izik461
izik461

Reputation: 1173

I've managed it by adding AccessibilityIdentifier to window in AppDelegate that contains info needed:

int scaleFactor = [[NSNumber numberWithDouble:[UIScreen mainScreen].scale] intValue];
self.window.accessibilityIdentifier = [NSString stringWithFormat:@"windowScale:%d", scaleFactor];

And later accessing it and parsing in my tests:

var windowsScaleFactor: Int {
        let scaleFactorString = self.windows.firstMatch.identifier
        guard let scaleFactor = Int(scaleFactorString.components(separatedBy: CharacterSet.decimalDigits.inverted).joined(separator: "")) else {
            fatalError("Could not determine window scale factor")
        }
        return scaleFactor
    } 

Upvotes: 1

Related Questions