2

I developped a Spring Boot / Angular JS app. Now I'm trying to implement some GUI interface tests.

I tryed to use the Selenium ChromeDriver, so I added the Selenium dependency :

<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>3.4.0</version>
</dependency>

And I created my first test :

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = MyMainClass.class)
public class SeleniumTest {
    private WebDriver driver;

    @Before
    public void setup() {
        System.setProperty("webdriver.chrome.driver", "my/path/to/chomedriver");
        driver = new ChromeDriver();
    }

    @Test
    public void testTest() throws Exception {
        driver.get("https://www.google.com/");
    }
}

This works fine. But now I want to get my app pages with :

driver.get("http://localhost:8080/");

But I get an "ERR_CONNECTION_REFUSED" in the chrome browser.

I think it's because I need to set up my test to run my web app before to run the test but I don't find how to achieve this ?

JeanJacques
  • 1,754
  • 1
  • 12
  • 25

1 Answers1

5

In your case service is not started. Try something like this this.

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class SeleniumTest {
    @LocalServerPort
    private int port;
    private WebDriver driver;

    @Value("${server.contextPath}")
    private String contextPath;
    private String base;

    @Before
    public void setUp() throws Exception {
        System.setProperty("webdriver.chrome.driver", "my/path/to/chromedriver");
        driver = new ChromeDriver();
        this.base = "http://localhost:" + port;
    }

    @Test
    public void testTest() throws Exception {
        driver.get(base + contextPath);
    }
}

UPDATE:

Add the dependency

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
Enginer
  • 3,048
  • 1
  • 26
  • 22
StanislavL
  • 56,971
  • 9
  • 68
  • 98
  • Springboot doesn't seems to recognize the @SpringBootTest annotation – JeanJacques May 19 '17 at 14:41
  • Before your answer I tried to add @IntegrationTest and it seems to works, but probably it's not the right way to do !? – JeanJacques May 19 '17 at 14:46
  • The main problem is to let test run your spring boot application. I posted my way but there are some more. – StanislavL May 19 '17 at 14:51
  • You can use the Spring Boot Maven plugin to start up the container before your selenium tests run in Maven, – PaulNUK May 19 '17 at 15:35
  • @StanislavL Your answer wasn't working because my project was under an old Spring Boot version (1.2.6) I updated it to 1.5.3 and it works. Thanks ! – JeanJacques May 30 '17 at 14:19
  • I wrote an article with the latest versions of selenium and spring boot and you can find all details here: https://www.swtestacademy.com/selenium-spring-boot-cucumber-junit5-project/ – Onur Baskirt Mar 01 '22 at 05:43