<?php
require 'vendor/autoload.php';
 
use PHPUnit\Framework\TestCase;
use Facebook\WebDriver\Remote\DesiredCapabilities;
use Facebook\WebDriver\Remote\RemoteWebDriver;
use Facebook\WebDriver\WebDriverBy;
use Facebook\WebDriver\WebDriverExpectedCondition;
 
$GLOBALS['LT_USERNAME'] = "user-name";
# accessKey:  AccessKey can be generated from automation dashboard or profile section
$GLOBALS['LT_APPKEY'] = "access-key";
$GLOBALS['ELEM_STATUS'] = False;
 
class Custom_Explicit_WaitTest extends TestCase
{
  protected $webDriver;
 
  public function build_browser_capabilities(){
    /* Local Selenium Grid */
    /* $capabilities = DesiredCapabilities::chrome(); */
    $capabilities = array(
      "build" => "[PHP] Demonstration of Explicit Wait with custom conditions",
      "name" => "[PHP] Demonstration of Explicit Wait with custom conditions",
      "platform" => "Windows 10",
      "browserName" => "Chrome",
      "version" => "85.0"
    );
    return $capabilities;
  }
 
  public function setUp(): void
  {
    $url = "https://". $GLOBALS['LT_USERNAME'] .":" . $GLOBALS['LT_APPKEY'] ."@hub.lambdatest.com/wd/hub";
    $capabilities = $this->build_browser_capabilities();
    /* Local Selenium Grid */
    /* $this->webDriver = RemoteWebDriver::create('http://localhost:4444/wd/hub', $capabilities); */
    $this->webDriver = RemoteWebDriver::create($url, $capabilities);
  }
 
  public function tearDown(): void
  {
    $this->webDriver->quit();
  }
  /*
  * @test
  */ 
  public function test_Custom_Explicit_Wait()
  {
    $test_url = "https://phptravels.com/demo/";
    $title = "Demo Script Test drive - PHPTRAVELS";
    $resultant_play_url = "https://play.google.com/store/apps/details?id=com.phptravelsnative";
    $title_2_str = "PHPTRAVELS Native";
    $gplay_xpath = "//p[.='Get it on  Google Play']";
 
    $this->webDriver->get($test_url);
    $this->webDriver->manage()->window()->maximize();
 
    $HandleCount = $this->webDriver->getWindowHandles();
    echo ("\nTotal number of window handles are " . sizeof($HandleCount));
    echo ("\nWindow 0: " . $HandleCount[0]);
 
    $win_title = $this->webDriver->getTitle();
    echo ("\nTitle of the window 0 is " . $win_title);
    $this->assertEquals($win_title, $title);
    
    $driver = $this->webDriver;
    $this->webDriver->wait(10, 500)->until(
    function () use ($driver) {
        $gplay_xpath = "//p[.='Get it on  Google Play']";
        $link = "window.scrollTo(0, 800)";
        $driver->executeScript($link);
        
        $elem_play_button_cnt = $driver->findElements(WebDriverBy::XPath($gplay_xpath));
        if ($elem_play_button_cnt > 0)
        {
            echo("\nGoogle Play Element is located\n");
            $GLOBALS['ELEM_STATUS'] = True;
        }
        return count($elem_play_button_cnt) > 0;
    },
    'Error locating the Google Play button'
    );
    
    if ($GLOBALS['ELEM_STATUS'] == False)
    {
        /* Close the newly opened Window and return to the old window */
        $this->webDriver->close();
    }
    else
    {
        $element = $driver->wait(5, 1000)->until(WebDriverExpectedCondition::elementToBeClickable(WebDriverBy::XPath($gplay_xpath)));
        $element->click();
        $HandleCount = $this->webDriver->getWindowHandles();
        echo ("\nTotal number of window handles are " . sizeof($HandleCount));
        echo ("\nWindow 1: " . $HandleCount[1]);
 
        /* Go the window opened in the next tab */
        $this->webDriver->switchTo()->window($HandleCount[1]);
        $win_title_2 = $this->webDriver->getTitle();
        echo ("\nTitle of the window 1 is " . $win_title_2);
 
        /* $this->assertEquals($win_title_2, $title_2); */
        if(strpos($win_title_2, $title_2_str) !== false)
        {
            echo "\nPlay Store window for PHP Native is open";
        }
        else
        {
            echo "\nPlay Store window for PHP Native is not open";
        }
        /*** If a click has to be performed on the Link that contains text 'Learn More', use the following commands ***/
        $driver->wait(10, 500)->until(
        WebDriverExpectedCondition::presenceOfElementLocated(WebDriverBy::XPath("//a[.='Learn More']"))
        )->click();
 
        /* Check if the Window Title contains the Expected Sub-String */
        echo ("\nTitle of the window 1.1 is " . $this->webDriver->getTitle());
        $driver->wait(10, 500)->until(
        WebDriverExpectedCondition::titleContains("Games content ratings on Google Play")
        );
        /* Close the newly opened Window and return to the old window */
        $this->webDriver->close();
    }
    echo ("\nCustom Explicit Wait test completed\n");
  }
}
?>