Untitled

mail@pastecode.io avatar
unknown
java
a month ago
4.4 kB
2
Indexable
Never
import org.apache.http.entity.ContentType;
import org.apache.jmeter.protocol.http.util.HTTPConstants;
import org.junit.jupiter.api.Test;
import us.abstracta.jmeter.javadsl.core.TestPlanStats;
import us.abstracta.jmeter.javadsl.core.postprocessors.DslRegexExtractor;
import us.abstracta.jmeter.javadsl.http.DslHttpSampler;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.time.Duration;

import static org.assertj.core.api.Assertions.assertThat;
import static us.abstracta.jmeter.javadsl.JmeterDsl.*;
import static us.abstracta.jmeter.javadsl.core.listeners.AutoStopListener.AutoStopCondition.errors;
import static us.abstracta.jmeter.javadsl.dashboard.DashboardVisualizer.dashboardVisualizer;

public class StoreExercise{
    @Test
    public void test() throws IOException {
        TestPlanStats stats =
                testPlan(
                    csvDataSet(testResource("datos.csv")),
                    threadGroup(3, 10,
                      httpDefaults()
                        .encoding(StandardCharsets.UTF_8)
                        .followRedirects(true)
                        .protocol("https")
                        .url("petstore.octoperf.com"),
                      transaction("Main",
                        getRequest("mainPage", null,"Enter the Store"),
                      transaction("Enter the Store",
                        getRequest("CatalogAction","/actions/Catalog.action", "Elevate you load-testing")
                          .children(
                            regexExtractor("JSESSIONID", "JSESSIONID=(.*?);")
                                    .fieldToCheck(DslRegexExtractor.TargetField.RESPONSE_HEADERS),
                            regexExtractor("SOURCE_PAGE", "_sourcePage\" value=\"(.*?)\"")
                                    .fieldToCheck(DslRegexExtractor.TargetField.RESPONSE_BODY),
                            regexExtractor("FP", "__fp\" value=\"(.*?)\"")
                                    .fieldToCheck(DslRegexExtractor.TargetField.RESPONSE_BODY)
                            )),
                      transaction("Search",
                        threadPause(Duration.ofSeconds(3)),
                        postRequest("searchPet","/actions/Catalog.action;jsessionid=${JSESSIONID}", "Return to Main Menu")
                          .param("keyword", "${MASCOTA}")
                          .param("searchProducts", "Search")
                          .param("_sourcePage", "${SOURCE_PAGE}")
                          .param("__fp", "${FP}")
                          .children(
                            regexExtractor("PRODUCT_ID", ";productId=(.*?)\"")
                                    .fieldToCheck(DslRegexExtractor.TargetField.RESPONSE_BODY)
                          )),
                      transaction("Open item",
                       constantTimer(Duration.ofSeconds(4)),
                       getRequest("viewPet","/actions/Catalog.action", "Add to Cart")
                        .param("viewProduct", "")
                        .param("productId", "${PRODUCT_ID}"),
                        dummySampler("Product id: ${PRODUCT_ID} - -  Pet: ${MASCOTA} ")
                  )), dashboardVisualizer()
                )).run();
        testAssertions(stats);
    }

  private DslHttpSampler getRequest(String name, String path, String assertionText){
        return httpSampler(name, path)
                .downloadEmbeddedResources()
                .children(
                        responseAssertion().containsSubstrings(assertionText),
                        autoStop()
                                .when(errors().total().greaterThanOrEqualTo(5L))
                );
    }

    private DslHttpSampler postRequest(String name, String path, String assertionText) {
        return httpSampler(name, path)
                .downloadEmbeddedResources()
                .children(
                        responseAssertion().containsSubstrings(assertionText)
                )
                .method(HTTPConstants.POST)
                .contentType(ContentType.APPLICATION_FORM_URLENCODED);
    }

  private static void testAssertions(TestPlanStats stats) {
    assertThat(stats.overall().sampleTimePercentile99()).isLessThan(Duration.ofMillis(1000));
    assertThat(stats.overall().errorsCount()).isEqualTo(0);
  }
    
}
Leave a Comment