2017-09-06 1 views
1

Je suis nouveau à Java, Rest-assuré et je suis incapable d'ajouter à mon script un moyen d'imprimer les journaux de la console dans un fichier lisible. J'ai essayé d'utiliser Log4j mais je n'ai pas pu l'implémenter correctement. J'ai trouvé en faisant des recherches, mais je ne sais pas comment mettre en œuvre - https://static.javadoc.io/com.jayway.restassured/rest-assured/2.7.0/com/jayway/restassured/config/LogConfig.htmlComment écrire des logs de console de repos dans un fichier redable ou txt en utilisant le framework Testng?

import static io.restassured.RestAssured.given; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.IOException; 
import java.nio.file.Files; 
import java.nio.file.Paths; 
import java.util.Properties; 

import org.testng.annotations.BeforeTest; 
import org.testng.annotations.Test; 
import files.reusableFunctions; 
import io.restassured.RestAssured; 
import io.restassured.http.ContentType; 
import io.restassured.path.xml.XmlPath; 
import io.restassured.response.Response; 

public class Playlist_Acknowledgement { 

    Properties prop = new Properties(); // creating prop object as global 

    @BeforeTest 
    public void testData1() throws Exception { 

     FileInputStream f = new FileInputStream("D:\\Tools\\Workspace\\BXF\\src\\files\\config.properties"); 
     prop.load(f); // to load the file object into prop file 

     reusableFunctions rf = new reusableFunctions(); 
     rf.createBxfPlaylistXml(); 
    } 

    @SuppressWarnings("unused") 
    @Test 
    public void postData() throws IOException { 


     String pth = prop.getProperty("PLAYLISTACK_ENDPOINT"); 
     File path = new File(pth); 

     File[] files = path.listFiles(); 
     for (int i = 0; i < files.length; i++) { 

      pth = files[i].toString(); 
      System.out.println("Path = " + pth); 
      String postData = new String(Files.readAllBytes(Paths.get(pth))); 
      // BaseURL 
      // value populating from property above 
      RestAssured.baseURI = prop.getProperty("AISHOST"); 

      Response resp = given().log().all() 
        .header("Content-Type", "application/XML; charset=utf-8") 
        .body(postData) 
        .when().post("/bxfxml") 
        .then().log().all() 
        .assertThat() 
        .statusCode(200).and() 
        .contentType(ContentType.XML) 
        .extract().response(); 


      // to convert raw data to string 
      XmlPath xmlResponse = reusableFunctions.rawToXML(resp); 
      String responseString = resp.asString(); 
      System.out.println("XML response is - " + responseString); 
      // Files.delete(files[i].toPath()); 
      // Files.copy(path.toPath(), prop.getProperty(key)); 
     } 
    } 
} 

Répondre

1

Vous pouvez configurer la configuration comme ci-dessous

LogConfig logconfig = new LogConfig().enableLoggingOfRequestAndResponseIfValidationFails().enablePrettyPrinting(true); 
RestAssured.config().logConfig(logconfig); 

Et après que vous pouvez procéder à votre régulière

RestAssured.given().log().all() 
     .header("Content-Type", "application/XML; charset=utf-8") 
     .body("") 
     .when().post("/bxfxml") 
     .then().log().all() 
     .assertThat() 
     .statusCode(200).and() 
     .contentType(ContentType.XML) 
     .extract().response();