{"id":16,"date":"2019-05-24T01:31:08","date_gmt":"2019-05-24T01:31:08","guid":{"rendered":"http:\/\/ittech.expert\/?p=16"},"modified":"2019-05-24T01:32:43","modified_gmt":"2019-05-24T01:32:43","slug":"very-basic-automation-testing-with-selenium-and-python-3","status":"publish","type":"post","link":"https:\/\/ittech.expert\/?p=16","title":{"rendered":"Very basic automation testing with Selenium and Python 3"},"content":{"rendered":"\n<p>This is great option to use when you&#8217;re unit testing a web-based application or need to do testing that revolves around using a Web browser.<\/p>\n\n\n\n<p>Selenium is an open-source automation tool used in this situation &#8212; <a href=\"https:\/\/www.seleniumhq.org\/\">https:\/\/www.seleniumhq.org\/<\/a> . You can use Selenium with pretty much any language you want, but in this example, I&#8217;m choosing to use Python 3.<\/p>\n\n\n\n<p>Very easy guide can be found at <a href=\"https:\/\/pypi.org\/project\/selenium\/\">https:\/\/pypi.org\/project\/selenium\/<\/a> which will give you the basics on how to install Selenium via pip.<\/p>\n\n\n\n<p>Once you have this installed, you&#8217;ll need to make sure you have WebDrivers for each browser you wish to use. In my case, I&#8217;m just using Firefox. It&#8217;s not explained too well how to use the driver, but essentially you just need to add the driver directory or copy the driver into a directory that is present in your environment path (my OS of choice is Fedora, so this will be the $PATH environment variable).<\/p>\n\n\n\n<p>I&#8217;ve taken the example shown in the easy guide, and adjusted based on an excellent tutorial at <a href=\"https:\/\/www.internalpointers.com\/post\/run-painless-test-suites-python-unittest\">https:\/\/www.internalpointers.com\/post\/run-painless-test-suites-python-unittest<\/a> to show how Python&#8217;s unittest library and Selenium work together &#8212; I made two small Python files to handle the test:<\/p>\n\n\n\n<p><strong>browser_test.py<\/strong><br><\/p>\n\n\n\n<p>import unittest<br> from selenium import webdriver<\/p>\n\n\n\n<p>class BrowserTestCase(unittest.TestCase):<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def setUp(self):\n    self.browser = webdriver.Firefox()\n    self.addCleanup(self.browser.quit)\n\ndef testPageTitle(self):\n    self.browser.get('http:\/\/ittech.expert')\n    self.assertIn('Nathan', self.browser.title)<\/code><\/pre>\n\n\n\n<p><br><\/p>\n\n\n\n<p><strong>driver.py<\/strong><\/p>\n\n\n\n<p>\nimport unittest<\/p>\n\n\n\n<p># Import our browser\ntest case<\/p>\n\n\n\n<p>import browser_test<\/p>\n\n\n\n<p># Init test suite<\/p>\n\n\n\n<p>test_loader =\nunittest.TestLoader()<\/p>\n\n\n\n<p>test_suite =\nunittest.TestSuite()<\/p>\n\n\n\n<p># Add tests to suite<\/p>\n\n\n\n<p>test_suite.addTests(test_loader.loadTestsFromModule(browser_test))<\/p>\n\n\n\n<p># Run the test!<\/p>\n\n\n\n<p>test_runner =\nunittest.TextTestRunner(verbosity=2)<\/p>\n\n\n\n<p>test_result =\ntest_runner.run(test_suite)<\/p>\n\n\n\n<p><strong>Testing it out&#8230;<br><\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"477\" height=\"153\" src=\"http:\/\/ittech.expert\/wp-content\/uploads\/2019\/05\/image-1.png\" alt=\"\" class=\"wp-image-18\" srcset=\"https:\/\/ittech.expert\/wp-content\/uploads\/2019\/05\/image-1.png 477w, https:\/\/ittech.expert\/wp-content\/uploads\/2019\/05\/image-1-300x96.png 300w\" sizes=\"auto, (max-width: 477px) 100vw, 477px\" \/><\/figure>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is great option to use when you&#8217;re unit testing a web-based application or need to do testing that revolves around using a Web browser. Selenium is an open-source automation tool used in this situation &#8212; https:\/\/www.seleniumhq.org\/ . You can use Selenium with pretty much any language you want, but in this example, I&#8217;m choosing to use Python 3. Very easy guide can be found at https:\/\/pypi.org\/project\/selenium\/ which will give<\/p>\n<div class=\"read-more\"><a class=\"btn read-more-btn\" href=\"https:\/\/ittech.expert\/?p=16\">Read More<\/a><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[2,4,3],"class_list":["post-16","post","type-post","status-publish","format-standard","hentry","category-python","tag-automation","tag-python","tag-selenium"],"_links":{"self":[{"href":"https:\/\/ittech.expert\/index.php?rest_route=\/wp\/v2\/posts\/16","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ittech.expert\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ittech.expert\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ittech.expert\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ittech.expert\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=16"}],"version-history":[{"count":1,"href":"https:\/\/ittech.expert\/index.php?rest_route=\/wp\/v2\/posts\/16\/revisions"}],"predecessor-version":[{"id":19,"href":"https:\/\/ittech.expert\/index.php?rest_route=\/wp\/v2\/posts\/16\/revisions\/19"}],"wp:attachment":[{"href":"https:\/\/ittech.expert\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=16"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ittech.expert\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=16"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ittech.expert\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=16"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}