2016-08-09 71 views
1

我已經在Xcode 7中編寫了一些UI測試,並且當我需要引用按鈕時,我使用它的accessibility.identifier。這個邏輯適用於所有的語言。在iOS中使用多語言進行UI自動化測試

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

和Xcode 7.3,當我嘗試推出這一代碼,因爲如果仿真語言不是英語的按鈕無法找到測試失敗。我也嘗試記錄導航,以檢查Xcode在語言與英語不同時如何讀取此按鈕,並且我發現它使用翻譯作爲鍵......它根本沒有任何意義!

這些測試對創建屏幕截圖非常有用......但顯然,對於這個問題,我無法爲所有語言運行測試(並創建屏幕)。

如果無法通過標識符識別,我該如何指向絕對路徑的按鈕!?

----編輯

我發現了主要問題。該做的翻譯公司翻譯了labelidentifier字段:/

我試圖讓使用app.buttons.elementBoundByIndex(1)的元素,但它似乎沒有正常工作

回答

0

您可以有實際accessibilityIdentifier分配到按鈕,然後嘗試訪問它。隨着文本隨時可能改變,訪問帶有文本的按鈕總是一個壞主意。 例如:XCUIApplication()。buttons [「AppSignInIdentifier」]

0

我希望你的項目維護本地化文件。我面臨同樣的問題。如果是這樣,那麼本地化不直接與UIAutomation一起工作。 有一個解決方法,我與你分享代碼片段。 您需要首先找到捆綁包中的xcodeproj文件。由於這些文件未捆綁在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; 
} 

,並使用這個//

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

,而且爲更好地利用自動化的,請設置無障礙標誌對於您的控制,因此,這將是容易的自動化過程中找到出於這個原因,處理器上的開銷較小,不太容易出現崩潰和問題。

0

輔助功能標識符不應是本地化的字符串,否則當您以不同語言運行您的應用時,它們會發生變化。

只需對可訪問性標識符進行硬編碼即可使其持續存在,而不管語言如何。

button.accessibilityIdentifier = "myButton"