如何启动我的网页并使用Selenium进行测试

时间:2019-02-13 23:36:18

标签: selenium junit

我也在运行带有PostgresSQL的Spring Boot的本地计算机上安装了Selenium。 现在,我想使用Selenium运行单元测试。 一旦我将google.com用作测试源,一切都会按预期工作,但是当我要测试我的网页时,就无法打开它。

这是我的课程:

@RunWith(SpringRunner.class)
@SpringBootTest
public class LoginWebsiteTest {

    private static WebDriver driver;

    @BeforeClass
    public static void setUp() {
        WebDriverManager.chromedriver().setup();
        driver = new ChromeDriver();
        driver.manage().timeouts().implicitlyWait(90, TimeUnit.SECONDS);
    }

    @Test
    public void seleniumLoginWebsiteTest() {
        driver.get("http://localhost:9000/login");
        ....

我们可以看到我的网页在9000端口上运行,但是Google Chrome浏览器自动启动后,该网页无法显示。

我在这里到底想念什么?

如果有人可以帮助我,请

谢谢。

1 个答案:

答案 0 :(得分:1)

我找到了答案:

我一直认为它使用application.properties加载正确的端口以及其他所有内容。我要做的是:

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class LoginWebsiteTest {
    @LocalServerPort
    private int port;
    private static WebDriver driver;
    ...
    driver.get("http://localhost:" + port + "/login");

它选择任何端口,并且该端口必须用于我的网站!