Completed
Push — master ( 4a8070...c4a530 )
by Paweł
22s
created

waitForAsynchronousActionsToFinish()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 4
rs 10
c 1
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sylius\Behat\Service;
13
14
use Behat\Mink\Driver\Selenium2Driver;
15
use Behat\Mink\Element\NodeElement;
16
use Behat\Mink\Session;
17
use Webmozart\Assert\Assert;
18
19
/**
20
 * @author Kamil Kokot <[email protected]>
21
 */
22
abstract class AutocompleteHelper
23
{
24
    /**
25
     * @param Session $session
26
     * @param NodeElement $element
27
     * @param string $value
28
     */
29
    public static function chooseValue(Session $session, NodeElement $element, $value)
30
    {
31
        Assert::isInstanceOf($session->getDriver(), Selenium2Driver::class);
32
33
        static::activateAutocompleteDropdown($session, $element);
34
35
        $element->find('css', sprintf('div.item:contains("%s")', $value))->click();
36
37
        static::waitForElementToBeVisible($session, $element);
38
    }
39
40
    /**
41
     * @param Session $session
42
     * @param NodeElement $element
43
     * @param string[] $values
44
     */
45
    public static function chooseValues(Session $session, NodeElement $element, array $values)
46
    {
47
        Assert::isInstanceOf($session->getDriver(), Selenium2Driver::class);
48
49
        static::activateAutocompleteDropdown($session, $element);
50
51
        foreach ($values as $value) {
52
            $element->find('css', sprintf('div.item:contains("%s")', $value))->click();
53
54
            JQueryHelper::waitForAsynchronousActionsToFinish($session);
55
        }
56
57
        static::waitForElementToBeVisible($session, $element);
58
    }
59
60
    /**
61
     * @param Session $session
62
     * @param NodeElement $element
63
     */
64
    private static function activateAutocompleteDropdown(Session $session, NodeElement $element)
65
    {
66
        JQueryHelper::waitForAsynchronousActionsToFinish($session);
67
68
        $element->click();
69
70
        JQueryHelper::waitForAsynchronousActionsToFinish($session);
71
        static::waitForElementToBeVisible($session, $element);
72
    }
73
74
    /**
75
     * @param Session $session
76
     * @param NodeElement $element
77
     */
78
    private static function waitForElementToBeVisible(Session $session, NodeElement $element)
79
    {
80
        $session->wait(5000, sprintf(
81
            '$(document.evaluate("%s", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue).dropdown("is visible")',
82
            $element->getXpath()
83
        ));
84
    }
85
}
86