161 lines
6.7 KiB
PHP
161 lines
6.7 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace LoretaAudit\Tests\DataSetup\Traits\App\Helpers;
|
|
|
|
use LoretaAudit\Tests\DataSetup\Constants\siteLanguages;
|
|
|
|
trait assertionsTrait
|
|
{
|
|
private $baseTableData = '#base_table > div.table-data';
|
|
private $colHeaderSelector = '#cms-table-drop-marker ul.thead';
|
|
private $tableBodyContainer = '#cms-table-drop-marker .table-rows .tbody';
|
|
private $tablePagerContainer = "//ul[@class='dx pager-cnt']";
|
|
|
|
public function assertStringContains(string $haystack, string $needle): void
|
|
{
|
|
$this->assertTrue(strpos($haystack, $needle) !== false);
|
|
}
|
|
|
|
public function assertTableTitle(string $expectedTitle, ?string $expectedSubTitle = null): void
|
|
{
|
|
$sel = "$this->baseTableData li.breadcrumb-item.active";
|
|
$this->elementWait($sel, self::CSS_SELECTOR);
|
|
$this->assertElementTextContainsByCss($sel, $expectedTitle, "On selector: $sel");
|
|
|
|
if ($expectedSubTitle) {
|
|
$this->assertElementTextContainsByCss("$this->baseTableData li:nth-child(3)", $expectedSubTitle);
|
|
}
|
|
}
|
|
|
|
public function assertFormTitle(string $expectedTitle): void
|
|
{
|
|
$sel = '.form-data li.breadcrumb-item.active';
|
|
$this->elementWait($sel, self::CSS_SELECTOR);
|
|
$this->assertElementTextContainsByCss($sel, $expectedTitle);
|
|
}
|
|
|
|
public function assertElementValueById(string $id, string $expectedValue, string $message = ''): void
|
|
{
|
|
$this->assertSame($expectedValue, $this->byId($id)->getAttribute('value'), $message);
|
|
}
|
|
|
|
public function assertElementValueByCss(string $selector, string $expectedText, string $message = ''): void
|
|
{
|
|
$this->assertSame($expectedText, $this->byCssSelector($selector)->getAttribute('value'), $message);
|
|
}
|
|
|
|
public function assertElementTextByCss(string $selector, string $expectedText, string $message = ''): void
|
|
{
|
|
$this->assertSame($expectedText, $this->byCssSelector($selector)->getText(), $message);
|
|
}
|
|
|
|
public function assertElementTextContainsByCss(string $selector, string $expectedText, string $message = ''): void
|
|
{
|
|
$this->assertStringContainsString($expectedText, $this->byCssSelector($selector)->getText(), $message);
|
|
}
|
|
|
|
public function assertFieldError(string $fieldId, string $expectedErrorMessage): void
|
|
{
|
|
// Wait for the error class to be set on input
|
|
$this->elementWait("div.validate.v-error #$fieldId", self::CSS_SELECTOR);
|
|
// Hover the element
|
|
$this->webDriver->action()->moveToElement($this->byId($fieldId))->perform();
|
|
// Wait for the error bubble
|
|
$this->elementWait("#$fieldId + div.errMsg", self::CSS_SELECTOR);
|
|
// Get an error message from the error bubble
|
|
$this->assertEquals($expectedErrorMessage, $this->byCssSelector("#$fieldId + div.errMsg")->getText());
|
|
}
|
|
|
|
public function assertCmsCheckboxStateById(string $id, bool $isChecked = true, string $message = ''): void
|
|
{
|
|
$exists = $this->elementExists("#$id.chk.chked", self::CSS_SELECTOR, true);
|
|
$this->assertSame($isChecked, $exists, $message);
|
|
}
|
|
|
|
public function assertCmsCheckboxState(string $selector, bool $shouldBeChecked = true, string $message = ''): void
|
|
{
|
|
$exists = $this->elementExists($selector . ' .chk.chked', self::CSS_SELECTOR, true);
|
|
$this->assertSame($shouldBeChecked, $exists, $message);
|
|
}
|
|
|
|
public function assertCmsTableNoResults(string $lang): void
|
|
{
|
|
switch ($lang) {
|
|
case siteLanguages::LANG_LV:
|
|
$msg = 'Nav ierakstu, lūdzu pārbaudiet filtru un mēģiniet vēlreiz!';
|
|
break;
|
|
case siteLanguages::LANG_RU:
|
|
$msg = 'Результаты не найдены данные, пожалуйста, проверьте фильтр и попробуйте еще раз!';
|
|
break;
|
|
case siteLanguages::LANG_EN:
|
|
default:
|
|
$msg = 'No result data found, please check your filter and try again!';
|
|
}
|
|
$this->assertElementTextContainsByCss($this->tableBodyContainer . ' > div', $msg);
|
|
}
|
|
|
|
public function assertCmsTableRow(int $row, array $values, bool $scrollIntoView = false): void
|
|
{
|
|
$rowSel = $this->tableBodyContainer . " > ul:nth-child($row)";
|
|
|
|
if ($scrollIntoView) {
|
|
$this->scrollElementIntoView($rowSel, self::CSS_SELECTOR);
|
|
}
|
|
|
|
$col = 3;
|
|
foreach ($values as $value) {
|
|
if (is_string($value) || is_int($value)) {
|
|
$this->assertCmsTableRowData($row, $col, $value);
|
|
} else {
|
|
$this->assertCmsCheckboxState("$rowSel > li:nth-child($col)", $value);
|
|
}
|
|
|
|
$col++;
|
|
}
|
|
}
|
|
|
|
public function assertCmsTableRowData($row, $column, $expectedText): void
|
|
{
|
|
$selector = $this->tableBodyContainer . " > ul:nth-child($row) > li:nth-child($column) > div";
|
|
$exists = $this->elementExists($selector, self::CSS_SELECTOR, true);
|
|
if (empty($expectedText) && !$exists) {
|
|
$selector = $this->tableBodyContainer . " > ul:nth-child($row) > li:nth-child($column)";
|
|
}
|
|
$errorMessage = "Table record with text '$expectedText' not found in column $column, selector: $selector";
|
|
$this->assertElementTextByCss($selector, $expectedText, $errorMessage);
|
|
}
|
|
|
|
public function assertFormValueById(string $id, string $expectedValue): void
|
|
{
|
|
if (strpos($id, '_visible_text')) {
|
|
/** Visible text is used to assert the chosen selected value in form. */
|
|
$id = str_replace('_visible_text', '_chosen', $id);
|
|
$field = $this->getFormElementSelector($id) . " > a.chosen-single > span";
|
|
$this->assertElementTextByCss($field, $expectedValue, "Field '$field' text is not '$expectedValue'");
|
|
return;
|
|
}
|
|
|
|
$field = $this->getVisibleFormFieldElement($id);
|
|
$this->assertSame($expectedValue, $field->getAttribute('value'), "Field '$id' text is not '$expectedValue'");
|
|
}
|
|
|
|
private function assertDataRowCountInFormSection(string $settings, int $count = 0)
|
|
{
|
|
$this->assertCount($count, $this->elementsByCssSelector("#$settings > div > div.data > ul > li"));
|
|
}
|
|
|
|
public function assertSaveButton($exists = true): void
|
|
{
|
|
$elementExists = $this->elementExists('.buttons_bottom #fsave', self::CSS_SELECTOR, true);
|
|
$this->assertSame($exists, $elementExists, 'Save button state mismatch');
|
|
}
|
|
|
|
public function assertBackButton($exists = true): void
|
|
{
|
|
$elementExists = $this->elementExists('.buttons_bottom #tback', self::CSS_SELECTOR, true);
|
|
$this->assertSame($exists, $elementExists, 'Back button state mismatch');
|
|
}
|
|
}
|