0

I am automating IOS app test cases using Appium. I have a scenario in which I have to identify.. i am on which screen so that I can close the screen

In broader view , we have a inapp notification in our app which get open in random way.

So I can handle these condition if I would be able to identify the current screen. I handle the same thing in Android using driver.currentActivity method

I need the some kind of same method for IOS apps

argneshu
  • 21
  • 3
  • See this link might help you - http://stackoverflow.com/questions/22882078/how-to-get-visible-viewcontroller-from-app-delegate-when-using-storyboard – Bharat Modi Sep 28 '16 at 09:20

1 Answers1

-3

you get top most view controller by using below method

+ (UIViewController*) topMostController
{
    UIViewController *topController = [UIApplication sharedApplication].keyWindow.rootViewController;

    while (topController.presentedViewController) {
    topController = topController.presentedViewController;
}

return topController;

}

Or if you have navigation or UIabbar controller then use below method to get visible view controller

- (void)applicationWillResignActive:(UIApplication *)application
{
    UIViewController *vc = [self visibleViewController:[UIApplication sharedApplication].keyWindow.rootViewController];
}

- (UIViewController *)visibleViewController:(UIViewController *)rootViewController
{
if (rootViewController.presentedViewController == nil)
{
    return rootViewController;
}
if ([rootViewController.presentedViewController isKindOfClass:[UINavigationController class]])
{
    UINavigationController *navigationController = (UINavigationController *)rootViewController.presentedViewController;
    UIViewController *lastViewController = [[navigationController viewControllers] lastObject];

    return [self visibleViewController:lastViewController];
}
if ([rootViewController.presentedViewController isKindOfClass:[UITabBarController class]])
{
    UITabBarController *tabBarController = (UITabBarController *)rootViewController.presentedViewController;
    UIViewController *selectedViewController = tabBarController.selectedViewController;

    return [self visibleViewController:selectedViewController];
}

UIViewController *presentedViewController = (UIViewController *)rootViewController.presentedViewController;

return [self visibleViewController:presentedViewController];

}

iOS Developer
  • 437
  • 4
  • 11
  • I am using java for my automation scripts.Please let me know how can I run your piece of code in java environment, – argneshu Oct 03 '16 at 09:01
  • Question specifically asked about doing this in Appium. There is no need to copy paste iOS code. – Taha Samad May 03 '18 at 08:11