Есть интеграционный тест на Selenium + PHPUnit:
<?php
namespace Site\Selenium\Test;
use Facebook\WebDriver\Exception\NoSuchElementException;
use Facebook\WebDriver\Exception\TimeoutException;
use Facebook\WebDriver\Remote\RemoteWebDriver;
use Facebook\WebDriver\WebDriverBy;
use Facebook\WebDriver\WebDriverExpectedCondition;
use phpDocumentor\Reflection\Types\Self_;
use PHPUnit\Framework\TestCase;
use function PHPUnit\Framework\assertTrue;
class NewsFiltersTest extends TestCase
{
use WebDriverTrait;
use SiteTrait;
/**
* Проверка работы фильтров на странице "Новости"
*
* @return void
* @throws NoSuchElementException
* @throws TimeoutException
*/
public function testFilters()
{
$chrome = self::getChrome();
$chrome->manage()->deleteAllCookies();
$news_url = self::getTargetUrl() . '/news/';
$chrome->get($news_url);
$newsHref = self::getNewsItems($chrome);
// Дата
self::openCalendar($chrome);
self::clickToRandomDate($chrome);
$filterNewsHref = self::getNewsItems($chrome);
assertTrue((array_diff($newsHref, $filterNewsHref)) ? true : false, 'Фильтр по дате не сработал');
// Раздел
$chrome->navigate()->to($news_url);
sleep(1);
$sectionSelector = WebDriverBy::xpath("//form[contains(@action, 'news')]//div[contains(@class, 'select-styled')]");
$chrome->wait()->until(WebDriverExpectedCondition::visibilityOfElementLocated($sectionSelector));
$chrome->findElement($sectionSelector)->click();
$sectionItemsSelector = WebDriverBy::xpath("//form[contains(@action, 'news')]//div[contains(@class, 'select-options')]//li");
$chrome->wait()->until(WebDriverExpectedCondition::visibilityOfElementLocated($sectionItemsSelector));
$sectionItems = $chrome->findElements($sectionItemsSelector);
$sectionItems[count($sectionItems) - 1]->click();
$filterNewsHref = self::getNewsItems($chrome);
assertTrue((array_diff($filterNewsHref, $newsHref)) ? true : false, 'Фильтр по разделу не сработал');
// Рубрика
$chrome->navigate()->to($news_url);
sleep(1);
$typeItemsSelector = WebDriverBy::xpath("//form[contains(@action, 'news')]//div[contains(@class, 'b-checkbox')]//label");
$chrome->wait()->until(WebDriverExpectedCondition::visibilityOfElementLocated($typeItemsSelector));
$typeItems = $chrome->findElements($typeItemsSelector);
$typeItems[rand(0, count($typeItems) - 1)]->click();
$filterNewsHref = self::getNewsItems($chrome);
assertTrue((array_diff($filterNewsHref, $newsHref)) ? true : false, 'Фильтр по рубрике не сработал');
}
private static function getNewsItems(RemoteWebDriver $chrome): array
{
$newsHref = [];
try {
$newsItemsSelector = WebDriverBy::xpath("//a[contains(@class, 'news-item')]");
$newsItems = $chrome->findElements($newsItemsSelector);
while (count($newsItems) === 0) {
self::openCalendar($chrome);
self::openPrevMount($chrome);
self::clickToRandomDate($chrome);
$newsItems = $chrome->findElements($newsItemsSelector);
}
foreach ($newsItems as $frch_item) {
$newsHref[] = $frch_item->getAttribute('href');
}
}
catch (NoSuchElementException $e) {
$newsHref[] = 'empty';
}
return $newsHref;
}
private static function openCalendar($chrome) {
$dateFromSelector = WebDriverBy::xpath("//form[contains(@action, 'news')]//div[contains(@class, 'date from')]");
$chrome->wait()->until(WebDriverExpectedCondition::visibilityOfElementLocated($dateFromSelector));
$chrome->findElement($dateFromSelector)->click();
}
private static function openPrevMount($chrome) {
$prevCalendarArrow = $chrome->findElement(WebDriverBy::className('bx-calendar-left-arrow'));
$prevCalendarArrow->click();
sleep(1);
}
private static function clickToRandomDate($chrome) {
$calendarItemsSelector = WebDriverBy::xpath("//div[contains(@class, 'bx-calendar-layer')]//a[not(contains(@class, 'bx-calendar-date-hidden'))]");
$chrome->wait()->until(WebDriverExpectedCondition::visibilityOfElementLocated($calendarItemsSelector));
$calendarItems = $chrome->findElements($calendarItemsSelector);
$calendarItems[rand(0, count($calendarItems) - 1)]->click();
}
}
Если запускать только этот тест, то всё ok, он прекрасно работает, но вот если запустить все тесты, то он выдаёт ошибку:
1) Site\Selenium\Test\NewsFiltersTest::testFilters
Facebook\WebDriver\Exception\WebDriverCurlException: Curl error thrown for http POST to /session/43170fb6bd154f91bc8fb7c3d2694345/elements with params: {"using":"xpath","value":"//a[contains(@class, 'news-item')]"}
Operation timed out after 120001 milliseconds with 0 bytes received
/app/vendor/php-webdriver/webdriver/lib/Remote/HttpCommandExecutor.php:333
/app/vendor/php-webdriver/webdriver/lib/Remote/RemoteWebDriver.php:585
/app/vendor/php-webdriver/webdriver/lib/Remote/RemoteWebDriver.php:225
/app/tests/NewsFiltersTest.php:77
/app/tests/NewsFiltersTest.php:42
phpvfscomposer:///app/vendor/phpunit/phpunit/phpunit:97
Элемента селектор котого фигурирует в тексте ошибки:
$newsItemsSelector = WebDriverBy::xpath("//a[contains(@class, 'news-item')]");
На странице может и не быть и тогда сработает цикл while, который будет работать пока не найдёт страницу на который элементы по селектору есть в наличии:
$newsItemsSelector = WebDriverBy::xpath("//a[contains(@class, 'news-item')]");
$newsItems = $chrome->findElements($newsItemsSelector);
while (count($newsItems) === 0) {
self::openCalendar($chrome);
self::openPrevMount($chrome);
self::clickToRandomDate($chrome);
$newsItems = $chrome->findElements($newsItemsSelector);
}
Подскажите в чём дело?!? Почему этот код работает если запускать тест в одиночку и не работает при общем запуске тестов?!?