简体   繁体   English

当测试类作为“ TestNG Test”运行时,无法从testng.xml中检索参数值

[英]Unable to retrieve value of parameter from testng.xml when test class is run as 'TestNG Test'

In order to retrieve browser type(on which I want to run Selenium tests) from testng.xml , I've written code like following - 为了从testng.xml检索浏览器类型(我要在其上运行Selenium测试),我编写了如下代码:

public class TestClass {

    @BeforeClass
      public void beforeClass(ITestContext context) {
          String browser = context.getCurrentXmlTest().getParameter("browser");

          if(browser.equals("firefox")){
            driver = new FirefoxDriver();
          }
.
.
.

Following is my testng.xml - 以下是我的testng.xml

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >

<suite name="Task-Manager-Suite" verbose="10" >
    <parameter name="browser" value="firefox" />
  <test name="Test" >
    <classes>
      <class name="org.test.st.StackOverflowTest" />
    </classes>
  </test>
</suite>

When I run testng.xml , test run fine and browser is set with value mention in parameter tag in the testng.xml 当我运行testng.xml ,测试运行正常,并且在testng.xml parameter标记中设置了带有提及值的browser

But when I open another test class file by right click on it and selecting 'Run As' -> 'TestNG Test', value of browser is set to null . 但是,当我通过右键单击另一个测试类文件并选择“运行方式”->“ TestNG测试”来打开文件时, browser值设置为null It doesn't pick up value from testng.xml 它不会从testng.xml获取值

Is there anything after doing that I can run test class file and value of browser will be picked up from testng.xml ? 完成后可以运行测试类文件并且从testng.xml获取浏览器的值吗?

When I want to check tests written by me, every time I've to go to testng.xml and update class tag with the file name which I want to run. 当我想检查我编写的测试时,每次都要去testng.xml并用要运行的文件名更新class标记。

That's why I'm looking for something by which I can avoid editing testng.xml every time I want to check tests written by me. 因此,我一直在寻找可以避免每次检查我编写的测试时都编辑testng.xml的东西。

Please point me some blog or github link where this kind of problem has been handled. 请指出一些解决了此类问题的博客或github链接。

This is because when you run from test class as Run as > testng test, TestNg will create a custom xml file, in which the parameter that you have defined will not be there. 这是因为当您从测试类运行方式为>运行方式> testng测试时,TestNg将创建一个自定义xml文件,其中您定义的参数将不存在。 So to get your parameters in suite file, select your suite file and Run as> TestNG Suite. 因此,要在套件文件中获取参数,请选择套件文件,然后选择运行方式> TestNG Suite。

If you insist on running from your test class, set your Suite file as Template XML file in eclipse in Project>Properties>Template XML file. 如果您坚持要从测试类运行,请在Eclipse中的Project> Properties> Template XML文件中将Suite文件设置为Template XML文件。

Hope it helps 希望能帮助到你

I use: 我用:

@Parameters{"browser"}
public void beforeClass(@Optional("firefox") String browser) {
    // ...
}

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

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