<commit_before><?php

namespace Acme\Bundle\TestsBundle\Pages;

use PHPUnit_Framework_Assert;

class PageGrid extends Page
{

    protected $gridPath = '';

    protected $filtersPath = '';

    /**
     * Select random entity from current page
     *
     * @param int $pageSize
     * @return array
     */
    public function getRandomEntity($pageSize = 10)
    {
        $entityId = rand(1, $pageSize);

        $entity = $this->elements($this->using('xpath')->value("{$this->gridPath}//table/tbody/tr[{$entityId}]/td"));
        $headers = $this->elements($this->using('xpath')->value("{$this->gridPath}//table/thead/tr/th"));

        $entityData = array();
        for ($i=0; $i< count($headers); $i++) {
            $entityData[$headers[$i]->text()] = $entity[$i]->text();
        }
        return $entityData;
    }

    /**
     * Change current grid page
     *
     * @param int $page
     * @return $this
     */
    public function changePage($page = 1)
    {
        $pager = $this->byXPath("{$this->filtersPath}//div[contains(@class,'pagination')]/ul//input");
        //set focus
        $pager->click();
        //clear field
        $this->clearInput($pager);
        $pager->value($page);
        $this->keysSpecial('enter');
        $this->waitForAjax();
        return $this;
    }

    /**
     * Navigate to the next page
     *
     * @return $this
     */
    public function nextPage()
    {
        $this->byXPath("{$this->gridPath}//div[contains(@class,'pagination')]//a[contains(.,'Next')]")->click();
        $this->waitForAjax();
        return $this;
    }

    /**
     * Navigate to the previous page
     *
     * @return $this
     */
    public function previousPage()
    {
        $this->byXPath("{$this->gridPath}//div[contains(@class,'pagination')]//a[contains(.,'Prev')]")->click();
        $this->waitForAjax();
    }

    /**
     * Get the current page number
     *
     * @return int
     */
    public function getCurrentPageNumber()
    {
        return intval($this->byXPath("{$this->gridPath}//div[contains(@class,'pagination')]/ul//input")->value());
    }

    /**
     * Get pages count by parsing text label
     *
     * @return int
     */
    public function getPagesCount()
    {
        $pager = $this->byXPath("{$this->gridPath}//div[contains(@class,'pagination')]//label[@class='dib'][2]")->text();
        preg_match('/of\s+(\d+)\s+\|\s+(\d+)\s+records/i', $pager, $result);
        return intval($result[1]);
    }

    /**
     * Get records count in grid by parsing text label
     *
     * @return int
     */
    public function getRowsCount()
    {
        $pager = $this->byXPath("{$this->gridPath}//div[contains(@class,'pagination')]//label[@class='dib'][2]")->text();
        preg_match('/of\s+(\d+)\s+\|\s+(\d+)\s+records/i', $pager, $result);
        return intval($result[2]);
    }

    /**
     * Get all elements from data grid
     *
     * @param null|int $id
     * @return array PHPUnit_Extensions_Selenium2TestCase_Element
     */
    public function getRows($id = null)
    {
        if (is_null($id)) {
            $records = $this->elements($this->using('xpath')->value("{$this->gridPath}//table/tbody/tr"));
        } else {
            $records = $this->elements($this->using('xpath')->value("{$this->gridPath}//table/tbody/tr[{$id}]"));
        }

        return $records;
    }

    /**
     * Verify entity exist on the current page
     *
     * @param array $entityData
     * @return bool
     */
    public function entityExists($entityData)
    {
        $xpath = '';
        foreach ($entityData as $entityField) {
            if ($xpath != '') {
                $xpath .= " and ";
            }
            $xpath .=  "td[contains(.,'{$entityField}')]";
        }
        $xpath = "{$this->gridPath}//table/tbody/tr[{$xpath}]";
        return $this->isElementPresent($xpath);
    }

    /**
     * Verify entity exist on the current page
     *
     * @param array $entityData
     * @return bool
     */
    public function getEntity($entityData)
    {
        $xpath = '';
        foreach ($entityData as $entityField) {
            if ($xpath != '') {
                $xpath .= " and ";
            }
            $xpath .=  "td[contains(.,'{$entityField}')]";
        }
        $xpath = "{$this->gridPath}//table/tbody/tr[{$xpath}]";
        return $this->byXPath($xpath);
    }

    public function deleteEntity($entityData = array())
    {
        $entity = $this->getEntity($entityData);
        $entity->element($this->using('xpath')->value("td[@class = 'action-cell']//a[contains(., '...')]"))->click();
        $entity->element($this->using('xpath')->value("td[@class = 'action-cell']//a[contains(., 'Delete')]"))->click();
        $this->byXPath("//div[div[contains(., 'Delete Confirmation')]]//a[text()='OK']")->click();

        $this->waitPageToLoad();
        $this->waitForAjax();
        return $this;
    }

    /**
     * Get grid headers
     *
     * @return array PHPUnit_Extensions_Selenium2TestCase_Element
     */
    public function getHeaders()
    {
        $records = $this->elements($this->using('xpath')->value("{$this->gridPath}//table/thead/tr/th"));
        return $records;
    }
<commit_msg>BAP-570 Refactor Selenium Tests to use PageObject  - fixed confirmation message<commit_after><?php

namespace Acme\Bundle\TestsBundle\Pages;

use PHPUnit_Framework_Assert;

class PageGrid extends Page
{

    protected $gridPath = '';

    protected $filtersPath = '';

    /**
     * Select random entity from current page
     *
     * @param int $pageSize
     * @return array
     */
    public function getRandomEntity($pageSize = 10)
    {
        $entityId = rand(1, $pageSize);

        $entity = $this->elements($this->using('xpath')->value("{$this->gridPath}//table/tbody/tr[{$entityId}]/td"));
        $headers = $this->elements($this->using('xpath')->value("{$this->gridPath}//table/thead/tr/th"));

        $entityData = array();
        for ($i=0; $i< count($headers); $i++) {
            $entityData[$headers[$i]->text()] = $entity[$i]->text();
        }
        return $entityData;
    }

    /**
     * Change current grid page
     *
     * @param int $page
     * @return $this
     */
    public function changePage($page = 1)
    {
        $pager = $this->byXPath("{$this->filtersPath}//div[contains(@class,'pagination')]/ul//input");
        //set focus
        $pager->click();
        //clear field
        $this->clearInput($pager);
        $pager->value($page);
        $this->keysSpecial('enter');
        $this->waitForAjax();
        return $this;
    }

    /**
     * Navigate to the next page
     *
     * @return $this
     */
    public function nextPage()
    {
        $this->byXPath("{$this->gridPath}//div[contains(@class,'pagination')]//a[contains(.,'Next')]")->click();
        $this->waitForAjax();
        return $this;
    }

    /**
     * Navigate to the previous page
     *
     * @return $this
     */
    public function previousPage()
    {
        $this->byXPath("{$this->gridPath}//div[contains(@class,'pagination')]//a[contains(.,'Prev')]")->click();
        $this->waitForAjax();
    }

    /**
     * Get the current page number
     *
     * @return int
     */
    public function getCurrentPageNumber()
    {
        return intval($this->byXPath("{$this->gridPath}//div[contains(@class,'pagination')]/ul//input")->value());
    }

    /**
     * Get pages count by parsing text label
     *
     * @return int
     */
    public function getPagesCount()
    {
        $pager = $this->byXPath("{$this->gridPath}//div[contains(@class,'pagination')]//label[@class='dib'][2]")->text();
        preg_match('/of\s+(\d+)\s+\|\s+(\d+)\s+records/i', $pager, $result);
        return intval($result[1]);
    }

    /**
     * Get records count in grid by parsing text label
     *
     * @return int
     */
    public function getRowsCount()
    {
        $pager = $this->byXPath("{$this->gridPath}//div[contains(@class,'pagination')]//label[@class='dib'][2]")->text();
        preg_match('/of\s+(\d+)\s+\|\s+(\d+)\s+records/i', $pager, $result);
        return intval($result[2]);
    }

    /**
     * Get all elements from data grid
     *
     * @param null|int $id
     * @return array PHPUnit_Extensions_Selenium2TestCase_Element
     */
    public function getRows($id = null)
    {
        if (is_null($id)) {
            $records = $this->elements($this->using('xpath')->value("{$this->gridPath}//table/tbody/tr"));
        } else {
            $records = $this->elements($this->using('xpath')->value("{$this->gridPath}//table/tbody/tr[{$id}]"));
        }

        return $records;
    }

    /**
     * Verify entity exist on the current page
     *
     * @param array $entityData
     * @return bool
     */
    public function entityExists($entityData)
    {
        $xpath = '';
        foreach ($entityData as $entityField) {
            if ($xpath != '') {
                $xpath .= " and ";
            }
            $xpath .=  "td[contains(.,'{$entityField}')]";
        }
        $xpath = "{$this->gridPath}//table/tbody/tr[{$xpath}]";
        return $this->isElementPresent($xpath);
    }

    /**
     * Verify entity exist on the current page
     *
     * @param array $entityData
     * @return bool
     */
    public function getEntity($entityData)
    {
        $xpath = '';
        foreach ($entityData as $entityField) {
            if ($xpath != '') {
                $xpath .= " and ";
            }
            $xpath .=  "td[contains(.,'{$entityField}')]";
        }
        $xpath = "{$this->gridPath}//table/tbody/tr[{$xpath}]";
        return $this->byXPath($xpath);
    }

    public function deleteEntity($entityData = array())
    {
        $entity = $this->getEntity($entityData);
        $entity->element($this->using('xpath')->value("td[@class = 'action-cell']//a[contains(., '...')]"))->click();
        $entity->element($this->using('xpath')->value("td[@class = 'action-cell']//a[contains(., 'Delete')]"))->click();
        $this->byXPath("//div[div[contains(., 'Delete Confirmation')]]//a[text()='Yes, Delete']")->click();

        $this->waitPageToLoad();
        $this->waitForAjax();
        return $this;
    }

    /**
     * Get grid headers
     *
     * @return array PHPUnit_Extensions_Selenium2TestCase_Element
     */
    public function getHeaders()
    {
        $records = $this->elements($this->using('xpath')->value("{$this->gridPath}//table/thead/tr/th"));
        return $records;
    }

{'fraction_non_alphanumeric': 0.19771971496437055, 'fraction_numerical': 0.0023752969121140144, 'mean_word_length': 2.2557995669656665, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 129, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3740478', 'n_tokens_mistral': 3347, 'n_tokens_neox': 3305, 'n_words': 544}