3

I am trying to execute a selenium test case from a python file. I know that it can be done using subprocess module of python - but I want to explore the possibility of calling the testcase's functions instead.

This is my code

chrome_settings_test.py

from selenium import w ebdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
import unittest, time, re
import os, shutil, sys

from selenium.webdriver.chrome.options import Options
from selenium import webdriver

class SeleniumException(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Chrome()
        self.driver.implicitly_wait(30)


    #self.driver = nested_selenium.driver
        self.base_url = "https://www.google.co.in/"
        self.verificationErrors = []

    def test_selenium_exception(self):
        driver = self.driver
        driver.get(self.base_url + "/")
        driver.find_element_by_id("gbqfq").clear()
        driver.find_element_by_id("gbqfq").send_keys("Check this out")

    def is_element_present(self, how, what):
        try: self.driver.find_element(by=how, value=what)
        except NoSuchElementException, e: return False
        return True

    def tearDown(self):
        self.driver.quit()
        self.assertEqual([], self.verificationErrors)

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

selenium_fnexec.py

import chrome_settings_test

print "going to call"
chrome_settings_test.unittest.main() #or
#chrome_settings_test.unittest.setUp()
1
  • How would you do this with the subprocess module? Commented Dec 13, 2012 at 5:36

2 Answers 2

5

This did it

import chrome_settings_test
import unittest

unittest.main(module=chrome_settings_test)

Thanks to santiycr https://gist.github.com/4274570

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

1 Comment

Can i pass parameters to test along with test name?
0

You can instantiate the SeleniumException class if you want to be able to call it's methods directly.

import chrome_settings_test

print "going to call"
my_test = chrome_settings_test.SeleniumException()
my_test.setUp()

1 Comment

i get this error Traceback (most recent call last): File "selenium_fnexec_frm_py.py", line 4, in <module> my_test = chrome_settings_test.SeleniumException() File "C:\Python27\lib\unittest\case.py", line 191, in init (self.__class__, methodName)) ValueError: no such test method in <class 'chrome_settings_test.SeleniumException'>: runTest

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.