2013-03-06 18 views
6

Sto utilizzando lo strumento "Strumenti" Xcode per creare l'automazione per un'applicazione iOS e devo verificare che l'etichetta per una voce che ho creato nella mia app sia corretta.Come posso verificare il nome di un'etichetta in una vista tabella utilizzando Strumenti su iOS?

Il codice elencato di seguito è, per qualche ragione, non risultante in un passaggio solido o negativo. Al contrario, quando viene eseguito, viene visualizzato un avviso "Problema" nel registro e il test termina senza chiusura esplicita.

Voglio cambiare i miei test in modo da controllare il nome dell'etichetta che so sia stato creato, perché posso vederlo con AccessibilityViewer dopo l'esecuzione dell'automazione.

Se l'etichetta è corretta, desidero registrare il test come passaggio.

Ho usato UIATarget.localTarget().logElementTree() per mappare il mio albero degli elementi e ho utilizzato AccessibilityInspector per verificare il nome della mia etichetta dopo che la mia voce è stata creata. Il problema è che non riesco a ottenere la sintassi per verificare questo corretto.

Il mio ispettore di accessibilità ha verificato che il nome dell'etichetta sia: MyDogs! e ha i tratti di Static Text e dà il Frame di {{114, 0}, {166,480}}

Osservando l'albero degli elementi - che vorrei incollare qui, sembra l'etichetta si troverebbe in questo cammino:

\Target 
-+ 
--\Application 
---+ 
----\Window 
-----+ 
------\TableView 
-------+ 
--------\TableCell: name:MyDogs! rect:{0, 40},{480,166}} 
---------|UIAStaticText: name:MyDogs! value:MyDogs! rect:{{0, 40},{480, 166}} 
---------|UIAButton: name:story list share rect:{{439, 41},{33, 28}} 

qualcuno può dirmi come diamine verificare questa etichetta?

mio codice attuale assomiglia a questo (ma non è il controllo per l'etichetta - perché non so come):

var testName = "LoginCreateEntry"; 

//Start test logging 
UIALogger.logStart(testName); 

//This is supposed to target the entry that my automation has created. 
//The flow goes, run the automation that creates the entry, then verify that the entry 
//got created as expected and is visible to the user in the iPhone interface. 

    var myEntry = target.frontMostApp().mainWindow().scrollViews().staticTexts()["My Dogs!"].value(); 

    var entryName = "My Dogs!"; 

//Do a bunch of UI automation here to create my entry, which results in the entry 
//appearing in the mainWindow with the label: My Dogs! 

//If myEntry evaluates to true, then call this test a pass. 

if (myEntry === entryName) {  
UIALogger.logMessage("My entry was created!"); 

    //Mark the test as a PASS 
    UIALogger.logPass(testName); 
} 
else { 

    UIALogger.logMessage("My entry was not created!"); 

    //Mark the test as a FAIL 
    UIALogger.logFail(testName); 
    } 

//End test 

Tutte le risposte o aiuto sarebbero apprezzate !!

--------------------------------- AGGIORNAMENTO ------------ --------------------------
Grazie a tutti per il vostro aiuto! Ho effettivamente ottenuto il valore del titolo e mostrerò la mia soluzione qui sotto. Ma NON POSSO che la funzionalità di registrazione pass/fail funzioni correttamente, non importa quello che faccio - e il problema è stato anche encountered by others. Continuo a ricevere il messaggio arrabbiare

Issue: Script ended without explicting closing this test

alla fine dei miei test. Mi sto convincendo che è un bug con gli strumenti.

Ecco il mio test aggiornato:

var target = UIATarget.localTarget(); 
var app = UIATarget.localTarget().frontMostApp(); 

var testName = "LoginCreateEntry"; 

//Start test logging 
UIALogger.logStart(testName); 

//Do lots of gui automation stuff here to create the entry which will appear in my app interface. 
//I want to verify that the title I gave the entry matches what appears in the app interface 

var window = app.mainWindow(); 
var tableView = window.tableViews()[0]; 
var tableGroup = tableView.groups()[0]; 
var entryName = "My Dogs!"; 

var myEntry = tableView.cells()[0].name(); //<-- This is what I needed!!! 

UIALogger.logMessage("My Story Title: " + myEntry); //Print out entry name in log 

if (myEntry === entryName) {  

    UIALogger.logMessage("My entry was created!"); 

    //Mark the test as a PASS 
    UIALogger.logPass (testName); 

} else { 

    UIALogger.logMessage("My entry was not created!"); 

    //Mark the test as a FAIL 
    UIALogger.loFails (testName); 

    } 
//End test 
+0

Ho apportato alcune modifiche alla tua domanda per portarlo in linea con le preferenze di stile generali su SO così come per darti tag più mirati, spero che questo ti aiuti a ottenere buone risposte. Grande domanda a proposito! –

+0

Qualcosa che potrebbe essere interessante per casi futuri come questo - se trovi la risposta da solo, è apparentemente OK pubblicare la risposta su SO e accettarla. :) Vedi http://meta.stackexchange.com/questions/17845/etiquette-for-answering-your-own-question –

risposta

5

mi consiglia di utilizzare tuneup_js. Con questa libreria si può facilmente creato casi di test e verificare se l'etichetta esiste ed è pari a My Dogs

Si può usare in questo modo

test("LoginCreateEntry", function(target,app){ 
    //Create Entry 
    //.... 
    var myEntry = target.frontMostApp().mainWindow().scrollViews().staticTexts()["My Dogs!"].name(); 

    //Check is Label is equal to My Dogs 
    //Test "LoginCreateEntry" will fail if myEntry is not equal to My Dogs 
    assertEquals(myEntry, "My Dogs"); 
}); 

P.S. dovresti usare e non .value() per ottenere il nome dell'etichetta

+0

Grazie mille! Risulta che questo è quello che mi serviva: var myEntry = tableView.cells() [0] .name(); – Wulf

5

Almeno a partire da estate 2012 e iOS 5, ho trovato che non potevo accedere a un elemento UIA da accessibilityLabel a meno che anche l'accessibilityIdentifier fosse impostato.

E sfortunatamente da quel momento (e da Xcode 4.2) l'accessibilityIdentifier poteva essere impostato solo nel codice, non in Interface Builder. Non sono sicuro che le cose siano migliorate da allora. Vedere questo SO domanda per ulteriori informazioni:

can I set accessibility identifier in interface builder? Xcode4.2

Ma, come ho già detto, è possibile ottenere l'accesso UIA da automationIdentifier a lavorare se si ha accesso alla fonte della app, però. (O può influenzare qualcuno con accesso, suppongo!)

Ho un progetto su github che mostra come impostare l'accessibilityIdentifier su una UILabel (nell'app source) e quindi scrivere un test UIA che acceda l'elemento con successo come sembra che il tuo codice stia cercando di fare.

Il primo è il mio file di prova, dove cerco di individuare un UILabel che visualizza la stringa "!":

https://github.com/billagee/UnicodeTapper-iphone4.2/blob/master/UnicodeTapperTests/tests.js

// for iOS 5 - note 4 must be handled differently 
    var charDisplayed = window.staticTexts()["bigCharLabel"].value(); 
    if (charDisplayed == "!") { 
     UIALogger.logPass("Exclamation point displayed at startup"); 
    } else { 
     UIALogger.logFail("Incorrect character displayed: " + charDisplayed); 
    } 

Poi, nella fonte applicazione di destinazione, impostazione degli attributi di accessibilità del UILabel si presenta così:

// If device supports it, set accessibility identifiers for the UILabels 
// in order to find them in UIAutomation easily. Note that the 
// accessibilityIdentifier can't be set in IB yet, as of Xcode 4.3.3; 
// also, it's only supported in iOS 5 and up. 
topBarLabel.isAccessibilityElement = YES; 
bigCharLabel.isAccessibilityElement = YES; 
if ([topBarLabel respondsToSelector:@selector(accessibilityIdentifier)]) { 
    topBarLabel.accessibilityIdentifier = @"topBarLabel"; 
    bigCharLabel.accessibilityIdentifier = @"bigCharLabel"; 
} else { 
    topBarLabel.accessibilityLabel = @"topBarLabel"; 
    bigCharLabel.accessibilityLabel = @"bigCharLabel";   
} 

La sorgente completo è qui:

https://github.com/billagee/UnicodeTapper-iphone4.2/blob/master/UnicodeTapper/UnicodeTapperViewController.m

+0

Queste informazioni sono state passate sul nostro team Dev ed è stato molto utile. È ovvio che l'app in prova non è stata progettata pensando all'automazione, quindi questa è stata un'esperienza di apprendimento per tutti. Le app iOS pensate per la grande distribuzione devono essere progettate pensando all'automazione sin dall'inizio. – Wulf

+0

Grazie mille! – Wulf

+0

Felice che sia stato utile! Se questa risposta e la risposta dell'altro poster sono state utili, è sempre utile valorizzarle. Il che mi ricorda, dovrei invertire la tua domanda. –