微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

java-使用硒的TestNG并行执行

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

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文件中包含:

    <!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>

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

谁能帮忙?

谢谢

解决方法:

您可以考虑将以下内容作为可能的解决方

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文件如下所示

<?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侦听器,该侦听器将帮助我们构造< test>套件xml文件中的标记会动态添加,并且< test>套件xml文件中的标记将直接等于通过套件级别参数browserFlavors(或通过JVM参数-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
===============================================

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐