I have a folder that consist a python Selenium Appium script and a Laravel folder, here is the folder path:
mobile-automation
laravel
app.py
What I'm trying to do is, I want to run the shell script pytest -s app.py
from Laravel. So I already create the frontend using Laravel:
welcome.blade.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN" crossorigin="anonymous">
<title>Run Test</title>
</head>
<body>
<div class="container">
<button type="button" class="btn btn-outline-primary w-100" id="runScriptButton">All Feature</button>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-C6RzsynM9kWDrMNeT87bh95OGNyZPhcTNXj1NW7RuBCsyN/o0jlpcV8Qyq46cDfL" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.11.8/dist/umd/popper.min.js" integrity="sha384-I7E8VVD/ismYTF4hNIPjVp/Zjvgyol6VFvRkX/vR+Vc4jQkC+hVqc2pM8ODewa9r" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.min.js" integrity="sha384-BBtl+eGJRgqQAUMxJ7pMwbEyER4l1g+O15P+16Ep7Q9Q+zqX6gSbd85u4mG4QzX+" crossorigin="anonymous"></script>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
$(document).ready(function () {
$('#runScriptButton').click(function () {
// Execute an AJAX request to the /run-script route
$.ajax({
url: "{{ route('runScript') }}", // Replace with your Laravel route
method: "GET",
success: function (response) {
alert('Script executed successfully');
},
error: function (error) {
alert('Error: Script encountered an error');
}
});
});
});
</script>
</body>
</html>
then there's the route
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ScriptController;
Route::get('/', function () {
return view('welcome');
});
Route::get('/run-script', [ScriptController::class, 'runScript'])->name('runScript');
and the Controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class ScriptController extends Controller
{
public function runScript()
{
try {
// Your script execution code here
$command = '/mobile-automation/app.py';
exec($command, $output, $returnCode);
if ($returnCode === 0) {
return response('Script executed successfully', 200);
} else {
return response('Script encountered an error', 500);
}
} catch (\Exception $e) {
// Log the exception
\Log::error('Error executing script: ' . $e->getMessage());
return response('Internal Server Error', 500);
}
}
}
When I click the button, it gives an error Error: Script encountered an error
and when I check the console:
Any solution on this?
check for file path , that you are executing , use shell_exec() function
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class ScriptController extends Controller
{
public function runScript()
{
try {
// Your script execution code here
$command = '/mobile-automation/app.py';
$output=shell_exec($command);
dd($output);
if ($returnCode === 0) {
return response('Script executed successfully', 200);
} else {
return response('Script encountered an error', 500);
}
} catch (\Exception $e) {
// Log the exception
\Log::error('Error executing script: ' . $e->getMessage());
return response('Internal Server Error', 500);
}
}
}