You may follow the steps below to test a cron job from browser.
Step 1)
Create a PHP file names "cronLaunch.php" under your project root directory
FIle: cronLaunch.php
<?php
use Magento\Framework\App\Bootstrap;
require __DIR__ . '/app/bootstrap.php';
//require '../../app/bootstrap.php';
if (php_sapi_name() !== 'cli' && isset($_GET['job'])) {
define('CRONJOBCLASS', $_GET['job']);
} elseif (php_sapi_name() !== 'cli') {
die('Please add the class of the cron job you want to execute as a job parameter (?job=Vendor\Module\Class)');
} elseif (!isset($argv[1])) {
die('Please add the class of the cron job you want to execute enclosed IN DOUBLE QUOTES as a parameter.' . PHP_EOL);
} else {
define('CRONJOBCLASS', $argv[1]);
}
class CronRunner extends \Magento\Framework\App\Http
implements \Magento\Framework\AppInterface
{
public function __construct(
\Magento\Framework\App\State $state,\Magento\Framework\App\Response\Http $response)
{
$this->_response = $response;
$state->setAreaCode('adminhtml');
}
function launch()
{
$cron = \Magento\Framework\App\ObjectManager::getInstance()
->create(CRONJOBCLASS);
$cron->execute();
return $this->_response;
}
}
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
$app = $bootstrap->createApplication('CronRunner');
$bootstrap->run($app);
step 2)
Open browser and text the cron job
I assume you your cron class file is MyVendor/MyModule/Cron/CronClass.php
so enter the following URL in your browser and test your cron file.
http://localhost/YOURMAGENTOPROJECT/cronLaunch.php?job=MyVendor\MyModule\Cron\CronClass