What is Selenium?
You can look at it like a browser that fires of a set of requests that you asks it to. Sort of like this:
- Go to vg.no
- Check that the title is “Forsiden at VG”
Or
- Go to vg.no
- Click on the first article
- Check that the article loads correctly and some elements are present on the page
You can also do more advanced things like triggering a javascript method for show and hide a menu.
- Go to vg.no
- Click on menu tab
- Wait for 500 ms
- Check that the menu is now visible
So let’s start to write some tests. Ehm, okay, we actually don’t have to write it. We can use Selenium IDE, a Firefox plugin; Awesome!
When the test works okay we can export it to PHP Unit Selenium tests.
Go to File -> Export Test Case As -> PHP (PHPUnit)
This gives you a file like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | [code language="php"]<?php class Example extends PHPUnit_Extensions_SeleniumTestCase { protected function setUp() { $this->setBrowser("*chrome"); $this->setBrowserUrl("http://vg.no/"); } public function testMyTestCase() { $this->open("utenriks"); $lastTwenty = $this->getXpathCount("//section[@class='widget articles imageList section-index']/article"); $this->click("//a[contains(text(),'Se neste')]"); sleep(5); $this->assertNotEquals($lastTwenty, $this->getXpathCount("//section[@class='widget articles imageList section-index']/article")); } } }[/code] |
Here is another example for testing that the menu becomes visible and gets populated with sub menu items:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | [code language="php"]<?php /** * Test menu */ public function testMenu() { $this->open(""); // Click on the menu button $this->click("link=MENY"); // Wait for the ajax call to complete and populate the menu usleep(500000); // Check that the menu slides down and become visible $this->assertTrue($this->isVisible("//div[@id='sectionNavBar']")); // Check that first menu include eight sub menu items $this->assertEquals("8", $this->getXpathCount("//li[@id='menuItem_0']/ul/li")); // Click on sub menu header 1 $this->click("//li[@id='menuItem_1']/a"); // Wait for the animation to complete usleep(500000); // Check that its sub menu is not empty $this->assertNotEquals("0", $this->getXpathCount("//li[@id='menuItem_1']/ul/li")); // Click on sub menu header 2 $this->click("//li[@id='menuItem_2']/a"); // Wait for the animation to complete usleep(500000); // Check that its sub menu is not empty $this->assertNotEquals("0", $this->getXpathCount("//li[@id='menuItem_2']/ul/li")); // Click on sub menu header 3 $this->click("//li[@id='menuItem_3']/a"); // Wait for the animation to complete usleep(500000); // Check that its sub menu is not empty $this->assertNotEquals("0", $this->getXpathCount("//li[@id='menuItem_3']/ul/li")); // Click on sub menu header 4 $this->click("//li[@id='menuItem_4']/a"); // Wait for the animation to complete usleep(500000); // Check that its sub menu is not empty $this->assertNotEquals("0", $this->getXpathCount("//li[@id='menuItem_4']/ul/li")); // Click on sub menu header 5 $this->click("//li[@id='menuItem_5']/a"); // Wait for the animation to complete usleep(500000); // Check that its sub menu is not empty $this->assertNotEquals("0", $this->getXpathCount("//li[@id='menuItem_5']/ul/li")); // Close the menu $this->click("link=MENY"); }[/code] |
This is what it looks like when you run the IDE
Selenium is just great and should really be used whenever integrating new features. Properly designed tests can check and verify that existing interactivity and markup still works as intended when new features are introduced.
Finally, to create a really powerful environment, you should set up Jenkins to automatically start a build and run phpunit tests and Selenium tests for each commit. This will save you a lot of time, by automatically be notified when something unintentionally breaks.