|

Selenium Testcase with Nose in Python

Nose makes Testing with Python wonderful. It basically extends the Unittest and provides features such as running only failed test, skip Test cases, running test based on priorities, REGEX pattern that makes Tester’s life easy.

Contents

Installing Nose:

[code]pin install -U nose[/code]

Creating a Selenium Test Cases for Nose:

Project Structure :

Project Structure used with Nose in Python Lets write some code for the Login and Registration test cases in the Selenium with Nose in Python.
Login Test Case
[python] import nose from selenium import webdriver import unittest from nose.plugins.attrib import attr class LoginTest(unittest.TestCase): @classmethod def setUpClass(cls): cls.driver = webdriver.Firefox() cls.driver.implicitly_wait(20) cls.driver.maximize_window() cls.driver.get("http://newtours.demoaut.com/mercurysignon.php") @attr(priority="high") def test_LoginTest(self): ## Login in the Application ## self.driver.find_element_by_xpath("//input[@name=’userName’]").send_keys("techdutta") self.driver.find_element_by_xpath("//input[@name=’password’]").send_keys("test123") self.driver.find_element_by_xpath("//input[@name=’login’]").click() ## Verify that the Profile is Comming , Means Login ## self.assertTrue(self.driver.find_element_by_xpath("//html/body/div[1]/table/tbody/tr/td[2]/table/tbody/tr[2]/td/table/tbody/tr/td[1]/a").is_displayed()) @classmethod def tearDownClass(cls): cls.driver.quit() if __name__ == "__main__": nose.main(verbosity=2) [/python]
Registration Test Case
[python] import nose from selenium import webdriver import unittest from nose.plugins.attrib import attr class RegTest(unittest.TestCase): @classmethod def setUpClass(cls): cls.driver = webdriver.Firefox() cls.driver.implicitly_wait(20) cls.driver.maximize_window() cls.driver.get("http://demoaut.com/mercuryregister.php") @attr(priority="low") def test_RegTest(self): ## Login in the Application ## self.driver.find_element_by_xpath("//input[@name=’email’]").send_keys("techdutta") self.driver.find_element_by_xpath("//input[@name=’password’]").send_keys("test123") self.driver.find_element_by_xpath("//input[@name=’confirmPassword’]").send_keys("test123") self.driver.find_element_by_xpath("//input[@name=’register’]").click() ## Verify that the Profile is Comming , Means Login ## self.assertTrue(self.driver.find_element_by_xpath("//html/body/div[1]/table/tbody/tr/td[2]/table/tbody/tr[2]/td/table/tbody/tr/td[1]/a").is_displayed()) @classmethod def tearDownClass(cls): cls.driver.quit() if __name__ == "__main__": nose.main(verbosity=2) [/python] Now Lets see what all options we can use with Nose to Test these Test Cases. Case 1: Run all Test Cases in the Folder. Navigate to the Folder where all test cases are present and execute below command.This will execute every Test case that match with the TestMatch Regex [code] >nosetests –verbosity=3[/code] Case 2: Run all Test Cases in the Folder based on the Low or High priority. You can mention the priority of the test function by importing attr from nose.plugins.attrib [code] from nose.plugins.attrib import attr[/code] Adding the attr annotations above Test Function. [code]@attr(priority="low")[/code]
Command line
[code] >nosetests –verbosity=3[/code] Case 3: Running multiple test cases by Name: Case 4: Running Failed Test. If you have any Test case failed in the previous run. It will run only that cases(Failed). Case 5:  You can create a config file and run it directly by entering the command line argument in it. e.g. Config.cfg [code] [nosetests] verbosity=3 with-doctest=1 [/code] Case 6: Running test with Regex Pattern [code](Default: (?:^|[b_./-])[Tt]est [NOSE_TESTMATCH])[/code] Command Line: [code]nosetests –testmatch=REGEX[/code] Case 7: Arguments to see all plugins installed [code]nosetests –plugins[/code]

More about Nose can find here. SourceCode on Github

🎭 Want to master this with real projects? Join the Playwright Automation Mastery course at The Testing Academy.

🎓 Master Playwright End to End

Join hundreds of SDETs building real automation frameworks. Lifetime access, hands-on projects, and a job-ready portfolio.

Similar Posts

6 Comments

  1. Good pоst. I will be facing some of these issues as well..

    my blog post :: led lommelygte tilbud

  2. Yes but you need to look at all the pluses and negatives. Many companies are just trying to get rid of it. Too much to just maintain these type of nose framewotks

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.