반응형

프로젝트 생성 후 pom.xml 설정

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.test.ngrinder</groupId>
    <artifactId>ngrinder-test</artifactId>
    <version>1.0-SNAPSHOT`</version>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.15</version>
                <configuration>
                    <argLine>
                        -javaagent:${settings.localRepository}/net/sf/grinder/grinder-dcr-agent/3.9.1/grinder-dcr-agent-3.9.1.jar
                    </argLine>
                    <useSystemClassLoader>true</useSystemClassLoader>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>nhnopensource.maver.repo</id>
            <url>https://github.com/nhnopensource/nhnopensource.maven.repo/raw/master/releases</url>
        </repository>
    </repositories>

    <dependencies>
        <dependency>
            <groupId>org.ngrinder</groupId>
            <artifactId>ngrinder-groovy</artifactId>
            <version>3.3</version>
        </dependency>
    </dependencies>
</project>

예시 스크립트 - 1

import HTTPClient.Cookie
import HTTPClient.CookieModule
import HTTPClient.HTTPResponse
import HTTPClient.NVPair
import net.grinder.plugin.http.HTTPPluginControl
import net.grinder.plugin.http.HTTPRequest
import net.grinder.script.GTest
import net.grinder.scriptengine.groovy.junit.GrinderRunner
import net.grinder.scriptengine.groovy.junit.annotation.BeforeProcess
import net.grinder.scriptengine.groovy.junit.annotation.BeforeThread
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith

import static net.grinder.script.Grinder.grinder
import static org.hamcrest.Matchers.is
import static org.junit.Assert.assertThat

// import static net.grinder.util.GrinderUtils.* // You can use this if you're using nGrinder after 3.2.3

/**
 * A simple example using the HTTP plugin that shows the retrieval of a
 * single page via HTTP.
 *
 * This script is automatically generated by ngrinder.
 *
 * @author admin
 */
@RunWith(GrinderRunner)
class TestRunner {

    public static GTest test
    public static HTTPRequest request
    public static NVPair[] headers = []
    public static NVPair[] params = []
    public static Cookie[] cookies = []

    // 프로세스가 생성될 때 실행
    // 사전 설정, 리소스 로드 작업
    @BeforeProcess
    static void beforeProcess() {
        HTTPPluginControl.getConnectionDefaults().timeout = 6000
        test = new GTest(1, "github.com")
        request = new HTTPRequest()
        grinder.logger.info("before process.")
    }

    // 각 쓰레드가 실행되기 전에 실행
    // 사전 로그인 작업, 필요한 쿠킹 세팅 작업
    @BeforeThread
    void beforeThread() {
        test.record(this, "test")
        grinder.statistics.delayReports = true
        grinder.logger.info("before thread.")
    }

    // 각 테스트가 실행되기 전에 실행
    // 사전 로그인 작업, 필요한 쿠킹 세팅 작업
    @Before
    void before() {
        request.setHeaders(headers)
        cookies.each { CookieModule.addCookie(it, HTTPPluginControl.getThreadHTTPClientContext()) }
        grinder.logger.info("before thread. init headers and cookies")
    }

    // 테스트 실행
    @Test
    void test() {
        HTTPResponse result = request.GET("https://github.com/naver/ngrinder/blob/master/script-sample/test-with-login/login.groovy", params)

        if (result.statusCode == 301 || result.statusCode == 302) {
            grinder.logger.warn("Warning. The response may not be correct. The response code was {}.", result.statusCode)
        } else {
            assertThat(result.statusCode, is(200))
        }
    }
}

예시 스크립트 - 2

import HTTPClient.CookieModule
import HTTPClient.HTTPResponse
import HTTPClient.NVPair
import net.grinder.plugin.http.HTTPPluginControl
import net.grinder.plugin.http.HTTPRequest
import net.grinder.script.GTest
import net.grinder.scriptengine.groovy.junit.GrinderRunner
import net.grinder.scriptengine.groovy.junit.annotation.BeforeProcess
import net.grinder.scriptengine.groovy.junit.annotation.BeforeThread
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith

/**
 * A simple example using the HTTP plugin that shows the retrieval of a
 * single page via HTTP. 
 * 
 * This script is automatically generated by ngrinder.
 * 
 * @author Gisoo Gwon
 */
@RunWith(GrinderRunner)
class Login {
    public static GTest test
    public static HTTPRequest request
    public Object cookies = []
 
    @BeforeProcess
    public static void beforeProcess() {
        HTTPPluginControl.getConnectionDefaults().timeout = 6000
        test = new GTest(1, "login test")
        request = new HTTPRequest()
        test.record(request);
    }
 
    @BeforeThread
    public void beforeThread() {
        net.grinder.script.Grinder.grinder.statistics.delayReports=true;
         
        // reset to the all cookies
        def threadContext = HTTPPluginControl.getThreadHTTPClientContext()
        cookies = CookieModule.listAllCookies(threadContext)
        cookies.each {
            CookieModule.removeCookie(it, threadContext)
        }
         
        // do login & save to the login info in cookies 
        NVPair[] params = [new NVPair("id", "MY_ID"), new NVPair("pw", "MY_PASSWORD")];
        HTTPResponse res = request.POST("https://login.site.com/login/do", params);
        cookies = CookieModule.listAllCookies(threadContext)
    }
 
    @Before
    public void before() {
        // set cookies for login state
        def threadContext = HTTPPluginControl.getThreadHTTPClientContext()
        cookies.each {
            CookieModule.addCookie(it ,threadContext)
            net.grinder.script.Grinder.grinder.logger.info("{}", it)
        }
    }
 
    @Test
    public void test(){
        HTTPResponse result = request.GET("http://my.site.com")
 
        if (result.statusCode == 301 || result.statusCode == 302) {
            net.grinder.script.Grinder.grinder.logger.warn("Warning. The response may not be correct. The response code was {}.", result.statusCode);
        } else {
            org.junit.Assert.assertThat(result.statusCode, org.hamcrest.Matchers.is(200));
        }
    }
}

참고

반응형

'Development > nGrinder' 카테고리의 다른 글

[nGrinder] 설치  (0) 2019.09.08

+ Recent posts