From 3fb875947643df59cbf4ffc263a575b4ffbd9d5c Mon Sep 17 00:00:00 2001 From: elwafa Date: Sun, 19 Jan 2025 21:51:58 +0200 Subject: [PATCH 1/2] Descriptive the example in README --- README.md | 89 +++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 63 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index 7e07c41..51a1833 100644 --- a/README.md +++ b/README.md @@ -30,41 +30,78 @@ For detailed documentation, visit [https://php.volt-test.com](https://php.volt-t ### Installation ```bash -composer require volt-test/php-sdk +git clone git@github.com:volt-test/php-sdk.git +cd php-sdk +composer install ``` ### Basic Usage - +```bash +touch test.php +``` ```php setVirtualUsers(10) - ->setDuration('1m') - ->setRampUp('10s') - ->setTarget('https://api.example.com'); - -// Create a test scenario -$scenario = $test->scenario('Basic API Flow') - ->setWeight(1) - ->autoHandleCookies(); - -// Add test steps -$scenario->step('Get Users') - ->get('/api/users') - ->validateStatus('success', 200) - ->extractFromJson('userId', 'data[0].id'); - -$scenario->step('Get User Details') - ->get('/api/users/${userId}') - ->validateStatus('success', 200); - -// Run the test +$test = new VoltTest( + 'User Login Flow', + 'Tests user authentication process' +); +// Configure test parameters +$test + ->setVirtualUsers(1) // number of VUs (Virtual Users) +// ->setDuration('1s') // to run the test for 1 second + ->setHttpDebug(true); // to enable http debug mode remove it if you don't want to see the http debug + +// Create login scenario +$loginScenario = $test->scenario('User Login') + ->autoHandleCookies() // this will save cookies with all requests in this scenario + ->setDataSourceConfiguration(new DataSourceConfiguration(__DIR__ .'/data-file.csv', 'sequential', true)); + + +$loginScenario->step('Register') + ->get('http://localhost:8001/register') + ->extractFromRegex('csrf_token_register', 'name="_token" value="(.+?)"') // Extract the csrf token to submit a form + ->header('Accept', 'text/html'); + +$loginScenario->step('Submit Register') + ->post( + 'http://localhost:8001/register', + '_token=${csrf_token_register}&name=Test-v&email=${email}&password=${password}&password_confirmation=${password}') // send data with extracted data and source file + ->header('Content-Type', 'application/x-www-form-urlencoded') + ->validateStatus('status validator from php',302); + + +// Add first step - Get login page +$loginScenario->step('get_login_page') + ->get('http://localhost:8001/login') + ->header('Accept', 'text/html') + ->extractFromRegex('csrf_token', 'name="_token" value="(.+?)"') + ->validateStatus('status validator from php',200); + +// Add second step - Submit login +$loginScenario->step('submit_login') + ->post( + 'http://localhost:8001/login', + '_token=${csrf_token}&email=${email}&password=${password}') + ->header('Content-Type', 'application/x-www-form-urlencoded') +->validateStatus('status validator from php',302); + + +// Add third step - Visit dashboard +$loginScenario->step('visit_dashboard') + ->get('http://localhost:8001/dashboard') + ->header('Accept', 'text/html') + ->validateStatus('status_code', 200); +// Run the test +// This will start the test and block until it completes +// pass true to run() to run the test with progress and real time results $result = $test->run(); +// OR $test->run(true); to run the test with progress and real time results // Access test results echo "Success Rate: " . $result->getSuccessRate() . "%\n"; From 44be2a0b2a505ea9ac098702bd26728baa0bebfd Mon Sep 17 00:00:00 2001 From: elwafa Date: Sun, 19 Jan 2025 21:59:52 +0200 Subject: [PATCH 2/2] update Readme --- README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README.md b/README.md index 51a1833..47f60c9 100644 --- a/README.md +++ b/README.md @@ -40,6 +40,7 @@ composer install touch test.php ``` ```php +// test.php getSuccessRate() . "%\n"; echo "Average Response Time: " . $result->getAvgResponseTime() . "\n"; ``` +```bash +php test.php +``` + ## Features - Cross-platform support (Windows, Linux, MacOS)