Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python selenium multiple test cases

I have the following code in python

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
from unittestzero import Assert
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import ElementNotVisibleException
import unittest, time, re

class HomePageTest(unittest.TestCase):
    expected_title="  some title here "
    def setUp(self):
        self.driver = webdriver.Firefox()
        self.driver.implicitly_wait(30)
        self.base_url = "https://somewebsite.com"
        self.verificationErrors = []

    def test_home_page(self):
        driver=self.driver
        driver.get(self.base_url)
        print "test some things here"




    def test_whatever(self):
        print "test some more things here"

    def tearDown(self):
        self.driver.quit()


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

My problem is after the function test_home_page, the firefox instance closes and opens again for the next test_whatever function. How can I do his so that all the test cases are executed from the same firefox instance.

like image 362
jacksparrow007 Avatar asked Jun 15 '12 11:06

jacksparrow007


People also ask

How do I run multiple tests in Pytest?

Run Multiple Tests From a Specific File and Multiple Files To run all the tests from all the files in the folder and subfolders we need to just run the pytest command. This will run all the filenames starting with test_ and the filenames ending with _test in that folder and subfolders under that folder.


2 Answers

Usually you want the browser to close between tests so that you start each test with a clean cache, localStorage, history database, etc. Closing the browser between tests does slow down the tests, but it saves in debug time because a test doesn't interact with the browser cache and history of a previous test.

like image 60
dave Avatar answered Sep 24 '22 10:09

dave


Initialize the firefox driver in __init__:

class HomePageTest(unittest.TestCase):
    def __init__(self):
        self.driver = webdriver.Firefox()
        self.driver.implicitly_wait(30)
        self.base_url = "https://somewebsite.com"
        self.verificationErrors = []

    ...

    def tearDown(self):
        self.driver.quit()
like image 40
schlamar Avatar answered Sep 21 '22 10:09

schlamar