1<?php
2
3namespace ls\tests;
4
5use Facebook\WebDriver\Remote\RemoteWebDriver;
6use Facebook\WebDriver\WebDriverBy;
7use Facebook\WebDriver\Exception\NoSuchElementException;
8
9/**
10 * Subclass of Facebook webdriver.
11 * This class contains helper methods to interact with a LimeSurvey
12 * survey, like filling in questions, going to next question group,
13 * changing language etc.
14 */
15class LimeSurveyWebDriver extends RemoteWebDriver
16{
17    /**
18     * Change language using the <select> element
19     * on survey welcome page.
20     * @param string $newLang Like 'de' or 'en'.
21     * @return void
22     */
23    public function changeLanguageSelect($newLang)
24    {
25        // Try with welcome page select first.
26        $langSelectOption = $this->findElement(
27            WebDriverBy::cssSelector(
28                sprintf(
29                    '#lang option[value="%s"]',
30                    $newLang
31                )
32            )
33        );
34        $langSelectOption->click();
35        $langSubmit = $this->findElement(
36            WebDriverBy::cssSelector('button[value=changelang]')
37        );
38        $langSubmit->click();
39    }
40
41    /**
42     * Change language using links in top-right corner.
43     * @param string $newLang Like 'de' or 'en'.
44     * @return void
45     */
46    public function changeLanguage($newLang)
47    {
48        $langSelect = $this->findElement(
49            WebDriverBy::cssSelector('.form-change-lang')
50        );
51        $langSelect->click();
52        $langSelectLink = $this->findElement(
53            WebDriverBy::cssSelector(
54                sprintf(
55                    '.form-change-lang a[data-limesurvey-lang="%s"]',
56                    $newLang
57                )
58            )
59        );
60        $langSelectLink->click();
61    }
62
63    /**
64     * @param string $sgqa Like 123X345X567
65     * @param string $answer Answer to question.
66     * @return void
67     */
68    public function answerTextQuestion($sgqa, $answer)
69    {
70        $firstQuestion = $this->findElement(
71            WebDriverBy::cssSelector(
72                sprintf(
73                    'textarea[name="%s"], input[name="%s"]',
74                    $sgqa,
75                    $sgqa
76                )
77            )
78        );
79        $firstQuestion->sendKeys($answer);
80    }
81
82    /**
83     * Go to next question/question group.
84     * @return void
85     */
86    public function next()
87    {
88        $nextButton = $this->findElement(WebDriverBy::id('ls-button-submit'));
89        $nextButton->click();
90    }
91
92    /**
93     * Alias for next().
94     */
95    public function submit()
96    {
97        $this->next();
98    }
99
100    /**
101     * @return void
102     */
103    public function clickButton($id)
104    {
105        $button = $this->findElement(WebDriverBy::id($id));
106        $button->click();
107    }
108
109    /**
110     * Debug method to dump all text in <body></body>.
111     * @return void
112     */
113    public function dumpBody()
114    {
115        $body = $this->findElement(WebDriverBy::tagName('body'));
116        var_dump('body text = ' . $body->getText());
117    }
118
119    /**
120     * @param string $id
121     * @return Element
122     */
123    public function findById($id)
124    {
125        return $this->findElement(WebDriverBy::id($id));
126    }
127
128    /**
129     * @param string $css
130     * @return ElementCollection
131     */
132    public function findByCss($css)
133    {
134        return $this->findElement(WebDriverBy::cssSelector($css));
135    }
136}
137