我开发了一个 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”。
我认为这是因为我需要在运行测试之前设置测试来运行我的网络应用程序,但我不知道如何实现这一点?
在您的情况下,服务未启动。尝试这样的事情。
@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:
添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)