0

My framework consists of TestNG + Cucumber +Jenkins , I'm running the job using bat file configuration in jenkins.

My doubt is , I have a class file to launch the browser and I pass string value to if loop saying ,

if string equals "chrome" then launch the Chrome browser and soon. Is there a way to pass the chrome value from jenkins into class file ?

example :

public class launch(){

public static String browser ="chrome"

public void LaunchBrowser() throws Exception{

        if (browser.equalsIgnoreCase("chrome")) 
        {
launch chrome driver
}

} 

Now i would like to pass the static string value from jenkins ,

Help is appreciated.

Thanks in advance.

Gurwinder Singh
  • 38,557
  • 6
  • 51
  • 76
Rahul
  • 239
  • 1
  • 9
  • 21

1 Answers1

0

You can do something like below

public class Launch {
    //You would be passing the Browser flavor using -Dbrowser
    //If you don't pass any browser name, then the below logic defaults to chrome
    private static String browser =System.getProperty("browser", "chrome");

    public void LaunchBrowser() throws Exception {
        if (browser.equalsIgnoreCase("chrome")) {
            //launch chrome driver
        }
    } 
}
Krishnan Mahadevan
  • 14,121
  • 6
  • 34
  • 66
  • Hi Krishnan, Thanks for the reply , but my requirement is to send the string value from jenkins , so that any one who has the access to jenkins can change the value and start the build ? can you please let me know , if this is possible with my current framework ? – Rahul Jan 02 '17 at 10:52
  • 1
    @Rahul You can do that easily by having your job configured to accept parameters and then you can read the values of those parameters and set them into your script via maven command line options. For e.g., lets say your build job defines a parameter named "browser", then your maven command line would be like : mvn clean test -Dbrowser=$browser (For more info see here http://bayley.green-gaillard.com/blog/?p=92) – Krishnan Mahadevan Jan 02 '17 at 11:51
  • Got it !! But the problem here , am not using Maven , i run the job using a bat file , where bat is configured with TestNG XML. – Rahul Jan 02 '17 at 12:05
  • @Rahul You should be able to do the same even when using a batch file as well. The idea is to be able to export a Jenkins build parameter as a JVM argument. Once that is done, you should be able to run your code in whatever ways and the JVM argument (browser flavor in this case) would still be exposed to your code. – Krishnan Mahadevan Jan 02 '17 at 12:08
  • Thank you , if possible , can you please elaborate the same ? am new to such configurations , hence i request you to specify the steps , sorry troubling :) – Rahul Jan 03 '17 at 13:37
  • @Rahul - Take a look at this SO thread : http://stackoverflow.com/questions/18991273/how-to-use-jenkins-parameterized-builds – Krishnan Mahadevan Jan 04 '17 at 01:48
  • here is my bat file set classpath=C:\Users\Administrator\Selenium\OneAccess_C\bin;C:\Users\Administrator\Selenium\lib\* java org.testng.TestNG C:\Users\Administrator\Selenium\OneAccess_C\BuySide.xml , so where exactly i can add parameter here ? , so as per my knowledge , i need to give chrome string i.e value in jenkins and that jenkins parameter name of value in bat file ? correct if am wrong please – Rahul Jan 04 '17 at 10:50
  • Before you invoke the batch file do : export Browser=$browserFlavor and modify your batch file to refer to the above exported env variable by adding java -Dbrowser=$Browser org.testng.TestNG Here "browserFlavor" would be the name of the parameter you added to your Jenkins build – Krishnan Mahadevan Jan 04 '17 at 13:28
  • I have a question for you , suppose lets think, i have created a environment variable Browser=$browserFlavor and configured Bat file java -Dbrowser=$Browser org.testng.TestNG so on. Now if i give "chrome" in jenkins , will the string would be assiged to browserFlavor ? , because my requirement if i place Firefox in place of chrome , i want my tests to run in Firefox. – Rahul Jan 06 '17 at 07:25
  • The string will be assigned to the environment variable Browser provided your "browserFlavor" represents the String parameter in your job configuration which accepts the browser flavor on the Jenkins UI. – Krishnan Mahadevan Jan 06 '17 at 10:22
  • One last time need your help , actually i configured as per the comments , like STEP 1: Created Env Variable in System "Browser=$browserFlavor" , STEP 2: added java -Dbrowser=$Browser org.testng.TestNG this line to bat file , STEP 3 : Provided browserFlavour as name and ie as value in Jenkins , STEP 4 : static String browser = System.getenv("Browser"); , when i run jenkins , by default this is taking chrome browser even though i give "ie" as value in jenkins , could you please help me ? – Rahul Jan 12 '17 at 09:10