2012-10-12 54 views
3

我是Python的新手,请耐心等待我的无知。我说,我有以下结构:如何调用python子目录中包含的selenium webdriver测试

parentDir\ 
    runTests.py 
    commonpageelements.py 
    testcases\ 
     __init__.py 
     test1.py 
     test2.py 

我想初始化的webdriver和runTests.py打开网页,然后传递参数给test1.py,test2.py等。在这个例子中,我有一个test1.py的登录测试。我也想要一个包含常见页面元素的pageelements.py文件。我将详细介绍下面的python文件。

对于commonpageelements.py我简单地定义了各种元素,如username =“inspected_name”。

我试过runTests.py如下:

from selenium import webdriver 
from testcases import * 
import common_page_elements 
import sys, unittest, re, time, os.path, logging 

class RunTests(unittest.TestCase): 

    def setUp(self): 

     self.driver = webdriver.Firefox() 

     self.driver.implicitly_wait(30) 
     self.driver.get("http://url_for_page") 

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

    def test_loginLogout(username, password): 
     test_login_logout("myusername", "mypassword") 

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

然后,在test1.py我:

class test1(username, password): 

    def test_login_logout(): 
     driver = self.driver 
     driver.get(self.base_url) 
     driver.find_element_by_id(pageelements.textfield_username).clear() 
     driver.find_element_by_id(pageelements.textfield_username).send_keys(username) 
     driver.find_element_by_id(pageelements.textfield_password).clear() 
     driver.find_element_by_id(pageelements.textfield_password).send_keys(password) 
     driver.find_element_by_name(pageelements.button_submit).click() 
     driver.find_element_by_id(pageelements.link_logout).click() 

我只是真的不是如何实现这一目标肯定。我有点在这里盲注,尝试调整,但无处可去。任何帮助将不胜感激!由于

回答

1

我想通了这一点通过重写runTests.py如下:

from selenium import webdriver 
from testcases import login 
import common_page_elements 
import sys, unittest, re, time, os.path, logging 

class runTests(unittest.TestCase): 

    def setUp(self): 
     self.driver = webdriver.Firefox()   
     self.driver.implicitly_wait(30) 
     self.base_url = "http://url_for_page" 
     self.driver.get(self.base_url) 

    def testLogin(self): 
     test1.test_login(self, "myusername", "mypassword") 

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


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


and test1.py as: 

    import common_page_elements 

    def test_login(self, username, password): 
     driver = self.driver 
     driver.get(self.base_url) 
     driver.find_element_by_id(common_page_elements.textfield_username).clear() 
     driver.find_element_by_id(common_page_elements.textfield_username).send_keys(username) 
     driver.find_element_by_id(common_page_elements.textfield_password).clear() 
     driver.find_element_by_id(common_page_elements.textfield_password).send_keys(password) 
     driver.find_element_by_name(common_page_elements.button_submit).click() 
+0

我添加了一个可能有用的答案。主要思想是它将使您的测试更好地分区,并且更易于集体执行。希望能帮助到你 :) – RocketDonkey

3

别人就能超过我所能,增加更多的价值,但有两件事情,我注意到:

  • if __name__ == '__main__'需要位于最外层的范围(否则它将保留在类中并且不会被执行)。
  • 当您导入common_page_elements时,您不会在run_tests.py中引用它。如果这是您定义诸如username之类的地方,则需要在其前面明确引用common_page_elements
  • 在您的test_loginLogout函数中,您将两个参数传递给它。我不是专家,但我从来没有见过除self以外的其他变量传递给测试用例的情况。因此,即使你重组你的代码正确地从test1.py调用函数test_login_logout,你不及格self(其将持有的setUp内容。因此,这是行不通的。
  • 如果你要运行不同测试方法模块我认为每个模块必须密封其测试用例,这意味着他们应该能够独立运行。在你的情况下,test1.py似乎没有任何必要的信息来运行你想要的报告需要的设置
  • 考虑到上述问题,您可能需要使用unittest.TestLoader().loadTestsFromModule()函数,该函数可让您加载其他测试模块并执行它们。

这是完全未经测试(可怕的双关语,我知道)的代码,但这样的事情可能会在你的情况下工作:

import logging, os.path, re, sys, time, unittest 
from testcases import test1, test2 

def main(): 
    # Create a loader object 
    test_loader = unittest.TestLoader() 

    # Add your tests 
    suite = test_loader.loadTestsFromModule(test1) 
    suite.addTests(test_loader.loadTestsFromModule(test2)) 

    # Run the tests 
    unittest.TextTestRunner(verbosity=2).run(suite) 

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

至于setUptearDown作品,我会把那些在您的测试模块本身,因此您的Test1Test可能看起来像这样:

# test1.py 

import common_page_elements as page_elements 

from selenium import webdriver 


class Test1Test(unittest.TestCase): 

    def setUp(self): 
     self.driver = webdriver.Firefox() 
     self.driver.implicitly_wait(30) 
     self.driver.get("http://url_for_page") 

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

    def testLoginLogout(self): 
     # Likely not necessary to redefine `driver` here - just use `self.driver` 
     driver = self.driver 
     # Not sure how this relates to the `get` in `setUp` 
     driver.get(self.base_url) 
     driver.find_element_by_id(page_elements.textfield_username).clear() 
     driver.find_element_by_id(page_elements.textfield_username).send_keys(username) 
     driver.find_element_by_id(page_elements.textfield_password).clear() 
     driver.find_element_by_id(page_elements.textfield_password).send_keys(password) 
     driver.find_element_by_name(page_elements.button_submit).click() 
     driver.find_element_by_id(pageelements.link_logout).click() 
相关问题