Need loop within the java class using TestNG -


i wrote loop if have 1 method, if have multiple method in testng scripts.

i able make work if put variable within public class, need run hostnum 2 through hostnum 50. need loop within class while using testng therefore can't use public static main.

here code please advise me can do. i'm noob :(

    package firsttestngpackage;  import org.openqa.selenium.*; import org.testng.annotations.*; import org.openqa.selenium.chrome.chromedriver;  public class test5 {      webdriver driver;      //i need loop within class!!           //this not working /*  {      }     int hostnum = x;*/      //this not working /*  (int x = 1; x <= 2; x++){         int hostnum = x;     }*/      //this working 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");     } } 

use parameters testng.xml , pass them 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);   }  } 

Comments

Popular posts from this blog

css - SVG using textPath a symbol not rendering in Firefox -

Java 8 + Maven Javadoc plugin: Error fetching URL -

order - Notification for user in user account opencart -