我开发了一个Spring Boot/Angular JS应用程序。现在我正在尝试实现一些 GUI 界面测试。
我尝试使用Selenium ChromeDriver,所以我添加了Selenium依赖项:
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.4.0</version>
</dependency>
我创建了我的第一个测试:
@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/");
}
}
这工作正常。但是现在我想通过以下方式获取我的应用程序页面:
driver.get("http://localhost:8080/");
但是我在chrome浏览器中得到了一个"ERR_CONNECTION_REFUSED"。
我认为这是因为我需要在运行测试之前设置测试以运行我的 Web 应用程序,但我找不到如何实现这一点?
在您的情况下,服务未启动。试试这样的事情。
@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);
}
}
更新:
添加依赖项
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>