<?php
require 'vendor/autoload.php';
 
use PHPUnit\Framework\TestCase;
use Facebook\WebDriver\Remote\DesiredCapabilities;
use Facebook\WebDriver\Remote\RemoteWebDriver;
use Facebook\WebDriver\WebDriverKeys;
use Facebook\WebDriver\WebDriverBy;
 
$GLOBALS['LT_USERNAME'] = "user-name";
# accessKey:  AccessKey can be generated from automation dashboard or profile section
$GLOBALS['LT_APPKEY'] = "access-key";
 
class JS_ExecuteScriptTest extends TestCase
{
  protected $webDriver;
 
  public function build_browser_capabilities(){
    /* $capabilities = DesiredCapabilities::chrome(); */
    $capabilities = array(
      "build" => "[PHP-2] Use executeScript instead of traditional Selenium WebDriver APIs",
      "name" => "[PHP-2] Use executeScript instead of traditional Selenium WebDriver APIs",
      "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();
    /* $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_Wait_Sleep()
  {
  $test_url = "https://lambdatest.github.io/sample-todo-app/";
  $title = "Sample page - lambdatest.com";
  $itemName = 'Yey, Lets add it to list';
  
  $driver = $this->webDriver;
  $driver->get($test_url);
  $driver->manage()->window()->maximize();
  
  $elementli1 = $driver->findElements(WebDriverBy::name("li1"));
  $driver->executeScript('arguments[0].click();',$elementli1);
  
  $elementli2 = $driver->findElements(WebDriverBy::name("li2"));
  $driver->executeScript('arguments[0].click();',$elementli2);
 
  $elementtodotext = $driver->findElement(WebDriverBy::id("sampletodotext"));
  $elementtodotext->sendKeys($itemName);
  
  /* This did not work, hence, we used sendKeys method instead of the executeScript method */
    /*
    $elementtodotext = $driver->findElements(WebDriverBy::id("sampletodotext"));
    $new_item_link = "arguments[0].value='"  .$itemName. "';";
    $driver->executeScript($new_item_link,$elementtodotext);
  */
 
  sleep(2);
  
  $addbutton = $driver->findElements(WebDriverBy::id("addbutton"));
  $driver->executeScript('arguments[0].click();',$addbutton);
  
  $driver->wait(10, 500)->until(function($driver) {
          $elements = $driver->findElements(WebDriverBy::cssSelector("[class='list-unstyled'] li:nth-child(6) span"));
          echo "\n New entry count " . count($elements);
          $this->assertEquals(1, count($elements));
          return count($elements) > 0;
    }
  );
  }
}
?>