Gherkin est le format anglais qui permet à Cucumber d’exécuter le test d’automatisation. Le langage de fichier d'entités Gherkin est un langage spécifique à un domaine lisible par l'entreprise.
Ce code est destiné à convertir votre fichier de fonctionnalité Gherkin au format JSON basé sur la structure JSON Formatter.
Gherkin à l’analyseur JSON principal
// Fichier d'entités Gherkin en classe principale convertie en JSON
classe publique GToJRun {
// Ici, définissez le chemin Gherkin pour récupérer le fichier de fonctions et le chemin du fichier JSON sur
// écrit au format JSON
private static String featurePath = “c: \ Json \ login.feature”;
private String statique jasonPath = “c: \ Json \ login.json”;
// Définir comme joli / laid format pour la sortie JSON. Par défaut c'est joli
statique GtoJCore testG = nouveau GtoJCore («joli»);
public static void main (String [] args) {
testG.gherkinTojson (featurePath, jasonPath);
}
}
La classe principale est basée sur le framework de formatage JSON. Vous pouvez obtenir le temps de conversion si vous souhaitez vérifier le temps de traitement total d'un fichier de fonction plus volumineux.
Gherkin à JSON Parser Core
importer java.io.FileInputStream;
importer java.io.FileNotFoundException;
importer java.io.FileWriter;
importer java.io.IOException;
importer java.io.InputStreamReader;
importer java.io.UnsupportedEncodingException;
import gherkin.formatter.JSONFormatter;
import gherkin.formatter.JSONPrettyFormatter;
importer gherkin.parser.Parser;
importer gherkin.util.FixJava;
// Gherkin au fichier de base de l'analyseur Json.
Classe publique GtoJCore {
format de chaîne privé;
// Pour obtenir le temps total d'exécution (facultatif)
long startTime = System.currentTimeMillis ();
public GtoJCore (String outFormat) {
this.format = outFormat;
}
public String getOutFormat () {
format de retour;
}
public void gherkinTojson (String fPath, String jPath) {
// Définition du fichier de fonctions et du chemin du fichier JSON.
Gherkin de chaîne = null;
essayer {
gherkin = FixJava.readReader (new InputStreamReader (
nouveau FileInputStream (fPath), «UTF-8»));
} catch (FileNotFoundException e) {
System.out.println («Fichier de fonctionnalité introuvable»);
// e.printStackTrace ();
} catch (UnsupportedEncodingException e) {
e.printStackTrace ();
} catch (RuntimeException e) {
e.printStackTrace ();
}
StringBuilder json = new StringBuilder ();
JSONFormatter formateur;
// jolie ou moche sélection, jolie par défaut
if (format.equalsIgnoreCase («moche»)) {
formatter = new JSONFormatter (json); // pas joli
} autre {
formatter = new JSONPrettyFormatter (json); // jolie
}
Analyseur Analyseur = nouvel analyseur (formateur);
parser.parse (gherkin, fPath, 0);
formateur.done ();
formatter.close ();
System.out.println (“sortie json: n” + json + “'”);
// enfin rincer et fermer
essayer {
FileWriter fichier = nouveau FileWriter (jPath);
fichier.write (json.toString ());
file.flush ();
file.close ();
} catch (IOException e) {
e.printStackTrace ();
}
long endTime = System.currentTimeMillis ();
//optionnel
System.out.println (“n Durée totale:” + (endTime - startTime)
+ ”Millisecondes”);
}
}
Cela vous fournit un joli ou laid format JSON que vous pouvez sélectionner dans la classe principale. Vous pouvez définir le chemin du fichier pour le fichier de caractéristiques à lire et le fichier JSON à écrire avec l'option permettant de sélectionner un format de sortie joli ou moche.