Selenium Element interaction in php

Below example demonstrates how to interact with web elements using Selenium in PHP.
  • Sending keys
  • Clearing the text box
  • Getting value from text box
  • Selecting value from drop down
  • Checking if element is disabled/enabled
  • Checking if element is selected or not selected
 
<?php
class MyTest extends PHPUnit_Framework_TestCase {

protected $driver;

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

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

$this->driver->findElement(WebDriverBy::id(‘fn’))->sendKeys(‘Shaun’);

//$this->driver->findElement(WebDriverBy::id(‘fn’))->click();

//$this->driver->getKeyboard()->sendKeys(‘Shaun’);
$firstName = $this->driver->findElement(WebDriverBy::id(‘fn’))->getAttribute(‘value’);

$this->assertEquals(‘Shaun’, $firstName);

$this->driver->findElement(WebDriverBy::id(‘fn’))->clear();

$this->driver->findElement(WebDriverBy::id(‘fn’))->sendKeys(‘Hyden’);

$this->driver->findElement(WebDriverBy::xpath(“//input[@value=’QTP’]”))->click();

sleep(1);

//To get value of an attribute of an element, use getAttribute(attributeName) method.
$attribute = $this->driver->findElement(WebDriverBy::linkText(‘Selenium in Java’))->getAttribute(‘href’);
echo(“
 href attribute of Link is ” . $attribute);

//To check if an element is enabled, use isEnabled() method.
$enabled = $this->driver->findElement(WebDriverBy::xpath(“//input[@value=’QTP’]”))->isEnabled();
echo(“
 QTP checkbox is enabled? ” . $enabled);

//To check if an element is enabled, use isSelected() method.
$selected = $this->driver->findElement(WebDriverBy::xpath(“//input[@value=’QTP’]”))->isSelected();
echo(“
 QTP checkbox is selected? ” . $selected);

//To get the text of an element, use getText() method.
$txt = $this->driver->findElement(WebDriverBy::tagName(“p”))->getText();
echo(“
 Text in first paragraph -> ” . $txt);

//To get the text of an element, use getText() method.
$displayed = $this->driver->findElement(WebDriverBy::xpath(“//input[@value=’Sign up’]”))->isDisplayed();
echo(“
 is Sign up button displayed? -> ” . $displayed);

//To get css value of an element , use getCssValue() method.

//To get tag name of an element , use getTagName() method.

$tagName = $this->driver->findElement(WebDriverBy::linkText(‘Selenium in Java’))->getTagName();
echo(“
TagName of Link is ” . $tagName);

//Select city from the dropdown
$this->driver->findElement(WebDriverBy::xpath(“//select//option[text()=’Mumbai’]”))->click();

sleep(2);

//We can also use WebDriverSelect class to work with drop downs
$dropDown = $this->driver->findElement(WebDriverBy::xpath(“//select”));
(new WebDriverSelect($dropDown))->selectByVisibleText(“Pune”);

//You can use below methods of WebDriverSelect class
//isMultiple(), getOptions(), getAllSelectedOptions(), getFirstSelectedOption()
//deselectAll(), selectByIndex(), selectByValue(), selectByVisibleText()
//deselectByIndex(), deselectByValue(), deselectByVisibleText(), escapeQuotes()

echo (“
The selected option is -> ” . (new WebDriverSelect($dropDown))->getFirstSelectedOption()->getText());

sleep(2);
}

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


Complex problems, Simple Solutions