0

Here is my code. I wrote script which can test multiple test cases. This scripts throws an exception when an error comes however i want that it keeps the count of errors and their descriptions and log after test execution whether it is success or failed. More important is should run complete test script in any how.

from selenium import webdriver
from generic_functions.FindElement import HandyWrappers
from generic_functions.takescreenshots import Screenshot
from generic_functions.error_handle import CatchExceptions
import os
import time
import unittest


class TestEnrollment(unittest.TestCase):
    driverLocation = 
    "C:\\Users\\Sales\\Desktop\\Automation\\Trendz\\libs\\chromedriver.exe"
    os.environ["webdriver.chrome.driver"] = driverLocation
    driver = webdriver.Chrome(driverLocation)
    driver.maximize_window()
    driver.implicitly_wait(10)


def test_login(self):
    try:
        baseURL = "https://portal.ABCD.com"
        driver = self.driver
        driver.get(baseURL)
        hw = HandyWrappers(driver)

        username = hw.getElement(".//form[@id='login-form']/fieldset/section[1]/label[2]/input[@name='email']",
                                 locatorType="xpath")
        username.send_keys("ABCD@live.com")
        time.sleep(2)

        password = hw.getElement(".//form[@id='login-form']/fieldset/section[2]/label[2]/input[@name='password']",
                                 locatorType="xpath")
        password.send_keys("ABCD09")

        signIn = hw.getElement(".//form[@id='login-form']/footer/button[contains(text(),'Sign in')]",
                               locatorType="xpath")
        signIn.click()

        self.assertTrue(driver.find_element_by_xpath(".//header[@id='header']/section/div/div[@id='logout']/span/a12"), "Sign Out")


    except Exception as err:
          raise err

def test_menu_enr(self):
    driver = self.driver
    hw = HandyWrappers(driver)

    find_enrollment = hw.getElement(".//aside[@id='left-panel']/nav/ul[14]/li/a/span", locatorType="xpath")
    find_enrollment.click()


if __name__ == '__main__':
    unittest.main()
  • you have `raise err` in `test_login` which throws error when exception happens. remove it and just print error like `print(err)` – REDDY PRASAD Jul 12 '18 at 08:19
  • yeah. I already did it. But it shows me 'Test result OK'. However there is an error, which must be in the final result @REDDYPRASAD – Ravindra Kumar Jul 12 '18 at 08:23
  • basically I have to log that error in my test report. If i did as u said then it will never come in error count. – Ravindra Kumar Jul 12 '18 at 08:27

3 Answers3

0

Don't raise it, Use pass or continue

def test_login():
   try:
       ... 
   except Exception:
       # log the exception here
       pass  # or you could use 'continue'

Note: Have to look into this also diff bw pass and continue

ramganesh
  • 741
  • 1
  • 8
  • 33
0

collect all errors in some list and then assert error list at end of test

from selenium import webdriver
from generic_functions.FindElement import HandyWrappers
from generic_functions.takescreenshots import Screenshot
from generic_functions.error_handle import CatchExceptions
import os
import time
import unittest


class TestEnrollment(unittest.TestCase):
    driverLocation = "C:\\Users\\Sales\\Desktop\\Automation\\Trendz\\libs\\chromedriver.exe"
    os.environ["webdriver.chrome.driver"] = driverLocation
    driver = webdriver.Chrome(driverLocation)
    driver.maximize_window()
    driver.implicitly_wait(10)

    def setUp(self):
        self.errors = []

    def tearDown(self):
        self.assertEqual([], self.errors)

    def test_login(self):
        try:
            baseURL = "https://portal.ABCD.com"
            driver = self.driver
            driver.get(baseURL)
            hw = HandyWrappers(driver)

            username = hw.getElement(".//form[@id='login-form']/fieldset/section[1]/label[2]/input[@name='email']",
                                     locatorType="xpath")
            username.send_keys("ABCD@live.com")
            time.sleep(2)

            password = hw.getElement(".//form[@id='login-form']/fieldset/section[2]/label[2]/input[@name='password']",
                                     locatorType="xpath")
            password.send_keys("ABCD09")

            signIn = hw.getElement(".//form[@id='login-form']/footer/button[contains(text(),'Sign in')]",
                                   locatorType="xpath")
            signIn.click()

            self.assertTrue(driver.find_element_by_xpath(
                ".//header[@id='header']/section/div/div[@id='logout']/span/a12"), "Sign Out")

        except Exception as err:
            self.errors.append(err)

    def test_menu_enr(self):
        driver = self.driver
        hw = HandyWrappers(driver)

        find_enrollment = hw.getElement(
            ".//aside[@id='left-panel']/nav/ul[14]/li/a/span", locatorType="xpath")
        find_enrollment.click()


if __name__ == '__main__':
    unittest.main()

tearDown method will run at end and shows all error messages if any present

As for i know , this is not ideal solution. its kind of hack fix

REDDY PRASAD
  • 1,309
  • 2
  • 14
  • 29
0

If you're using selenium webdriver with Python unittest (looks like you are), the SeleniumBase framework has the nifty delayed_assert_text and delayed_assert_element methods built-in for processing test failures at the end of the test. You can install seleniumbase with pip, or clone the repo from github.

The following code is taken from this example.

from seleniumbase import BaseCase

class MyTestClass(BaseCase):

    def test_delayed_asserts(self):
        self.open('http://xkcd.com/993/')
        self.wait_for_element('#comic')
        self.delayed_assert_element('img[alt="Brand Identity"]')
        self.delayed_assert_element('img[alt="Rocket Ship"]')  # Will Fail
        self.delayed_assert_element('#comicmap')
        self.delayed_assert_text('Fake Item', '#middleContainer')  # Will Fail
        self.delayed_assert_text('Random', '#middleContainer')
        self.delayed_assert_element('a[name="Super Fake !!!"]')  # Will Fail
        self.process_delayed_asserts()

Inside the script, there are multiple asserts that will fail, but only after self.process_delayed_asserts() is called. Here's the output after running it:

pytest delayed_assert_test.py

=============================== test session starts ===============================
platform darwin -- Python 3.6.5, pytest-3.7.3, py-1.5.4, pluggy-0.7.1

delayed_assert_test.py F

==================================== FAILURES =====================================
________________________ MyTestClass.test_delayed_asserts _________________________
E               Exception: 
E               *** DELAYED ASSERTION FAILURES FOR: examples.delayed_assert_test.MyTestClass.test_delayed_asserts
E               CHECK #2: (https://xkcd.com/993/)
E                Element {img[alt="Rocket Ship"]} was not visible after 1 second!
E               CHECK #4: (https://xkcd.com/993/)
E                Expected text {Fake Item} for {#middleContainer} was not visible after 1 second!
E               CHECK #6: (https://xkcd.com/993/)
E                Element {a[name="Super Fake !!!"]} was not visible after 1 second!

Exception
============================ 1 failed in 7.53 seconds =============================
Michael Mintz
  • 9,007
  • 6
  • 31
  • 48