Working with multiple frames in Selenium in php

Below example explains how to switch to frame in Selenium in PHP. In below example, we are switching to frame with name “g” and grabbing the portion of page source inside that frame. Then we have switched to main window document using defaultContent() method.
 
<?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 testFrames()
{
$this->webDriver->get(“https://www.softpost.org/selenium-test-page”);

//Switch to frame with name “g”
$this->webDriver->switchTo()->frame(“g”);

echo (“
Portion of HTML Source of Frame -> ” . substr($this->webDriver->getPageSource(), 0, 300));

echo (“

********************************************************************
”);
$this->webDriver->switchTo()->defaultContent();

echo (“
Portion of HTML Source of Main window -> ” . substr($this->webDriver->getPageSource(), 0, 300));

}

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

Complex problems, Simple Solutions