Skip to content

Instantly share code, notes, and snippets.

@oceanapplications
Created May 18, 2018 02:49
Show Gist options
  • Save oceanapplications/2edf349178813e399dfb3d68c7e22757 to your computer and use it in GitHub Desktop.
Save oceanapplications/2edf349178813e399dfb3d68c7e22757 to your computer and use it in GitHub Desktop.
Search engine ranking position tracker build on Laravel Dusk
<?php
namespace Tests\Browser;
use function foo\func;
use Tests\DuskTestCase;
use Laravel\Dusk\Browser;
use Illuminate\Foundation\Testing\DatabaseMigrations;
class SerpTest extends DuskTestCase
{
private $pagePosition = null;
private $searchText = "https://github.com/laravel/framework";
private $searchTerm = "laravel";
/**
* A basic browser test example.
*
* @return void
*/
public function testBasicExample()
{
$this->browse(function (Browser $browser) {
//make search
$browser->visit('http://google.com/')
->value('#lst-ib', $this->searchTerm)
->keys('input[value="Google Search"]', '{enter}')
->pause(500);
$currentPage = 1;
$result = "Content Not Found";
do{
if ($this->checkPage($browser, $this->searchText)){
$position = $this->getPosition($browser, $this->searchText);
$result = "Found Link on page ".$currentPage." in position ".$this->pagePosition;
break;
} else {
$currentPage++;
$browser->click("#pnnext")
->pause(500);
}
} while($currentPage < 3);
dump($result);
});
}
private function checkPage($browser, $content){
try {
$browser->assertSee($content);
return true;
} catch (\Exception $e){
return false;
}
}
private function checkIn($browser, $content, $position){
try {
$browser->assertSeeIn('.g:nth-of-type('.$position.'n)',$content);
return true;
} catch (\Exception $e){
return false;
}
}
private function getPosition($browser, $content){
return $browser->with('.srg', function ($table) use ($content){
for ($position=0; $position < 10; $position++){
if($this->checkIn($table, $content, $position))
{
$this->pagePosition = $position;
}
}
return false;
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment