0

I am writing automatic tests using Java with Selenium Grid and JUnit framework and I have encountered a problem with user input. So my code looks like this:

package com.example.tests;

import com.thoughtworks.selenium.DefaultSelenium;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.Scanner;

import static org.junit.Assert.fail;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;


public class test {
    private DefaultSelenium selenium;

    @Before
    public void setUp() throws Exception {
        selenium = new DefaultSelenium("localhost", 5555, "*googlechrome", "www.google.com");
        selenium.start();
    }

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

    @After
    public void tearDown() throws Exception {
        selenium.stop();
    }

I would like to add a user input, so when user types for example "Google Chrome", the test will start with Google Chrome, when he types "Firefox", the test will start with Firefox etc. I have tried to put

Scanner in = new Scanner(System.in);
String web_browser = in.next();

somwhere in my code (in setUp method for example), but when the program starts, I can't type anything in the console. Does anyone know the solution for this?

  • Why exactly would you want an automated test that needs to be manually controlled? If you just want to automate certain tasks, you shouldn't make a unit test out of it, just create a POJO that will run the tasks that you need and give it parameters based on which tasks you want to run. – t0mppa Jul 20 '16 at 10:02
  • @t0mppa yeah, but I want to run the same test in parallel with different browsers – Michael K. Jul 20 '16 at 10:14
  • I still think putting manual work in the middle is the wrong way to go, since we're talking about automated tests. You could just have your test suite launch multiple browsers without the need for you to interact with it personally in any way. – t0mppa Jul 20 '16 at 11:10

1 Answers1

0

It's tricky dealing with System.in in the test.

I suggest that you rather read your driver preference as a system property?

String driver = System.getProperty("driver");
if (driver != null) {
  //use that driver
}
else {
  //use default driver
}

You can the launch your test like

mvn test -Ddriver=chrome

or by setting them in your IDE

Diego Martinoia
  • 4,592
  • 1
  • 17
  • 36