简体   繁体   English

在iOS中使用多语言进行UI自动化测试

[英]UI automation testing with multilanguage in iOS

I've written some UI tests in Xcode 7 and when I need to refer to a button I use its accessibility.identifier . 我在Xcode 7中编写了一些UI测试,当我需要引用一个按钮时,我使用了它的accessibility.identifier This logic worked correctly for all the languages. 这种逻辑适用于所有语言。

app.buttons["signin"].tap()

With Xcode 7.3 when I try to launch this code the test fails because the button cannot be found if the simulator language is not English. 使用Xcode 7.3,当我尝试启动此代码时,测试失败,因为如果模拟器语言不是英语,则无法找到该按钮。 I've also tried to record the navigation to check how Xcode reads this button when language is different by English and I found out that it use the translations as key... it doesn't make any sense at all! 我还尝试记录导航以检查Xcode在英语语言不同时如何读取此按钮,我发现它使用翻译作为关键...它根本没有任何意义!

Those test were really useful to create screenshots... but obviously with this issue I cannot run tests (and create screens) for all the languages. 这些测试对于创建屏幕截图非常有用......但很明显,在这个问题上,我无法为所有语言运行测试(并创建屏幕)。

How can I point to a button in absolute way if it cannot be recognized by identifier!? 如果标识符无法识别,我怎么能以绝对方式指向按钮!?

----EDIT - - 编辑

I found the main issue. 我找到了主要问题。 The company that did the translation has translated the labelidentifier fields :/ 做翻译的公司翻译了labelidentifier字段:/

I'm trying to get the element using app.buttons.elementBoundByIndex(1) but it doesn't seem to work correctly 我正在尝试使用app.buttons.elementBoundByIndex(1)获取元素,但它似乎无法正常工作

You can have actual accessibilityIdentifier assigned to button and then try accessing it. 您可以将实际的accessibilityIdentifier分配给按钮,然后尝试访问它。 Accessing buttons with text is always a bad idea as text may change anytime. 访问带有文本的按钮总是一个坏主意,因为文本可能随时更改。 Eg : XCUIApplication().buttons["AppSignInIdentifier"] 例如:XCUIApplication()。buttons [“AppSignInIdentifier”]

I hope your project is maintaining the localization file. 我希望你的项目是维护本地化文件。 I faced the same issue. 我遇到了同样的问题。 If so then localization doesn't work directly with UIAutomation yet. 如果是这样,那么本地化不能直接与UIAutomation一起使用。 There is a workaround for this, I am sharing the code snippet with you. 有一个解决方法,我正在与你共享代码片段。 You need to find out the xcodeproj file in your bundle first. 您需要先在捆绑包中找到xcodeproj文件。 As these files are not bundled in the UIAutomation target. 由于这些文件未捆绑在UIAutomation目标中。

- (NSString *)getLocalizedString:(NSString *)string withComments:(NSString *)comments {
    NSString *userLocale = [[NSLocale currentLocale] localeIdentifier];
    NSString *userLanguage = [userLocale substringToIndex:2];
    NSString *path = [self getProjectPath];

    path = [path stringByAppendingPathComponent:[NSString stringWithFormat:@"/YourProjectFileName/Resources/Localization/%@.lproj",userLanguage]];

    NSBundle *bundle = [NSBundle bundleWithPath:path];
    NSString *localizedString = NSLocalizedStringFromTableInBundle(string, @"Localizable", bundle, comments);

    return localizedString;
}


- (NSString *)getProjectPath {

    NSString *currentSourceFilePath = [[NSString stringWithCString:__FILE__ encoding:NSUTF8StringEncoding] stringByDeletingLastPathComponent];

    NSString *currentPath = [currentSourceFilePath copy];

    BOOL foundIt = NO;

    do {
        NSString *testPath = [currentPath stringByAppendingPathComponent:@"XYZ.xcodeproj"];

        if ([[NSFileManager defaultManager] fileExistsAtPath:testPath]) {
            // found it
            foundIt = YES;
            break;
        }

        if ([currentPath isEqualToString:@"/"]) {
            // cannot go further up
            break;
        }

        currentPath = [currentPath stringByDeletingLastPathComponent];

    } while ([currentPath length]);

    if (!foundIt) {
        return nil;
    }

    return currentPath;
}

And to use this // 并使用这个//

NSString *loginStr = [self getLocalizedString:@"Login" withComments:@""];

And moreover for the better use of Automation, please set the accessibility labels for your controls, so that it would be easy for the automation process to find out that, which is less overhead on processor and less prone to crashes and issues. 此外,为了更好地使用自动化,请为控件设置辅助功能标签,以便自动化过程很容易找到,这样可以降低处理器的开销,减少崩溃和问题。

Accessibility identifiers should not be localized strings, otherwise they will change when you are running your app in a different language. 辅助功能标识符不应是本地化字符串,否则当您使用其他语言运行应用程序时,它们将会更改。

Just hard-code the accessibility identifiers to make them persistent regardless of language. 只需硬编码可访问性标识符,使其无论语言如何都是持久的。

button.accessibilityIdentifier = "myButton"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM