简体   繁体   English

在 testNG Selenium 中并行执行测试

[英]Parallel execution of tests in testNG Selenium

I am not able to invoke the browsers in parallel, which are currently invoking one after another.我无法并行调用浏览器,这些浏览器目前正在一个接一个地调用。 Need a way to invoke the browsers in parallel tests.需要一种在并行测试中调用浏览器的方法。
NOTE: In my configuration xml file I have kept the thread count as 2.注意:在我的配置 xml 文件中,我将线程数保持为 2。

Below is my configuration file:下面是我的配置文件:

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE suite SYSTEM "testng.org/testng-1.0.dtd"; > 
<suite name="Parallel" parallel="tests" thread-count="4" > 
    <test verbose="3" name="<name>"> 
        <parameter name="platform" value ="win8"/> 
        <parameter name="browsername" value ="internet explorer"/> 
        <classes> 
            <class name="com.parallel.execution.ParallelExecution"> 
                <methods> 
                    <include name="testmethod1"/> 
                </methods> 
             </class> 
         </classes> 
     </test> 
</suite>

We have to define two attributes ' parallel ' and ' thread-count ' in simple testng.xml file.我们必须在简单的 testng.xml 文件中定义两个属性“ parallel ”和“ thread-count ”。 See below:见下文:

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Parallel Execution suite" parallel="methods" thread-count="2">
  <test name="Regression 2">
    <classes>
      <class name="com.parallel.TestParallelExecution"/>
    </classes>
  </test>
</suite>

In above, as we want the test methods to be executed parallel, we have set the parallel attribute as 'methods' and thread-count attribute will control the max number of threads to be created.在上面,由于我们希望测试方法并行执行,我们将并行属性设置为“方法”,线程计数属性将控制要创建的最大线程数。

Re-installing TestNG solved the above problem.重新安装TestNG解决了上述问题。

You have to explicitly write code to invoke the browser as part of a @Before configuration to invoke the browser every time a @Test is run .您必须明确编写代码来调用浏览器作为@Before配置的一部分,以便在每次运行 @Test 时调用浏览器。 I will specify one of the many approaches so that you get an idea.我将指定多种方法中的一种,以便您获得一个想法。

<suite name="Parallel" parallel="tests" thread-count="4" > 
    <test verbose="3" name="test1"> 
        <classes> 
            <class name="com.parallel.execution.ParallelExecution1"/>  
         </classes> 
    </test> 
    <test verbose="3" name="test2"> 
        <classes> 
            <class name="com.parallel.execution.ParallelExecution2"/>  
        </classes> 
     </test> 
</suite>

Consider a suite file with 2 tests set to run in parallel.考虑一个包含 2 个tests设置为并行运行的套件文件。 What we expect is @Test methods in ParallelExecution1 runs in first browser and @Test methods in ParallelExecution2 runs in second browser.我们期望 ParallelExecution1 中的@Test方法在第一个浏览器中运行,而 ParallelExecution2 中的@Test方法在第二个浏览器中运行。 So you need a mechanism by which you can invoke browser sessions and run your test methods.因此,您需要一种机制来调用浏览器会话并运行您的测试方法。 Enter BaseTest class.进入BaseTest类。

public abstract class BaseTest {
    protected WebDriver driver;        

    @BeforeTest
    @Parameters({"browser"}) 
    public void init(String browser) {
        // Initialize your browser here. Code below is dummy
        driver = new FF();
    }

    @AfterTest
    public void end() {
        driver.close();
        driver.quit();
    }
}

Now inherit this 'BaseTest' in both your test classes.现在在您的两个测试类中继承这个“BaseTest”。

public class ParallelExecution1 extends BaseTest {
    @Test
    public void test1() { 
    }
}


public class ParallelExecution2 extends BaseTest {
    @Test
    public void test2() { 
    }
}

Now both the tests have @BeforeTest and @AfterTest methods which will invoke the browsers.现在这两个测试都有@BeforeTest@AfterTest方法,它们将调用浏览器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM