samedi 14 mars 2020

How does Xcode UITest testing Orientation cases?

//
//  ABImagePicker_ExampleUITests.m
//  ABImagePicker_ExampleUITests
//
//  Created by Abenx on 2020/3/12.
//  Copyright © 2020 rushairer. All rights reserved.
//

#import <XCTest/XCTest.h>
#import "ABImagePicker_ExampleUITests-Swift.h"

@interface ABImagePicker_ExampleUITests : XCTestCase

@end

@implementation ABImagePicker_ExampleUITests

- (void)setUp {
    [super setUp];
    XCUIDevice.sharedDevice.orientation = UIDeviceOrientationPortrait;

    self.continueAfterFailure = NO;
    XCUIApplication *app = [[XCUIApplication alloc] init];

    [Snapshot setupSnapshot:app waitForAnimations:YES];

    [app launch];
    [self addUIInterruptionMonitorWithDescription:@"System Dialog"
                                          handler:^BOOL(XCUIElement * _Nonnull alert) {
           XCUIElement *okButton = alert.buttons[@"OK"];
           if ([okButton exists]) {
               [okButton tap];
           }
           XCUIElement *allowButton = alert.buttons[@"Allow"];
           if ([allowButton exists]) {
               [allowButton tap];
           }
           return YES;
    }];
}

- (void)tearDown {
    [super tearDown];
}

- (void)snapshopWithDescription:(NSString *)description {
    XCUIDevice.sharedDevice.orientation = UIDeviceOrientationPortrait;

    if (XCUIDevice.sharedDevice.orientation == UIDeviceOrientationLandscapeLeft || XCUIDevice.sharedDevice.orientation == UIDeviceOrientationLandscapeRight) {
        [Snapshot snapshot:[NSString stringWithFormat:@"%@ - Landscape", description] timeWaitingForIdle:0.0f];
    } else {
        [Snapshot snapshot:[NSString stringWithFormat:@"%@ - Portrait", description] timeWaitingForIdle:0.0f];
    }
}

- (void)testMainScreen {
    [self snapshopWithDescription:@"01MainScreen"];
}

- (void)testMainScreenLandscape {
    XCUIDevice.sharedDevice.orientation = UIDeviceOrientationLandscapeLeft;
    [self testMainScreen];
    XCUIDevice.sharedDevice.orientation = UIDeviceOrientationPortrait;
}

- (void)testAlbum {
    XCUIApplication *app = [[XCUIApplication alloc] init];
    XCTAssert([app.buttons[@"ImagePicker"] exists]);

    [app.buttons[@"ImagePicker"] tap];
    [[app coordinateWithNormalizedOffset:CGVectorMake(10000, 10000)] tap];

    [self snapshopWithDescription:@"02Album"];

    XCUIElementQuery *collectionViews = app.collectionViews;
    [[[[collectionViews childrenMatchingType:XCUIElementTypeCell] elementBoundByIndex:0] childrenMatchingType:XCUIElementTypeOther].element tap];

    [self snapshopWithDescription:@"04PhotoPreview"];

    XCUIElement *element = [collectionViews.scrollViews childrenMatchingType:XCUIElementTypeOther].element;
    [element swipeDown];

    XCUIElement *toolbars = collectionViews.toolbars[@"Toolbar"];
    [toolbars tap];

    [self snapshopWithDescription:@"05AlbumList"];
}

- (void)testAlbumLandscape {
    XCUIDevice.sharedDevice.orientation = UIDeviceOrientationLandscapeLeft;
    [self testAlbum];
    XCUIDevice.sharedDevice.orientation = UIDeviceOrientationPortrait;
}

- (void)testCamera {
    XCUIApplication *app = [[XCUIApplication alloc] init];
    XCTAssert([app.buttons[@"Custom Camera"] exists]);

    [app.buttons[@"Custom Camera"] tap];
    [[app coordinateWithNormalizedOffset:CGVectorMake(10000, 10000)] tap];

    [self snapshopWithDescription:@"03Camera"];
}

- (void)testCameraLandscape {
    XCUIDevice.sharedDevice.orientation = UIDeviceOrientationLandscapeLeft;
    [self testCamera];
    XCUIDevice.sharedDevice.orientation = UIDeviceOrientationPortrait;
}

/*
- (void)testLaunchPerformance {
    if (@available(macOS 10.15, iOS 13.0, tvOS 13.0, *)) {
        [self measureWithMetrics:@[XCTOSSignpostMetric.applicationLaunchMetric] block:^{
            [[[XCUIApplication alloc] init] launch];
        }];
    }
}
 */

@end


My idea is that start with the Portrait screen to testAlbum, then switch the Landscape screen to execute [self testalbum]; and then switch back to the Portrait screen. The problem is that [self testalbum]; is executed asynchronously and there is no waiting for the next line XCUIDevice.sharedDevice.orientation = UIDeviceOrientationPortrait;

And it's ugly. Is there a more elegant way? Thank you!


我的想法是先竖屏testAlbum,后切换横屏后再执行[self testAlbum];,然后切换回竖屏。问题是[self testAlbum];是异步执行的,没有进行等待。

而且这样处理太丑陋了,有没有更优雅的方法?谢谢!


Aucun commentaire:

Enregistrer un commentaire