【问题标题】:TestNG Multiple Test Class - Running other classesTestNG 多个测试类 - 运行其他类
【发布时间】:2015-12-22 09:43:46
【问题描述】:

我有一个多个测试类,我想执行 4 个测试,但是,我想执行的 4 个测试是不同类文件中的不同类。所以我想在我的多个测试类中一次调用每个类 1。因此,代码在各个类中执行,而多个 Test 类实际上只是根据优先级处理每个测试的执行。我只是不确定如何调用另一个类来执行。

我的多班:

@Test(priority = 0) //Set Priority of Test - Priority of test always starts from Zero
    public void one() {

  System.out.println("This is Test Case 1");
    }
         //Test 2
     @Test(priority = 1) // Test priority 1
    public void Two(){

  System.out.println("This is Test Case 2");
      }

我需要在 @Test 块中执行我的外部类。自动化新手,也许我的问题是我对 java 的理解?

如何传递我的参数:

public class TestNGDataProvider {

  private WebDriver driver;
 private static Logger logger1 = LogManager.getLogger("Logger1");

 @Test(dataProvider = "Authentication")
 public void testMethod1(String sUsername, String sPassword, String sMemorableWord) {

  DOMConfigurator.configure("log4j.xml");

driver = new FirefoxDriver();

  logger1.info("New Instance of firefox created");
  //Add wait to allow web elements to load

  driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

  logger1.info("Timeout Applied for 10 seconds to Allow Load of Elements");
  //Launch FireFoxDriver - Open WebPage

  driver.get("http://localhost/2010A15/");

  logger1.info("Website Launched");
  Reporter.log("Website Lauched Successfully  | "); //Main Event Logger/Report

  //Find Login Element
  driver.findElement(By.id("WEB_LoginButton")).click();

  logger1.info("Login Button Clicked");
  //Find User name Element

     driver.findElement(By.id("dijit_form_ValidationTextBox_1")).sendKeys(sUsername);

  logger1.info("Username Entered");

  //Find Password Element

  driver.findElement(By.id("dijit_form_ValidationTextBox_2")).sendKeys(sPassword);




     @DataProvider(name = "Authentication")
     public static Object[][] credentials() {
return new Object[][] { { "jane20@servicecharges.co.uk", "password1", "smith" }, { "rob23@orchard.co.uk", "password1", "smith" }, { "jeff23@hotmail.com", "password1", "smith" }}; 
 }}

【问题讨论】:

  • 那么,您有 4 个 diff 类和 4 种测试方法,并且您希望所有测试类都应该根据优先级运行所有 4 个测试用例,对吗?

标签: java selenium selenium-webdriver testng


【解决方案1】:

据我了解,您已经在单独的类文件中处理了测试逻辑并在主类文件中执行了这些逻辑。 因此,如果您有以下 2 个测试类,

public class TestLogic1
{  
   public void testMethod1()
    {
       System.out.println("Test 1 executing");
    }
}
public class TestLogic2
{  
   public void testMethod2()
    {
       System.out.println("Test 2 executing");
    }
}

然后你可以在你的执行类中调用这些测试方法,

public class TestExecute
{  
   TestLogic1 method1=new TestLogic1();
   TestLogic2 method2=new TestLogic2();

   @Test(priority=1)
   public void test1()
    {
       method1.testMethod1();
    }
@Test(priority=2)
   public void test2()
    {
       method2.testMethod2();
    }
}

编辑: 假设您在一个类中编写了 @Test 方法和 @DataProvider 并在另一个类中处理它的执行顺序。使用下面的代码让它使用来自 DataProvider 的参数。

 public class TestExecute{

TestNGDataProvider tg=new TestNGDataProvider();

@Test(dataProvider = "Authentication",dataProviderClass = TestNGDataProvider.class)
 public void test1(String login,String user,String pass) throws InterruptedException {

    tg.testMethod1(login,user,pass);
}
 }

希望这是您正在寻找的。 另外look here 了解有关 TestNG 数据提供者的更多详细信息。

【讨论】:

  • 在 MultipleTest 类中调用外部类 public class TestNGDataProvider { private WebDriver driver;私有静态记录器 logger1 = LogManager.getLogger("Logger1"); @Test(dataProvider = "Authentication") public void testMethod1(String sUsername, String sPassword, String sMemorableWord) method1.testmethod1();下划线为红色,好像存在语法问题。我错了吗?我认为这个问题与参数有关?
  • 当然,你需要实例化一个外部类的对象来访问它的方法。就像我在回答 TestLogic1 method1=new TestLogic1(); 中所做的那样,您正在迈出这一步吗?
  • 是的,我正在采取这一步:TestNGDataProvider method1=new TestNGDataProvider();
  • 好的,但是,它不起作用,因为它需要从外部类文件TestNGDataProvider传入的参数。如果我添加:method1.testMethod1(null, null, null);它将启动外部类测试,但没有参数测试将失败。如果我添加:method1.testMethod1(sUsername, sPassword, sMemorableWord);它不会工作?我如何获取要添加的参数?有什么想法吗?
  • 如果您编辑您的问题并添加数据提供者类,这将很有用。想看看你是如何传递参数的。
猜你喜欢
  • 1970-01-01
  • 2017-09-14
  • 2023-03-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-24
相关资源
最近更新 更多