0

I am trying to test the website live.guru99.com. I am testing three functionalities, named tests A, B and C. I am testing the code using geckodriver, whose exe is in the root folder.

The code is as follows:

from selenium import webdriver
import time
import unittest

class Guru99BankTest(unittest.TestCase):

    @classmethod
    def setUp(self):
        self.driver = webdriver.Firefox()

    def test_C(self):
        driver = self.driver
        driver.get("http://live.guru99.com/")

        driver.find_element_by_link_text('Mobile').click()

        listed_prd_el = driver.find_element_by_xpath(
            "//a[contains(text(), 'Sony Xperia')]/ancestor::div[@class = 'product-info']")
        listed_prd_el.find_element_by_tag_name('button').click()

        qty = driver.find_element_by_xpath("//input[@title='Qty']")

        qty.clear()
        qty.send_keys('1000')

        self.assertEqual(qty.get_attribute('value'), '1000')

        driver.find_element_by_xpath("//button[@title='Update']").click()

        time.sleep(3)
        self.assertIn('products cannot be ordered in requested quantity',
                      driver.find_element_by_class_name('error-msg').text)

        driver.find_element_by_xpath("//button[@title='Empty Cart']").click()

        time.sleep(3)

        self.assertIn('no items in your shopping cart',
                      driver.find_element_by_class_name('cart-empty').text)

    def test_A(self):
        driver = self.driver

        driver.get("http://live.guru99.com/")

        self.assertIn("This is demo site for", driver.page_source)

        driver.find_element_by_link_text('Mobile').click()

        self.assertEqual("Mobile", driver.title)

    def test_B(self):
        driver = self.driver

        driver.get("http://live.guru99.com/")

        driver.find_element_by_link_text('Mobile').click()

        driver.find_element_by_xpath("//select[@title='Sort By']/option[contains(text(), 'Name')]").click()

        product_names = ([e.text for e in driver.find_elements_by_xpath("//h2[@class='product-name']")])

        self.assertEqual(product_names, sorted(product_names))

        listed_prd_el = driver.find_element_by_xpath(
            "//a[contains(text(), 'Sony Xperia')]/ancestor::div[@class = 'product-info']")
        listed_price = listed_prd_el.find_element_by_class_name("price").text

        listed_prd_el.find_element_by_tag_name('a').click()

        prod_price = driver.find_element_by_xpath('//span[@class="price"]').text

        self.assertEqual(listed_price, prod_price)



    @classmethod
    def tearDown(self):
        self.driver.close()

def custom_suite():
    suite = unittest.TestSuite()
    suite.addTest(Guru99BankTest('test_A'))
    suite.addTest(Guru99BankTest('test_B'))
    suite.addTest(Guru99BankTest('test_C'))
    return suite

if __name__ == "__main__":
    runner = unittest.TextTestRunner()
    runner.run(custom_suite())

However, I find that the browser opens only one time and not three times.What am I doing wrong? I am quite new at unittests, so please bear with me.

2
  • there are two tests with the same name test_B, is it intentional? Commented Aug 9, 2017 at 21:00
  • @michaelsatish I am sorry that was a typo Commented Aug 9, 2017 at 22:43

1 Answer 1

1

It worked for me. I changed the last test to test_C as test_b was duplicated. However, having said that I'm not sure if that's the actual situation

Sign up to request clarification or add additional context in comments.

2 Comments

I just get a ran 1 test in my console
Try running it in chrome, not sure it that will make any difference, I'd the chrome driver lying aroung, so fired your code in Chrome and worked. But I dont think browser type and instance determines the flow

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.