{"id":486,"date":"2013-08-03T10:41:58","date_gmt":"2013-08-03T09:41:58","guid":{"rendered":"http:\/\/blog.cyril-grandjean.co.uk\/?p=486"},"modified":"2018-03-22T18:46:28","modified_gmt":"2018-03-22T17:46:28","slug":"ensure-the-quality-of-your-web-projects-with-selenium","status":"publish","type":"post","link":"https:\/\/www.cyril-grandjean.fr\/en\/quality-web-projects-with-selenium","title":{"rendered":"Ensure the quality of your web projects with Selenium"},"content":{"rendered":"<p>When you are developing a release of a web application, you will very often need to create a testing procedure which will test all the features of your application. Then, you will have to run all the tests manually for each version of your software in order to ensure that no regression has occurred. Functional manual tests are long, boring and can be subject to some human errors.<\/p>\n<p>You can avoid these disadvantages by automating your functional tests using Selenium library. By using Selenium library in Java, you will be able to automate your web browser actions and you will be able to check that these actions will make their expecting behaviour.<\/p>\n<p>We will take example of this basic PHP program:<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">&lt;?php\r\nsession_start();\r\n\r\n\/\/We add the element into an array in session\r\nif(isset($_POST&#x5B;'elementToAdd']))\r\n{\r\n\t\/\/If the array is already created\r\n\tif(is_array($_SESSION&#x5B;'array']))\r\n\t{\r\n\t\t\/\/Limit of 10 elements\r\n\t\tif(sizeof($_SESSION&#x5B;'array']) &lt; 10)\r\n\t\t{\r\n\t\t\t\/\/We add our element\r\n\t\t\tarray_push($_SESSION&#x5B;'array'],$_POST&#x5B;'elementToAdd']);\r\n\t\t}\r\n\t}\r\n\telse\r\n\t{\r\n\t\t\/\/We create a new array\r\n\t\t$_SESSION&#x5B;'array'] = array($_POST&#x5B;'elementToAdd']);\r\n\t}\r\n}\r\n\/\/We remove an element from the array in session\r\nelse if(isset($_POST&#x5B;'elementToDelete']))\r\n{\r\n\tunset($_SESSION&#x5B;'array']&#x5B;array_search($_POST&#x5B;'elementToDelete'], $_SESSION&#x5B;'array'])]);\r\n}\r\n?&gt;\r\n&lt;html&gt;\r\n&lt;head&gt;\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n\t&lt;?php\r\n\t\/\/We display the HTML array if there is any element in session\r\n\tif(isset($_SESSION&#x5B;'array']) &amp;&amp; is_array($_SESSION&#x5B;'array']) &amp;&amp; sizeof($_SESSION&#x5B;'array']) &gt; 0)\r\n\t{\r\n\t?&gt;\r\n\t\t&lt;table&gt;\r\n\t\t\t&lt;?php\r\n\t\t\t\/\/We display our elements in session\r\n\t\t\tforeach ($_SESSION&#x5B;'array'] as $value)\r\n\t\t\t{\r\n\t\t\t?&gt;\r\n\t\t\t\t&lt;tr class=&quot;aRow&quot;&gt;\r\n\t\t\t\t\t&lt;td class=&quot;rowValue&quot;&gt;\r\n\t\t\t\t\t\t&lt;?php echo $value ?&gt;\r\n\t\t\t\t\t&lt;\/td&gt;\r\n\t\t\t\t\t&lt;td&gt;\r\n\t\t\t\t\t\t&lt;form method=&quot;post&quot;&gt;\r\n\t\t\t\t\t\t\t&lt;input type=&quot;hidden&quot; name=&quot;elementToDelete&quot; value=&quot;&lt;?php echo $value ?&gt;&quot; \/&gt;\r\n\t\t\t\t\t\t\t&lt;input type=&quot;submit&quot; class=&quot;deleteButton&quot; value=&quot;Delete element&quot; \/&gt;\r\n\t\t\t\t\t\t&lt;\/form&gt;\r\n\t\t\t\t\t&lt;\/td&gt;\r\n\t\t\t\t&lt;\/tr&gt;\r\n\t\t\t&lt;?php\r\n\t\t\t}\r\n\t\t\t?&gt;\r\n\t\t&lt;\/table&gt;\r\n\t&lt;?php\r\n\t}\t\r\n\t?&gt;\r\n\t\r\n\t&lt;form method=&quot;post&quot;&gt;\r\n\t\t&lt;input name=&quot;elementToAdd&quot; type=&quot;text&quot; \/&gt;\r\n\t\t&lt;input id=&quot;submitButton&quot; type=&quot;submit&quot; value=&quot;Add element&quot; \/&gt;\r\n\t&lt;\/form&gt;\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;<\/pre>\n<p>You can click <a title=\"Selenium test page\" href=\"http:\/\/www.cyril-grandjean.fr\/en\/selenium.php\" target=\"_blank\">here<\/a> to find the result of this PHP script.<\/p>\n<p>Inside this PHP script, we have a form which we will use in order to add an element into a table stored in session limited to 10 elements only. When an element is added into the array, we can remove it by pressing the remove button.<\/p>\n<p>We will create the following functional tests which we will automate using Selenium:<\/p>\n<ul>\n<li>We create an element into the array and check that the element is well added.<\/li>\n<li>We remove the element from the array and check that the element is well removed.<\/li>\n<li>We test the limit of 10 elements.<\/li>\n<\/ul>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">public class SeleniumClass {\r\n\r\n    \/**\r\n     * Driver of the web browser\r\n     *\/\r\n    private WebDriver driver;\r\n\r\n    \/**\r\n     * Code to execute before any JUnit test\r\n     *\/\r\n    @Before\r\n    public void setUp() {\r\n\r\n        \/\/We instantiate a Firefox driver\r\n        driver = new FirefoxDriver();\r\n\r\n        \/\/We set a maximum timeout of 5 seconds\r\n        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);\r\n    }\r\n\r\n    \/**\r\n     * JUnit test for our functional tests\r\n     *\/\r\n    @Test\r\n    public void testElementPage()\r\n    {\r\n        \/\/We load the page\r\n        driver.get(&quot;http:\/\/www.cyril-grandjean.co.uk\/en\/selenium.php&quot;);\r\n\r\n        \/\/We fill the text field with the name elementToAdd with the value element to add\r\n        driver.findElement(By.name(&quot;elementToAdd&quot;)).sendKeys(&quot;element to add&quot;);\r\n\r\n        \/\/We click on the button with the id submitButton\r\n        driver.findElement(By.id(&quot;submitButton&quot;)).click();\r\n\r\n        \/\/We check that 1 element with the class name rowValue has been added\r\n        assert driver.findElements(By.className(&quot;rowValue&quot;)).size() == 1;\r\n\r\n        \/\/We check the value of the element in the HTML array\r\n        assert driver.findElement(By.className(&quot;rowValue&quot;)).getText().equals(&quot;element to add&quot;);\r\n\r\n        \/\/We check the hidden input of the form to delete the element by checking the value attribute\r\n        assert driver.findElement(By.name(&quot;elementToDelete&quot;)).getAttribute(&quot;value&quot;).equals(&quot;element to add&quot;);\r\n\r\n        \/\/We remove the element by clicking on the delete button\r\n        driver.findElement(By.className(&quot;deleteButton&quot;)).click();\r\n\r\n        \/\/We check that the element has been removed\r\n        assert driver.findElements(By.className(&quot;rowValue&quot;)).size() == 0;\r\n\r\n        \/\/We will try to add 11 elements\r\n        for(int counterElementAdded = 0; counterElementAdded &lt; 11; counterElementAdded++)\r\n        {\r\n            \/\/We fill the text field with the name elementToAdd with the value element to add\r\n            driver.findElement(By.name(&quot;elementToAdd&quot;)).sendKeys(&quot;element to add&quot;);\r\n\r\n            \/\/We click on the button with the id submitButton\r\n            driver.findElement(By.id(&quot;submitButton&quot;)).click();\r\n        }\r\n\r\n        \/\/We check that there is still 10 elements\r\n        assert driver.findElements(By.className(&quot;rowValue&quot;)).size() == 10;\r\n\r\n        \/\/We remove the elements by clicking on the delete buttons\r\n        while (driver.findElements(By.className(&quot;deleteButton&quot;)).size() &gt; 0)\r\n        {\r\n            \/\/We remove the element by clicking on the delete button\r\n            driver.findElements(By.className(&quot;deleteButton&quot;)).get(0).click();\r\n        }\r\n\r\n        \/\/We check that the elements have been removed\r\n        assert driver.findElements(By.className(&quot;rowValue&quot;)).size() == 0;\r\n    }\r\n\r\n    \/**\r\n     * Code to execute after each JUnit test\r\n     *\/\r\n    @After\r\n    public void tearDown() {\r\n\r\n        \/\/We quit the driver\r\n        driver.quit();\r\n    }\r\n}<\/pre>\n<p>With this JUnit Selenium test, we have been able to automatically tests all the features of our PHP script.<\/p>\n<p>You can find more information about Selenium at this link :\u00a0<a title=\"Selenium documentation website\" href=\"http:\/\/docs.seleniumhq.org\" target=\"_blank\">http:\/\/docs.seleniumhq.org<\/a><\/p>","protected":false},"excerpt":{"rendered":"<p>When you are developing a release of a web application, you will very often need to create a testing procedure which will test all the features of your application. Then, you will have to run all the tests manually for each version of your software in order to ensure that no regression has occurred. Functional [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[26,107],"tags":[77,81,78,54,80,82,76,75,79,73,109],"class_list":["post-486","post","type-post","status-publish","format-standard","hentry","category-developpement-web","category-tous","tag-assurance","tag-automatic","tag-functional","tag-java","tag-junit","tag-qa","tag-quality","tag-selenium","tag-tests","tag-web","tag-web-development"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.cyril-grandjean.fr\/en\/wp-json\/wp\/v2\/posts\/486","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.cyril-grandjean.fr\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.cyril-grandjean.fr\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.cyril-grandjean.fr\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.cyril-grandjean.fr\/en\/wp-json\/wp\/v2\/comments?post=486"}],"version-history":[{"count":0,"href":"https:\/\/www.cyril-grandjean.fr\/en\/wp-json\/wp\/v2\/posts\/486\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.cyril-grandjean.fr\/en\/wp-json\/wp\/v2\/media?parent=486"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.cyril-grandjean.fr\/en\/wp-json\/wp\/v2\/categories?post=486"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.cyril-grandjean.fr\/en\/wp-json\/wp\/v2\/tags?post=486"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}