2

I already wrote a for loop if I have one method, but what if I have multiple method in my TestNG scripts.

I was able to make it work if I put a variable within the public class, but I need this to run hostnum 2 through hostnum 50. So I need a for loop within the class while using TESTNG so therefore can't use public static main.

here is my code please advise me what I can do. I'm a noob :(

    package firsttestngpackage;

import org.openqa.selenium.*;
import org.testng.annotations.*;
import org.openqa.selenium.chrome.ChromeDriver;

public class Test5 {

    WebDriver driver;

    //I NEED A FOR LOOP WITHIN THIS CLASS!!






    //THIS IS NOT WORKING
/*  for {

    }
    int hostnum = x;*/

    //THIS IS NOT WORKING
/*  for (int x = 1; x <= 2; x++){
        int hostnum = x;
    }*/

    //THIS IS WORKING BUT NO LOOP :(
    int hostnum = 2;

    @Test(priority = 1, enabled = true)
    public void method1() throws Exception {
        System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");
        driver = new ChromeDriver();
        driver.get("https://www.google.com");
    }

    @Test(priority = 2, enabled = true)
    public void method2() throws Exception {
        driver.findElement(By.id("lst-ib")).sendKeys("host" + hostnum + "_CR");
    }
}
Max Smith
  • 65
  • 1
  • 2
  • 12
  • Is this what you are referring to? : http://www.codeaffine.com/2013/04/10/running-junit-tests-repeatedly-without-loops/ Maybe mixed with a static variable and you're set? – Brandon Ling Apr 14 '15 at 20:21
  • @BrandonLing not exactly. I'm not running junit. Running my webdriver as TestNG. I will need this hostnum to be run multiple times. Think of it as loadtesting and creating multiple users. The code example above is not exactly what my script represent but I just need to get this for loop going within the class so I can use that host number within all my 11 test method (which is not showing in my sample code) – Max Smith Apr 14 '15 at 20:42
  • Okay, so what about this, and set hostnum to be a static variable, then increment it in the methods: http://stackoverflow.com/questions/26128289/testng-webdriver-how-can-i-run-same-test-case-multiple-times – Brandon Ling Apr 14 '15 at 20:49
  • @BrandonLing I have googled invocationcount and don't see it helping my case. I have 10 Test methods and most have hostnum. I will need all 10 Test method to be run for hostnum=1 then 2,3,4 and you so on. Think of it as creating 100 users going through a signup process, but it includes 10 Test methods on TestNG so I can keep each methods on my TestNG reports. Thank you for you input, if there is anything please let me know. – Max Smith Apr 14 '15 at 21:30

1 Answers1

1

Use Parameters from your testng.xml and pass them to your tests.

Your testng.xml:

<suite name="TestSuite">
    <test name="Test2">
        <parameter name="host" value="host2_CR" />
        <classes>
            <class name="SampleTest" />
        </classes>
    </test> <!-- Test2 -->
    <test name="Test3">
        <parameter name="host" value="host3_CR" />
        <classes>
            <class name="SampleTest" />
        </classes>
    </test> <!-- Test3 -->
    <!-- ... -->
    <test name="Test50">
        <parameter name="host" value="host50_CR" />
        <classes>
            <class name="SampleTest" />
        </classes>
    </test> <!-- Test50 -->
</suite> <!-- TestSuite -->

Your class:

public class SampleTest {

  WebDriver driver;
  String host;

  @Parameters({"host"})
  @BeforeTest
  public void beforeTest(String host) {
    this.host = host;
  }

  @Test(priority = 1, enabled = true)
  public void method1() throws Exception {
    System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");
    driver = new ChromeDriver();
    driver.get("https://www.google.com");
  }

  @Test(priority = 2, enabled = true)
  public void method2() throws Exception {
    driver.findElement(By.id("lst-ib")).sendKeys(this.host);
  }

}
peetya
  • 3,578
  • 2
  • 16
  • 30
  • Thank you peetya but this is not exactly what i'm looking for. This will basically do host 2, host 3, and host 4 for the test method 1, and do host 2, host 3, and host 4 for test method 2, and then host 2, host 3, host 4 for test method 3, and so on... What i need is for to do host 2 to do method 1-10, then host 3 to do method 1-10, then host 4 to do method 1-10. So basically a complete loop. – Max Smith Apr 15 '15 at 15:55
  • i tried your parameter code and I might be able to use this. I found an issue though I can't get beforeTest to work. I get this error. (((org.testng.TestNGException: Method method2 requires 1 parameters but 0 were supplied in the Test annotation.... Removed 23 stack frames))) so i comment that portion out and just moved the at Parameters above at Test for method 2 and it worked. This isn't actually what I wanted. I want to do it on one browsers same session. Also what if I need to make a lot of users, I don't want to create 1000 in xml. Good stuff though! – Max Smith Apr 16 '15 at 20:40
  • Ah sorry I left a a parameter in method2. If you remove it, it will work. I edited the code too. If you don't want to fill the xml with 100 , you can create your xml programatically in runtime. For example you add only one parameter to your test (for example "100"), and it will create your xml with 100 in runtime and you don't have to care about it. You can read more on this link: http://testng.org/doc/documentation-main.html#running-testng-programmatically – peetya Apr 17 '15 at 07:05
  • Thank you peetya that worked. I will have to research this xml programmatically in runtime – Max Smith Apr 17 '15 at 19:05
  • can you take a look at this http://stackoverflow.com/questions/29719855/running-testng-programmatically-need-loop-to-create-multiple-test-automatically – Max Smith Apr 18 '15 at 16:27