【问题标题】:How to pass file location as a parameter in testng.xml如何在 testng.xml 中将文件位置作为参数传递
【发布时间】:2018-08-08 20:11:18
【问题描述】:

我需要在 testng.xml 中将文件位置作为参数传递。我正在开发一个框架,它接受 excel 文件(数据驱动)的位置作为 testng.xml 中的参数。下面是 testng.xml 的示例。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
<parameter name="Excel" value="C:\Users\workspace\PracticeProjects\MavenSample\src\test\resources\excel\hello.xlsx"></parameter>
  <test name="Test">
  <classes>
  <class name="com.abc.props.SampleTest11"></class>
  </classes>
  </test> <!-- Test -->
</suite> <!-- Suite -->

由于不同用户的工作空间位置会有所不同,我想了解是否有一种方法可以动态识别值而不是硬编码。如果确定了项目的基本路径位置或根目录,则剩余的 src\test\resources\excel\hello.xlsx 将保持不变。寻求有关如何做到这一点的帮助。

该项目是一个 Maven 项目,如果我能以某种方式在 pom.xml 中识别项目的基本目录并将其作为值传递给 testng.xml,那也将解决我的目的。

我知道有一种方法可以通过命令行传递值,但我正在寻找上述方法。

先谢谢了!!

【问题讨论】:

  • 您的 excel 文件在项目目录中是否相同?
  • @IshitaShah:如果项目在不同的机器上下载,那么项目目录将会改变。所以我们需要改变参数路径

标签: maven testng parameter-passing pom.xml testng-eclipse


【解决方案1】:

您可以使用以下代码访问文件及其位置:

// give you current execution direcory 
String currentDir = System.getProperty("user.dir");
String fileName = currentDir+File.separator+"src"+File.separator+"test"+File.separator
+"resources"+File.separator+"excel"+File.separator+"hello.xlsx"

// File.separator this will works for ubuntu,windows and Mac.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-07-05
    • 2016-09-08
    • 2023-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多