PHPWebDriver: Examples for Drupal Controls

 

<?php declare(strict_types=1);
namespace Pom\Utils;

use Facebook\WebDriver\Remote\RemoteWebDriver;
use Facebook\WebDriver\WebDriverBy;
use Facebook\WebDriver\WebDriverElement;
use Facebook\WebDriver\Remote\LocalFileDetector;
use Facebook\WebDriver\WebDriverSelect;
use Facebook\WebDriver\Remote\RemoteWebElement;
use phpDocumentor\Reflection\Types\Boolean;

class PhpWebDriverExamples
{
    protected $driver;
    protected $el;
    protected $els;
    protected $internalBool;
    
    /**
     * @return \phpDocumentor\Reflection\Types\Boolean
     */
    public function getInternalBool()
    {
        return $this->internalBool;
    }

    /**
     * @param \phpDocumentor\Reflection\Types\Boolean $internalBool
     */
    public function setInternalBool($internalBool)
    {
        $this->internalBool = $internalBool;
    }

    
    
    public function __construct(RemoteWebDriver $driver){
        $this->driver = $driver;
        $this->internalBool = new Boolean();
        
        $isThisRightOrWrong = "I have no idea";
        $a = "hello";
        $b = 1;
        $c = $a + $b;
    }
    
    /**
     * @param String $findBy
     * @param string $locator
     * @return WebDriverElement
     */
    public function getWebElement(String $findBy, string $locator):WebDriverElement
    {
        switch($type)
        {
            case "xpath":
                $by = WebDriverBy::xpath($locator);
                break;
            case "cssSelector":
                $by = WebDriverBy::cssSelector($locator);
                break;
            case "name":
                $by = WebDriverBy::name($locator);
                break;
            case "id":
                $by = WebDriverBy::id($locator);
                break;
            case "linktext":
                $by = WebDriverBy::linkText($locator);
                break;
            case "partiallinktext":
                $by = WebDriverBy::partialLinkText($locator);
                break;
            case "tagname":
                $by = WebDriverBy::tagName($locator);
                break;
            case "classname":
                $by = WebDriverBy::className($locator);
                break;
            default:
                break;
        }
        $el = $this->driver->findElement($by);
        if ($el != null){
            return $el;
        } else return null;
    }
    
    /**
     * @param string $type
     * @param string $locator
     */
    public function getWebElements(string $type, string $locator){
        switch($type)
        {
            case "xpath":
                $by = WebDriverBy::xpath($locator);
                break;
            case "cssSelector":
                $by = WebDriverBy::cssSelector($locator);
                break;
            case "name":
                $by = WebDriverBy::name($locator);
                break;
            case "id":
                $by = WebDriverBy::id($locator);
                break;
            case "linktext":
                $by = WebDriverBy::linkText($locator);
                break;
            case "partiallinktext":
                $by = WebDriverBy::partialLinkText($locator);
                break;
            case "tagname":
                $by = WebDriverBy::tagName($locator);
                break;
            case "classname":
                $by = WebDriverBy::className($locator);
                break;
            default:
                break;
        }
        $els = $this->driver->findElements($by);
    }
    
    private function get_InputTextBox():WebDriverElement{
        return $this->getWebElement('xpath', '//input[starts-with[@id,"")]');
    }
    /**
     * @param string $text
     */
    public function type_InputTextBox(string $text){
        $this->get_InputTextBox()->sendKeys($text);
    }
    
    private function get_TextArea():WebDriverElement{
        return $this->getWebElement('xpath', '//textarea[starts-with[@id,"")]');
    }
    public function type_TextArea(string $text){
        $this->get_TextArea()->sendKeys($text);
    }
    
    private function get_InputButton():WebDriverElement{
        return $this->getWebElement('xpath', '//input[starts-with[@id,"")]');
        
    }
    public function click_InputButton(){
        $this->get_InputButton()->click();
    }
    
    private function get_Button():WebDriverElement{
        return $this->getWebElement('xpath', '//button[starts-with[@id,"")]');
    }
    public function click_Button(){
        $this->get_Button()->click();
    }
    public function click_Button(){
        $this->get_Button()->click();
    }
    
    private function get_IFrame():WebDriverElement{
        return $this->getWebElement('xpath', '//iframe[starts-with[@id,"")]');
    }

    
    private function type_Body(string $bodyText):WebDriverElement{
        $this->driver->switchTo()->frame($this->get_IFrame());
        $this->driver->executeScript("document.getElementByTagName('body)[0].innerText = '" . $bodyText . "';");
        $this->driver->switchTo()->defaultContent();
    }
    public function click_Button(){
        $this->get_Button()->click();
    }
    
    private function get_Checkbox():WebDriverElement{
        return $this->getWebElement('xpath', '//input[starts-with[@id,"")]');
    }
    public function click_Checkbox(bool $enabled){
        if($this->get_Checkbox()->isSelected() == !$enabled){
            $this->get_Checkbox()->click();
        }
        $this->get_Checkbox()->click();
    }
    
    public function type_FileName():WebDriverElement{
        $fileInput = $this->driver->findElement(WebDriverBy::id(''));
        $fileInput->setFileDetector(new LocalFileDetector());
        $fileInput->sendKeys('T:\filename');
    }
    
    private function get_MultipleField(int $num):WebDriverElement{
        return $this->getWebElement('xpath', '//input[starts-with[@id,"'.$num.'")]');
    }
    public function type_MultipleField(int $num, string $text){
        $this->get_MultipleField($num)->clear()->sendKeys($text);
    }

    private function get_ExpandCollapse():WebDriverElement{
        return $this->getWebElement('xpath', '//input[starts-with[@id,"")]');
    }
    public function click_ExpandCollapse(int $num, string $text){
        if(($this->get_ExpandCollapse()->getAttribute("aria-expanded") =="false") && ($expanded))
        {
            $this->get_ExpandCollapse()->click();
        }
    }
    
    private function get_Select():WebDriverElement{
        return $this->getWebElement('xpath', '//input[starts-with[@id,"")]');
    }
    public function select_Select(string $option){
        try{
            $select = new WebDriverSelect($this->get_Select());
            $select->selectByVisibleText($option);
        } catch (\Exception $e ){
            
        }
    }

    
//     private function get_SelectOptions():WebDriverElement{}
//     private function get_SelectText():WebDriverElement{}
    
    public function ScrollToTop():WebDriverElement{
        $this->driver->executeScript('window.scrollTo(0,document.body.scrollTop);');

    }
     private function ScrollToBottom():WebDriverElement{
         $this->driver->executeScript("window.scrollTo(0,document.body.scrollHeight);");
     }
     
//     private function actionScrollToElement():WebDriverElement{}
   
}