image Rio das Ostras, Brazil, 06/2010

Codeption and Selenium on Laravel Homestead

<p>Codeception testing using PhantomJS is great, works, most of the times, but can be problematic. A simple click on a Javascript resource might be tricky and, oddly, may not work in different boxes/releases, even having the same PhantomJS and Codeception running on them, don't ask me why.</p> <p>As an example, here are the things I tried do so a simple click:</p> <p>1) Selecting by name</p> <pre class="prettyprint"><code >$I->click('Refresh'); $I->waitForText('johndoeskype', 10); </code></pre> <p>2) Selecting by id</p> <pre class="prettyprint"><code >$I->click('#refresh'); $I->waitForText('johndoeskype', 10); </code></pre> <p>3) Using Javascript to click</p> <pre class="prettyprint"><code >$I->waitForJS("return jQuery('#refresh').click();", 10); $I->see('johndoeskype'); // or even $I->waitForText('johndoeskype', 10); </code></pre> <p>One development box got this last one working, but the other one didn't. So I decided it was too complicated and I would have to try Selenium + Firefox, again. The problem with selenium, to me, is that my development server is a headless box, but I figured how to install it on Ubuntu 14.04:</p> <h3>Install Oracle Java</h3> <pre class="prettyprint"><code >sudo add-apt-repository ppa:webupd8team/java sudo apt-get update sudo apt-get --yes install oracle-java8-installer </code></pre> <h3>Install Xvfb and Firefox</h3> <p>Note that you don't need to install a full x-window-system, just Xvfb (X virtual framebuffer).</p> <pre class="prettyprint"><code >sudo apt-get --yes install xvfb firefox </code></pre> <h3>Download Selenium</h3> <pre class="prettyprint"><code >export SELENIUM_VERSION=2.42 export SELENIUM_PATCH=2 sudo mkdir -p /usr/local/share/selenium/selenium-$SELENIUM_VERSION.$SELENIUM_PATCH/ sudo wget -N --output-document=/usr/local/share/selenium/selenium-$SELENIUM_VERSION.$SELENIUM_PATCH/selenium-server-standalone-$SELENIUM_VERSION.$SELENIUM_PATCH.jar$SELENIUM_VERSION/selenium-server-standalone-$SELENIUM_VERSION.$SELENIUM_PATCH.jar sudo wget -N --output-document=/usr/local/share/selenium/selenium-java-$SELENIUM_VERSION.$$SELENIUM_VERSION/selenium-java-$SELENIUM_VERSION.$ sudo unzip /usr/local/share/selenium/selenium-java-$SELENIUM_VERSION.$ -d /usr/local/share/selenium/ </code></pre> <h3>Execute Xvfb</h3> <p>I'm using display 99, but you can choose whatever number you think is best for you:</p> <pre class="prettyprint"><code >Xvfb :99 -ac & </code></pre> <h3>Execute Firefox</h3> <pre class="prettyprint"><code >export DISPLAY=:99 firefox & </code></pre> <h3>Execute Selenium</h3> <p>Create a new terminal and keep it open:</p> <pre class="prettyprint"><code >export SELENIUM_VERSION=2.42 export SELENIUM_PATCH=2 java -jar /usr/local/share/selenium/selenium-$SELENIUM_VERSION.$SELENIUM_PATCH/selenium-server-standalone-$SELENIUM_VERSION.$SELENIUM_PATCH.jar </code></pre> <h3>Configure your suite (acceptance.suite.yml)</h3> <pre class="prettyprint"><code >modules: enabled: [WebDriver] config: WebDriver: url: 'http://localhost/' browser: firefox window_size: 1024x768 wait: 10 capabilities: unexpectedAlertBehaviour: 'accept' </code></pre> <h3>Run your tests</h3> <pre class="prettyprint"><code >vendor/bin/codecept run acceptance </code></pre> <h3>Create a script</h3> <p>Now you got everything running, you can boot Selenium from a bash script:</p> <p>Add the exports to your <code class="spancode">.bashrc</code> file:</p> <pre class="prettyprint"><code >echo "export SELENIUM_VERSION=2.42" | tee -a ~/.bashrc echo "export SELENIUM_PATCH=2" | tee -a ~/.bashrc echo "export DISPLAY=:99" | tee -a ~/.bashrc </code></pre> <p>Create a new script:</p> <pre class="prettyprint"><code >sudo vi /usr/local/bin/selenium </code></pre> <p>Paste that in:</p> <pre class="prettyprint"><code >echo --- Killing all processes sudo killall -9 -q Xvfb sudo killall -9 -q firefox sudo killall -9 -q java echo --- Running Xvfb Xvfb :99 -ac & > /dev/null 2>&1 sleep 2 echo --- Running Firefox firefox & sleep 2 echo --- Running Selenium java -jar /usr/local/share/selenium/selenium-$SELENIUM_VERSION.$SELENIUM_PATCH/selenium-server-standalone-$SELENIUM_VERSION.$SELENIUM_PATCH.jar & </code></pre> <p>Make it executable:</p> <pre class="prettyprint"><code >sudo chmod +x /usr/local/bin/selenium </code></pre> <p>And now you just have to run it in a new terminal window, because it's really verbose while being used</p> <pre class="prettyprint"><code >selenium </code></pre> <h3>Enjoy Laravel!</h3>

comments powered by Disqus