【问题标题】:Execute a selenium test case from another python file从另一个 python 文件执行 selenium 测试用例
【发布时间】:2012-11-30 23:12:00
【问题描述】:

我正在尝试从 python 文件执行 selenium 测试用例。 我知道可以使用 python 的 subprocess 模块来完成 - 但我想探索调用测试用例函数的可能性。

这是我的代码

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()

【问题讨论】:

  • 您将如何使用 subprocess 模块执行此操作?

标签: python selenium selenium-webdriver remote-execution


【解决方案1】:

如果你想直接调用它的方法,你可以实例化 SeleniumException 类。

import chrome_settings_test

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

【讨论】:

  • 我收到此错误 Traceback(最近一次调用最后一次):文件“selenium_fnexec_frm_py.py”,第 4 行,在 my_test = chrome_settings_test.SeleniumException() 文件“C:\Python27\lib\ unittest\case.py", line 191, in init (self.__class__, methodName)) ValueError: no such test method in : runTest
【解决方案2】:

做到了

import chrome_settings_test
import unittest

unittest.main(module=chrome_settings_test)

感谢 santiycr https://gist.github.com/4274570

【讨论】:

  • 我可以将参数和测试名称一起传递给测试吗?
猜你喜欢
  • 1970-01-01
  • 2011-12-28
  • 2023-04-05
  • 2020-01-30
  • 1970-01-01
  • 1970-01-01
  • 2020-12-10
  • 2013-10-19
  • 2022-01-03
相关资源
最近更新 更多