【问题标题】:How to run multiple selenium web driver test case methods sequentially in python如何在 python 中按顺序运行多个 selenium web 驱动程序测试用例方法
【发布时间】:2017-04-07 23:21:01
【问题描述】:

我正在使用 selenium web 驱动程序为 python 编写一个测试用例。 测试用例有多种方法,例如:

class Test(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        cls.driver1 = webdriver.Firefox()
        cls.driver1.maximize_window()

    def test_1(self):
        .....
        .....
    def test_2(self):
        ....
        ....
    @classmethod
    def tearDownClass(cls):
        cls.driver1.quit()

当我尝试从 pycharm 或终端运行文件时,它会随机执行这些方法。 我想要一个顺序执行,所以 test_1() 应该在 test_2() 之前运行。 请帮我.. 提前致谢。

【问题讨论】:

标签: python python-2.7 selenium selenium-webdriver sequential


【解决方案1】:

在单个浏览器实例中运行多个测试的语法是正确的;虽然它不按随机顺序运行测试,而是按数字或字母顺序运行测试。

例如: 类测试(unittest.TestCase):

@classmethod
def setUpClass(cls):
    .....
def test_1(self): #or def test_a
    print 'test1'

def test_3(self): #or def test_c
    print 'test2'

def test_2(self): #or def test_b
    print 'test3'
@classmethod
def tearDownClass(cls):
    .......

测试执行顺序:升序,即 test_1 (test_a) 将首先执行 > 接下来是 test_2(test_b) 然后是 test_3(test_c)....不考虑 'def test_ (self)'( em> - 字母或数字)放在代码中。

o/p: 测试_1 好的\n 测试_2.....测试3 好的\n 测试_3.....测试2 好的

【讨论】:

    【解决方案2】:

    将 cucumber 与 Selenium 一起使用,它具有 功能文件,可以在其中给出步骤定义,并且您的所有步骤都像迷你测试一样,按照编写顺序执行。它还有一种非常通俗易懂的书写步骤格式,Gherkin 格式。

    应该像魅力一样工作。

    这是一个例子

    Feature: User Placing an order via different methods
    
        Scenario: User tries to place an order for test item
            Given User is on Home Page
            When User Searches for test item
            Then Open the search item page
            And User adds the item to cart
            And User proceeds to book
    

    Here's the link

    优先考虑你的测试方法,这个优先级在方法定义之上,至少在 Java 中是这样,当你使用 Selenium 和 TestNG 时。不知道它是如何在 Python 中完成的。如果我们优先考虑这些定义,那么它们肯定会在牢记方法的优先级的情况下执行。

    【讨论】:

    • 亲爱的 Rohit Gadia 是的,我已经像你说的那样保持了顺序,但首先运行的方法是检查注销功能,因此测试失败。是的,我看到了 java 的优先功能,但我无法为 python 找到类似的功能。感谢任何方式:)
    • Cucumber 完全是题外话,你没有以任何有意义的方式回答这个问题。 OP 想要 Python,而不是 Java。
    • Cucumber 步骤定义会对这个过程有所帮助,我只是提出建议,这是一个有效的建议。
    • 要求 OP 改变他们的工作方式(例如 BDD),采用新的工具或语言,在可能有更简单和破坏性更小的替代方案时没有帮助。
    猜你喜欢
    • 2022-10-15
    • 2021-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多