📜 ⬆️ ⬇️

Switch to Selenium 2 + PhantomJS

In this post I will write down how I switched from Selenium RC to Selenium 2. The transition was motivated by using PhantomJS as a browser. PhantomJS has a built-in Ghost Driver, which is a WebDriver in the sense that Selenium 2 uses. But my previous PHPUnit tests used the class PHPUnit_Extensions_SeleniumTestCase, which does not know how to work with WebDriver. It was necessary to replace it with PHPUnit_Extensions_Selenium2TestCase along with the entire syntax of the tests.

Existing environment


Tests written with the help of Selenium IDE, which, thanks to Jenkins, for each hg push are converted to PHPUnit TestCase using selenium2php. After this, PHPUnit itself is launched on the received php-test files. When converting, the IP address of the virtual machine is automatically entered, where the Selenium RC server is located, which runs the tests in the Firefox browser.

purpose


Get the same, only instead of Firefox console PhantomJS and the expected performance increase as a result.
')

Features of Selenium 2


Selenium 2 has its own characteristics that should be considered.
1. All link transitions no longer need to wait, that is, there is no need for clickAndWait, just click, if it comes to a normal link (not the js transition function). The reason for this is that now all calls are blocking, that is, not asynchronous, as noted on stackoverflow, this is good.
2. When working with javascript, no locks occur, and you need to manually insert wait commands. For example, waitForElementPresent to a locator that appears after an AJAX request has been completed.
3. You cannot manipulate invisible elements.
But features appeared and at PHPUnit Selenium2TestCase. The main thing is almost completely new syntax. All commands take only one argument, and some of the old ones are now called differently. For example, instead of open (), now url ().
There is a need to find ways to circumvent these innovations.

Method 1: Adapter.


I decided to try writing the MigrationToSelenium2 adapter to switch from PHPUnit_Extensions_SeleniumTestCase to PHPUnit_Extensions_Selenium2TestCase. That is, I planned to continue using selenium2php for converting tests in the Selenium RC format, but inheriting my Migration class, which will implement the methods using the PHPUnit Selenium2 format.
When it came to AJAX requests and the manipulation of the visibility of elements on the page, I realized that the tests would not be left unchanged. And since the tests are still changed, I decided to change the converter at the same time. There are currently no plans to support the Migration Class.

Method 2: selenium2php - Automatic converter of Selenium IDE tests to Selenium 2


I expanded the project selenium2php to support the Selenium2 format. In the description of the project on Github'e there are examples of use. In short, now from the html tests of the Selense format using the console utility we get the test file with the syntax for the class PHPUnit_Extensions_Selenium2TestCase.

Running tests on PhantomJS


Download (if not yet) Selenium Server (aka RC) and PhantomJS . Launch the Selenium grid hub and connect PhantomJS to it as a client:
java -jar selenium.jar -role hub
phantomjs --webdriver=1408 --webdriver-selenium-grid-hub=http://127.0.0.1:4444


The environment for the execution of tests is ready. Now you need to make sure that in the test the target browser contains the string "phantomjs", and the address and port match the grid hub.
It remains to run PHPUnit:
php phpunit.phar YourTest.php

Conclusion


Correction of ready-made tests, although in Selenium IDE, took a long time to support Selenium 2, but it is still faster and more convenient than creating and correcting php tests later. The selenium2php converter facilitates this work.

Unfortunately, as a result, serious shortcomings were discovered.
1. Everything was checked on PHPUnit 3.7.27, Selenium Server 2.37, PhantomJS 1.9.2, but in the end, on the WinXP virtual machine, the bundle was unstable and in 70% of cases a new test was issued FORWARDING_TO_NODE_FAILED. On Win7, this happened much less frequently, about 1 test out of 50. But the very fact of having such attacks is very difficult when using CI.
2. Another disappointment - the speed of the script. Although PhantomJS is a console one, it takes 14 minutes to complete all tests, instead of 19 minutes with Selenium 1 + Firefox.
Together, these two shortcomings made me temporarily set aside Selenium 2 until better times.

Source: https://habr.com/ru/post/198458/


All Articles