如何读取TestNG XML中<method>下的<parameter>?

时间:2019-08-13 20:03:02

标签: xml selenium testng

我正在尝试读取和存储<value><parameter>下的<methods>的{​​{1}}。我尝试了不同的方法来读取xml,但无法读取方法和类下的参数值。

我尝试过 <class> 但是没有运气。

String parameterName = Reporter.getCurrentTestResult().getTestContext().getCurrentXmlTest().getParameter("name of the parameter");

如何读取和存储方法参数,即“ Iphone”和“三星”?

2 个答案:

答案 0 :(得分:1)

以下是显示如何获取此值的示例。 FWIW,我使用的是TestNG的最新发布版本,即7.0.0-beta7

import java.util.List;
import java.util.Optional;
import org.testng.ITestResult;
import org.testng.Reporter;
import org.testng.annotations.Test;
import org.testng.xml.XmlClass;
import org.testng.xml.XmlInclude;

public class ReadMethodParameters {

  @Test
  public void testMethod() {
    ITestResult testResult = Reporter.getCurrentTestResult();
    String value = extractParameterValue(testResult, "Phone");
    String msg =
        String.format(
            "<parameter name='%s' value='%s'/> in Test <%s>",
            "Phone", value, testResult.getTestContext().getName());
    System.err.println(msg);
  }

  private String extractParameterValue(ITestResult testResult, String parameterName) {
    final String methodName = testResult.getMethod().getMethodName();
    XmlClass xmlClass = testResult.getTestClass().getXmlClass();
    List<XmlInclude> includedMethods = xmlClass.getIncludedMethods();
    Optional<XmlInclude> currentMethod =
        includedMethods.stream()
            .filter(xmlInclude -> xmlInclude.getName().equalsIgnoreCase(methodName))
            .findFirst();
    return currentMethod
        .map(
            xmlInclude -> {
              String value = xmlInclude.getAllParameters().get(parameterName);
              if (value == null) {
                value = xmlClass.getLocalParameters().get(parameterName);
              }
              return value;
            })
        .orElse(null);
  }
}

这是套件xml文件的样子:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="57484602_Suite" verbose="2">
  <test name="57484602_Test_1">
    <classes>
      <class
        name="com.rationaleemotions.stackoverflow.qn57484602.ReadMethodParameters">
        <methods>
          <parameter name="Phone" value="iPhone"/>
          <include name="testMethod"/>
        </methods>
      </class>
    </classes>
  </test>  <test name="57484602_Test_2">
    <classes>
      <class
        name="com.rationaleemotions.stackoverflow.qn57484602.ReadMethodParameters">
        <methods>
          <parameter name="Phone" value="Samsung"/>
          <include name="testMethod"/>
        </methods>
      </class>
    </classes>
  </test>
</suite>

输出内容如下:

...
... TestNG 7.0.0-beta7 by Cédric Beust (cedric@beust.com)
...




<parameter name='Phone' value='iPhone'/> in Test <57484602_Test_1>

<parameter name='Phone' value='Samsung'/> in Test <57484602_Test_2>
PASSED: testMethod

===============================================
    57484602_Test_1
    Tests run: 1, Failures: 0, Skips: 0
===============================================

PASSED: testMethod

===============================================
    57484602_Test_2
    Tests run: 1, Failures: 0, Skips: 0
===============================================





PASSED: testMethod
PASSED: testMethod

===============================================
    57484602_Test_2
    Tests run: 2, Failures: 0, Skips: 0
===============================================


===============================================
57484602_Suite
Total tests run: 2, Passes: 2, Failures: 0, Skips: 0
===============================================


Process finished with exit code 0

更新

如果您只有一个<test>标签,其中有多个<class>(如下所示)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="57484602_Suite" verbose="2">
  <test name="57484602_Test_1">
    <classes>
      <class
        name="com.rationaleemotions.stackoverflow.qn57484602.ReadMethodParameters">
        <methods>
          <parameter name="Phone" value="iPhone"/>
          <include name="testIPhone"/>
        </methods>
      </class>
      <class
        name="com.rationaleemotions.stackoverflow.qn57484602.ReadMethodParameters">
        <methods>
          <parameter name="Phone" value="Samsung"/>
          <include name="samsungTest"/>
        </methods>
      </class>
    </classes>
  </test>
</suite>

您想读取方法级别的参数,然后按以下步骤操作。

  private String extractParameterValue(ITestResult testResult, String parameterName) {
    final String methodName = testResult.getMethod().getMethodName();
    XmlClass xmlClass = testResult.getMethod().getTestClass().getXmlClass();
    List<XmlInclude> includedMethods = xmlClass.getIncludedMethods();
    Optional<XmlInclude> currentMethod =
        includedMethods.stream()
            .filter(xmlInclude -> xmlInclude.getName().equalsIgnoreCase(methodName))
            .findFirst();
    Optional<String> result =
        currentMethod.map(
            xmlInclude -> {
              String value = xmlInclude.getAllParameters().get(parameterName);
              if (value == null) {
                value = xmlClass.getLocalParameters().get(parameterName);
              }
              return value;
            });
    if (result.isPresent()) {
      return result.get();
    }
    String value = "";
    List<XmlClass> classes =
        testResult.getTestContext().getCurrentXmlTest().getClasses().stream()
            .filter(xmlClass1 -> xmlClass1.getName().equalsIgnoreCase(xmlClass.getName()))
            .collect(Collectors.toList());
    for (XmlClass each : classes) {
      value = each.getLocalParameters().get(parameterName);
      if (value != null) {
        break;
      }
    }
    return value;
  }

答案 1 :(得分:0)

在TestNG XML中import socket from sys import argv script, file1 = argv list = [] with open(file1) as f: for line in f: x = socket.gethostbyname(line.strip()) list.append(x) with open('returned_hosts.csv', 'w') as r: r.write("\n".join(list)) 级别的参数是可能的,并且非常简单。

我在下面整理了一个示例供您考虑,但请注意,原始问题中的某些内容已被省略。这只是为了简洁起见,并强调了解决方案的核心。

从Suite XML开始:

Column A       Column B
172.217.22.142|Google.com
13.66.212.205 |Bing.com
72.30.35.9    |Yahoo.com

我已经假设这两种方法都存在于同一个类中。请注意,我们现在将<methods>嵌套为子节点,而不是自终止<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> <suite name="Test"> <test name="Woohoo"> <classes> <class name="com.kroger.ParametersDemo"> <methods> <include name="testIPhone"> <parameter name="phone" value="iPhone"/> </include> <include name="testSamsung"> <parameter name="phone" value="Samsung"/> </include> </methods> </class> </classes> </test> </suite> 标记。

然后,我们进行测试:

<include>

在使用XML布局后,我们可以使用在使用XML参数的其他任何情况下都将使用的<parameters>注释。

运行XML,我们将获得以下输出:

public class ParametersDemo {

    @Test
    @Parameters ({"phone"})
    public void testIPhone(String phone) {
        System.out.println("Phone for iPhone test is: " + phone);
    }

    @Test
    @Parameters ({"phone"})
    public void testSamsung(String phone) {
        System.out.println("Phone for Samsung test is: " + phone);
    }
}

不要从Krishnan的解决方案中脱颖而出,但我认为这将使工作更加简单。