Spring Boot GUI Testing Selenium WebDriver

谁都会走 提交于 2020-01-04 06:56:34

问题


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 ?


回答1:


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>



回答2:


Driver are executable binary files. Maven cannot help you with that as it's just a dependency repository. Currently to run selenium for example on firefox we need to write:

 System.setProperty("webdriver.gecko.driver", "driverpath/.exe");
             WebDriver driver = new FirefoxDriver();

However we have new solution that will make us get rid if the first line of code and you won't need to download the dirver binary files anymore. Its called WebDriverManager and it's a dependency that can be added using Maven pom file. This will call the driver during run time with the latest version number. All you need to write now is :

        WebDriverManager.firefoxdriver().setup();
        WebDriver driver = new FirefoxDriver();

and you need to add this dependency in the pom file

<dependency>
    <groupId>io.github.bonigarcia</groupId>
    <artifactId>webdrivermanager</artifactId>
    <version>2.2.1</version>
</dependency>


来源:https://stackoverflow.com/questions/44072400/spring-boot-gui-testing-selenium-webdriver

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!