TestNG与硒并行执行 [英] TestNG Parallel execution with selenium

查看:54
本文介绍了TestNG与硒并行执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我需要同时在两个不同的浏览器上运行相同的一种方法,那我将如何实现呢?例如:

If i need to run same one method with two different browser at the same time then how will i implement it? For example:

public class AppTest2{

@parameters("browser")
@Test(dataProvider="loginData")
public void login(String userName , String password, String param){
  if(param.equals("firefox"){
         //do something
    }
  if(param.equals("chrome"){
        //do something else
    }
 }

}

我的testng.xml文件中的

包含:

in my testng.xml file contains:

    <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
    <suite name ="My sutie" parallel = "methods",  thread-count="5">
   <parameter name="browser"  value="firefox"/> 
   <test name ="My Test1">
     <classes>
        <class name="mq.demo.selenium.AppTest2"/>
     </classes>
    </test>
    </suite>

所以我的目标是使用两个不同的线程在两个不同的浏览器中同时运行登录方法.

So my target is to run the login method in two different browser at the same time using two different thread.

任何人都可以帮忙吗?

谢谢

推荐答案

您可以考虑使用以下类似的方法

You can consider something like the below as a possible solution

package com.rationaleemotions.stackoverflow;

import org.testng.IAlterSuiteListener;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
import org.testng.collections.Maps;
import org.testng.xml.XmlClass;
import org.testng.xml.XmlSuite;
import org.testng.xml.XmlTest;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class MultiBrowserSample {

    @Test
    @Parameters("browser")
    public void testMethod(String browser) {
        System.err.println("Browser : " + browser + " on Thread [" + Thread.currentThread().getId() + "]");
    }

    public static class MySuiteAlterer implements IAlterSuiteListener {

        @Override
        public void alter(List<XmlSuite> suites) {
            XmlSuite suite = suites.get(0);
            //Check if there was a parameter named "browserFlavors" defined at the suite
            String browserFlavors = suite.getParameter("browserFlavors");
            if (browserFlavors == null || browserFlavors.trim().isEmpty()) {
                //If no such parameter was found, then Try querying the JVM arguments to see if it contains
                //value for it. Just to ensure we don't end up in a situation wherein there's no JVM also provided
                //Lets add a default value for the JVM argument which in our case is "firefox"
                browserFlavors = System.getProperty("browserFlavors", "firefox");
            }
            String[] browsers = browserFlavors.split(",");
            List<XmlTest> xmlTests = new ArrayList<>();
            for (String browser : browsers) {
                XmlTest xmlTest = new XmlTest(suite);
                xmlTest.setName(browser + "_test");
                Map<String, String> parameters = Maps.newHashMap();
                parameters.put("browser", browser);
                xmlTest.setParameters(parameters);
                XmlClass xmlClass = new XmlClass();
                xmlClass.setName(MultiBrowserSample.class.getCanonicalName());
                xmlTest.getClasses().add(xmlClass);
                xmlTests.add(xmlTest);
            }
            suite.setTests(xmlTests);
        }
    }
}

套件xml文件如下所示

The suite xml file can look like below

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="1265_Suite" parallel="tests" verbose="2">
    <listeners>
        <listener class-name="com.rationaleemotions.stackoverflow.MultiBrowserSample$MySuiteAlterer"/>
    </listeners>
    <!--
    If the below line gets uncommented, then 3 <test> tags will be formed one for each browser flavor.
    Since its now commented, you will have to provide a value for it via the JVM argument 
    -DbrowserFlavors=firefox,chrome,ie (or) the system will default to just working with firefox
    -->
    <!--<parameter name="browserFlavors" value="firefox,chrome,ie"/>-->
</suite>

因此,如您所见,在这里我们要使用一个称为 IAlterSuiteListener 的TestNG侦听器,该实现将帮助我们在套件xml文件是动态的,套件xml文件中的< test> 标记数将直接等于通过套件级别参数 browserFlavors 指定的浏览器数(或)通过JVM参数 -DbrowserFlavors

So as you can see, here we are resorting to using a TestNG listener called IAlterSuiteListener implementation which is going to help us construct the <test> tags in the suite xml file dynamically and the number of <test> tags in the suite xml file will be directly equal to the number of browsers specified either via the suite level parameter browserFlavors (or) via the JVM argument -DbrowserFlavors

输出将如下所示

[TestNG] Running:
  /Users/krmahadevan/githome/PlayGround/testbed/src/test/resources/multi-browsers.xml
[ThreadUtil] Starting executor timeOut:2147483647ms workers:3 threadPoolSize:5
Browser : ie on Thread [13]
Browser : chrome on Thread [12]
Browser : firefox on Thread [11]
PASSED: testMethod("firefox")
PASSED: testMethod("ie")
PASSED: testMethod("chrome")

===============================================
    ie_test
    Tests run: 1, Failures: 0, Skips: 0
===============================================


===============================================
    firefox_test
    Tests run: 1, Failures: 0, Skips: 0
===============================================


===============================================
    chrome_test
    Tests run: 1, Failures: 0, Skips: 0
===============================================

===============================================
1265_Suite
Total tests run: 3, Failures: 0, Skips: 0
===============================================

这篇关于TestNG与硒并行执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆