UI automated testing using Selenium in Badgeyay

With all the major functionalities packed into the badgeyay web application, it was time to add some automation testing to automate the review process in case of known errors and check if code contribution by contributors is not breaking anything. We decided to go with Selenium for our testing requirements. What is Selenium? Selenium is a portable software-testing framework for web applications. Selenium provides a playback (formerly also recording) tool for authoring tests without the need to learn a test scripting language. In other words, Selenium does browser automation:, Selenium tells a browser to click some element, populate and submit a form, navigate to a page and any other form of user interaction. Selenium supports multiple languages including C#, Groovy, Java, Perl, PHP, Python, Ruby and Scala. Here, we are going to use Python (and specifically python 2.7). First things first: To install these package run this code on the CLI: pip install selenium==2.40 pip install nose Don’t forget to add them in the requirements.txt file Web Browser: We also need to have Firefox installed on your machine. Writing the Test An automated test automates what you'd do via manual testing - but it is done by the computer. This frees up time and allows you to do other things, as well as repeat your testing. The test code is going to run a series of instructions to interact with a web browser - mimicking how an actual end user would interact with an application. The script is going to navigate the browser, click a button, enter some text input, click a radio button, select a drop down, drag and drop, etc. In short, the code tests the functionality of the web application. A test for the web page title: import unittest from selenium import webdriver class SampleTest(unittest.TestCase): @classmethod def setUpClass(cls): cls.driver = webdriver.Firefox() cls.driver.get('http://badgeyay-dev.herokuapp.com/') def test_title(self): self.assertEqual(self.driver.title, 'Badgeyay') @classmethod def tearDownClass(cls): cls.driver.quit()   Run the test using nose test.py Clicking the element For our next test, we click the menu button, and check if the menu becomes visible. elem = self.driver.find_element_by_css_selector(".custom-menu-content") self.driver.find_element_by_css_selector(".glyphicon-th").click() self.assertTrue(elem.is_displayed())   Uploading a CSV file: For our next test, we upload a CSV file and see if a success message pops up. def test_upload(self): Imagepath = os.path.abspath(os.path.join(os.getcwd(), 'badges/badge_1.png')) CSVpath = os.path.abspath(os.path.join(os.getcwd(), 'sample/vip.png.csv')) self.driver.find_element_by_name("file").send_keys(CSVpath) self.driver.find_element_by_name("image").send_keys(Imagepath) self.driver.find_element_by_css_selector("form .btn-primary").click() time.sleep(3) success = self.driver.find_element_by_css_selector(".flash-success") self.assertIn(u'Your badges has been successfully generated!', success.text)   The entire code can be found on: https://github.com/fossasia/badgeyay/tree/development/app/tests We can also use the Phantom.js package along with Selenium for UI testing purposes without opening a web browser. We use this for badgeyay to run the tests for every commit in Travis CI which cannot open a program window. Resources Selenium with Python by Baiju Muthukadan: http://selenium-python.readthedocs.io Getting started with UI autometed tests using (Selenium + Python) by Daniel Anggrianto: https://engineering.aweber.com/getting-started-with-ui-automated-tests-using-selenium-python/ Selenium Webdriver Python Tutorial For Web Automation by Meenakshi Agarwal: http://www.techbeamers.com/selenium-webdriver-python-tutorial/ How to Use Selenium with Python by Guru99: https://www.guru99.com/selenium-python.html

Continue ReadingUI automated testing using Selenium in Badgeyay

Writing Selenium Tests for Checking Bookmark Feature and Search functionality in Open Event Webapp

We integrated Selenium Testing in the Open Event Webapp and are in full swing in writing tests to check the major features of the webapp. Tests help us to fix the issues/bugs which have been solved earlier but keep on resurging when some new changes are incorporated in the repo. I describe the major features that we are testing in this. Bookmark Feature The first major feature that we want to test is the bookmark feature. It allows the users to mark a session they are interested in and view them all at once with a single click on the starred button. We want to ensure that the feature is working on all the pages. Let us discuss the design of the test. First, we start with tracks page. We select few sessions (2 here) for test and note down their session_ids. Finding an element by its id is simple in Selenium can be done easily. After we find the session element, we then find the mark button inside it (with the help of its class name) and click on it to mark the session. After that, we click on the starred button to display only the marked sessions and proceed to count the number of visible elements on the page. If the number of visible session elements comes out to be 2 (the ones that we marked), it means that the feature is working. If the number deviates, it indicates that something is wrong and the test fails. Here is a part of the code implementing the above logic. The whole code can be seen here // Returns the number of visible session elements on the tracks page TrackPage.getNoOfVisibleSessionElems = function() { return this.findAll(By.className('room-filter')).then(this.getElemsDisplayStatus).then(function(displayArr) { return displayArr.reduce(function(counter, value) { return value == 1 ? counter + 1 : counter; }, 0); }); }; // Bookmark the sessions, scrolls down the page and then count the number of visible session elements TrackPage.checkIsolatedBookmark = function() { // Sample sessions having ids of 3014 and 3015 being checked for the bookmark feature var sessionIdsArr = ['3014', '3015']; var self = this; return self.toggleSessionBookmark(sessionIdsArr).then(self.toggleStarredButton.bind(self)).then(function() { return self.driver.executeScript('window.scrollTo(0, 400)').then(self.getNoOfVisibleSessionElems.bind(self)); }); }; Here is the excerpt of code which matches the actual number of visible session elements to the expected number. You can view the whole test script here //Test for checking the bookmark feature on the tracks page it('Checking the bookmark toggle', function(done) { trackPage.checkIsolatedBookmark().then(function(num) { assert.equal(num, 2); done(); }).catch(function(err) { done(err); }); }); Now, we want to test this feature on the other pages: schedule and rooms page. We can simply follow the same approach as done on the tracks page but it is time expensive. Checking the visibility of all the sessions elements present on the page takes quite some time due to a large number of sessions. We need to think of a different approach.We had already marked two elements on the tracks page. We then go to the schedule page and click on the starred mode. We calculate the current height of the page. We…

Continue ReadingWriting Selenium Tests for Checking Bookmark Feature and Search functionality in Open Event Webapp