When developing a solution, and more specific a web application in this example, part of your CI/CD process should be automated testing. Ensuring automated testing will save a lot of time and money and will support a fail fast principle where developers are made aware of issues in the application in a very early stage of the development process.
Part of a fail fast strategy is developing in a business driven development or test driven development manner and ensure that developers will develop automated tests to validate every part of the application. The growing set of tests can be executed every time the CI/CD automation triggers the automated testing.
Selenium for testing
One of the test automation tools commonly used is Selenium. Selenium is capable of running a web browser and interact as a user would interact with the system while checking every assertion defined in the test code.
Selenium example with XML output
The below example showcases a very small testcase which will execute two tests and which has been tested using a Oracle Linux instance to run the Selenium code. The difference between a standard selenium test and the below example is that it ensures that the test report is generated as XML and is stored in a default location.
Storing the test reports in XML will enable you to combine and report multiple testcases into a single test report while building individual tests instead of one large testcase. Selenium supports multiple development languages to define your testcases, the below example is a Python based testcase.
Executing the test
If you execute the above test you will notice that one case will fail and one will succeed (at this very moment). A standard execution looks like the example below:
As an addition you will find the XML report in the ./test-reports directory for future references and to enable you to parse the individual XML reports into a single report.
Part of a fail fast strategy is developing in a business driven development or test driven development manner and ensure that developers will develop automated tests to validate every part of the application. The growing set of tests can be executed every time the CI/CD automation triggers the automated testing.
Selenium for testing
One of the test automation tools commonly used is Selenium. Selenium is capable of running a web browser and interact as a user would interact with the system while checking every assertion defined in the test code.
Selenium example with XML output
The below example showcases a very small testcase which will execute two tests and which has been tested using a Oracle Linux instance to run the Selenium code. The difference between a standard selenium test and the below example is that it ensures that the test report is generated as XML and is stored in a default location.
Storing the test reports in XML will enable you to combine and report multiple testcases into a single test report while building individual tests instead of one large testcase. Selenium supports multiple development languages to define your testcases, the below example is a Python based testcase.
import unittest import xmlrunner from selenium import webdriver from selenium.webdriver.common.keys import Keys # set some generic variables used within the wider test scripting geckodriver = '/usr/local/lib/selenium/drivers/geckodriver' options = webdriver.FirefoxOptions() options.add_argument('-headless') class PythonOrgSearch(unittest.TestCase): def setUp(self): self.driver = webdriver.Firefox(executable_path=geckodriver, firefox_options=options) def test_search_in_python_org(self): driver = self.driver driver.get("http://www.python.org") self.assertIn("Python", driver.title) elem = driver.find_element_by_name("q") elem.send_keys("pycon") elem.send_keys(Keys.RETURN) assert "No results found." in driver.page_source def testCaseFindTitle(self): driver = self.driver driver.get("http://www.python.org") self.assertIn("Python", driver.title) elem = driver.find_element_by_name("q") elem.send_keys("pycon") elem.send_keys(Keys.RETURN) assert "No results found." not in driver.page_source def tearDown(self): self.driver.close() if __name__ == "__main__": unittest.main( testRunner=xmlrunner.XMLTestRunner(output='test-reports'), failfast=False, buffer=False, catchbreak=False)
Executing the test
If you execute the above test you will notice that one case will fail and one will succeed (at this very moment). A standard execution looks like the example below:
[root@testnode12]# python 7seltest.py Running tests... ---------------------------------------------------------------------- .F ====================================================================== ERROR [15.376s]: test_search_in_python_org (__main__.PythonOrgSearch) ---------------------------------------------------------------------- Traceback (most recent call last): File "7seltest.py", line 23, in test_search_in_python_org assert "No results found." in driver.page_source AssertionError ---------------------------------------------------------------------- Ran 2 tests in 27.462s FAILED (errors=1) Generating XML reports... [root@testnode12]#
As an addition you will find the XML report in the ./test-reports directory for future references and to enable you to parse the individual XML reports into a single report.
No comments:
Post a Comment