0

I want make a runnable jar for test the connection on a website. I would like pass parameters with the "$java -jar myjar.jar" shell command, parameters are the pseudo and the pass I want test.

So, I have a junit sample class entirely generated via selenium (plugin firefox)

CheckLog.java

import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;
import org.junit.*;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

public class CheckLog {
    private WebDriver driver;
    private String baseUrl;
    private boolean acceptNextAlert = true;
    private StringBuffer verificationErrors = new StringBuffer();

    @Before
    public void setUp() throws Exception {
        driver = new FirefoxDriver();
        baseUrl = "http://example.com/";
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    }

    @Test
    public void testl() throws Exception {
        // some tests            
    }

    @After
    public void tearDown() throws Exception {
        driver.quit();
        String verificationErrorString = verificationErrors.toString();
        if (!"".equals(verificationErrorString)) {
            fail(verificationErrorString);
        }
    }

    private boolean isElementPresent(By by) {
        try {
            driver.findElement(by);
            return true;
        } catch (NoSuchElementException e) {
            return false;
        }
    }

    private boolean isAlertPresent() {
        try {
            driver.switchTo().alert();
            return true;
        } catch (NoAlertPresentException e) {
            return false;
        }
    }

    private String closeAlertAndGetItsText() {
        try {
            Alert alert = driver.switchTo().alert();
            String alertText = alert.getText();
            if (acceptNextAlert) {
                alert.accept();
            } else {
                alert.dismiss();
            }
            return alertText;
        } finally {
            acceptNextAlert = true;
        }
    }
}

And my Main.java file

import java.io.Console;
import java.util.Scanner;
import org.junit.runner.JUnitCore;

public class Main {

    /**
     * @param args
     */
      public static void main(String[] args){
          if (args.length == 0){
              Console console = System.console();
              String pseudo = new String(console.readLine("Pseudo : "));
              String pass = new String(console.readPassword("Pass : "));
          }
          JUnitCore jCore = new JUnitCore();
          jCore.run(CheckLog.class);
      }
}

My question is : how can I pass some parameters to a test class from my main class ? (like pseudo and pass)

~ UPLOAD ~

I would something like :

(1) - can get argument(s) from the shell command

and

(2) - can get argument(s) from the Main class (if I don't pass any argument from the shell command, the jar ask the argument(s) )

This post help me for the way (1) https://stackoverflow.com/a/19530408/2137454 . But, maybe is there a way more proper ?

Community
  • 1
  • 1
spacecodeur
  • 2,206
  • 7
  • 35
  • 71
  • Have you tried searching first: https://stackoverflow.com/search?q=[junit]+pass+parameters – SiKing May 14 '15 at 20:18
  • thanks for your answer ! However, I already searched on google and stackoverflow (but I see your link in case :) ). I don't find an example or explain for pass an argument from the shell command to a main class, and pass from the main class to the test class. Is not possible maybe ? – spacecodeur May 14 '15 at 20:51

0 Answers0