Selenium basic automation in php

Below example shows how to perform basic Browser window automation. We are performing below things.
  • Navigating backward and forward
  • Resizing the window
  • Getting page source
  • Getting current page title and url
 
<?php
class MyTest extends PHPUnit_Framework_TestCase {

protected $webDriver;

public function setUp()
{
$capabilities = array(WebDriverCapabilityType::BROWSER_NAME => ‘chrome’);
$this->webDriver = RemoteWebDriver::create(‘https://localhost:4444/wd/hub’, $capabilities);
$this->webDriver->manage()->window()->maximize();
}

public function testBrowser()
{
$this->webDriver->get(“https://www.softpost.org/selenium-test-page”);

//We can use below code to navigate forward and backward.
$this->webDriver->navigate()->back();
$this->webDriver->navigate()->forward();

$this->webDriver->manage()->window()->setSize(new WebDriverDimension(300,300));

echo (“
Current Page Source -> ” . $this->webDriver->getPageSource());

echo (“
Current title -> ” . $this->webDriver->getTitle());

echo (“
Current URL -> ” . $this->webDriver->getCurrentUrl());

}

public function tearDown()
{
//Quit the driver
$this->webDriver->quit();
}
}
?>


Complex problems, Simple Solutions