<?php
namespace Facebook\WebDriver;
use Facebook\WebDriver\Remote\DesiredCapabilities;
use Facebook\WebDriver\Remote\RemoteWebDriver;
use Facebook\WebDriver\Remote\WebDriverCapabilityType;
/**** CONFIG ****/
require 'config.php';
/**** FIN CONFIG ****/
/**** REQUIRE ****/
$current_dir = getcwd();
$current_dir = str_replace("\\", "/", $current_dir);
require $current_dir.'/vendor/autoload.php';
$host = 'http://localhost:4444/wd/hub'; 
putenv("webdriver.chrome.driver=".$current_dir."/requires/chromedriver.exe");
$capabilities = [
    WebDriverCapabilityType::BROWSER_NAME => 'chrome'
];
$driver = RemoteWebDriver::create($host, $capabilities);
function wait($elementBY,$content,$action = null) {
	global $driver;
	$tmpElement = null;
	switch ($elementBY) {
		case 'id':
			$d = $driver->wait()->until(WebDriverExpectedCondition::presenceOfElementLocated(WebDriverBy::id($content)));
		break;
		case 'css':
			$d = $driver->wait()->until(WebDriverExpectedCondition::presenceOfElementLocated(WebDriverBy::cssSelector($content)));
		break;
		case 'xpath':
			$d = $driver->wait()->until(WebDriverExpectedCondition::presenceOfElementLocated(WebDriverBy::xpath($content)));
		break;
		default:
		break;
	}
	if(!is_null($action)) {
		$d->click();
	} else {
		return $d;
	}
}
require 'generate.php';
/**** FIN REQUIRE ****/
/**** LOGIN ****/
$driver->get('https://framalistes.org/sympa/loginrequest');
wait('id','email_login')->sendKeys($email);
wait('id','passwd')->sendKeys($password);
/**** FIN LOGIN ****/
sleep(5);
/**** GENERATE LISTES ****/
for ($i=0; $i < 30; $i++) { 
	$driver->get('https://framalistes.org/sympa/create_list_request');
	wait('id','template.confidential',true);
	wait('id','listname')->sendKeys($nameList.$i);
	$select = new WebDriverSelect($driver->findElement(WebDriverBy::id('topics')));
	$select->selectByValue($arraySelect[array_rand($arraySelect)]);
	wait('id','subject')->sendKeys($subjectTab[array_rand($subjectTab)]);
	wait('id','info')->sendKeys($descriptionTab[array_rand($descriptionTab)]);
	$driver->findElement(WebDriverBy::xpath('//input[@name="action_create_list"]'))->click();
}
/**** FIN GENERATE LISTES ****/
sleep(120);  
/**** EDIT PARAMS LISTES ****/
for ($i=0; $i < 30; $i++) { 
	$driver->get('https://framalistes.org/sympa/edit_list_request/'.$nameList.$i.'/sending');
	$driver->get('https://framalistes.org/sympa/edit_list_request/'.$nameList.$i.'/sending');
	$driver->findElement(WebDriverBy::id('param.custom_subject'))->clear();
	$driver->findElement(WebDriverBy::xpath('//input[@name="action_edit_list"]'))->click();
}
/**** FIN EDIT PARAMS LISTES ****/