如何使Webapp在Travis CI上运行?

时间:2018-11-12 18:39:36

标签: java selenium spring-mvc travis-ci

好吧,我有一个用Java和Spring-MVC框架(和Maven)编写的基于tomcat的Web应用程序,在这里我使用Selenium来测试某些页面。

在测试之前,我有以下设置:

@BeforeClass
public static void init() {
    System.setProperty("webdriver.chrome.driver", "/usr/local/bin/chromedriver");
    webDriver = new ChromeDriver();

    webDriver.get("localhost:8080/app/login");
    webDriver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
}

如果我在本地计算机上运行该应用程序,然后运行测试,则一切正常。

问题是,如果我希望使用Selenium对应用程序进行测试,则该应用程序必须正在运行(否则,我将如何连接到localhost?)。但是如何在应用程序开始检查测试之前使Webapp在Travis CI上运行?

也许我应该使用一些第三方资源?还是可以仅使用Travis CI来完成?

我知道Heroku上有一个webapp运行程序来启动您的webapp,但是Travis是否有任何工具?

已更新。

到目前为止,我唯一的想法是在Heroku上部署并启动应用程序,然后在Selenium测试中使用已经运行的应用程序。所以在测试中就像这样:

webDriver.get("someHerokuUrl");

每次Github推送都将是这样:该应用在Heroku上自动部署,然后在Travis CI上进行了测试。

但是我觉得这是错误的方式。

我的.travis.yml配置:

language: java
jdk:
  - openjdk8
sudo: required
dist: trusty
addons: # get google-chrome-stable
  apt:
    packages:
      - google-chrome-stable
before_script:
  - "export DISPLAY=:99.0"
  - "sh -e /etc/init.d/xvfb start"
  - sleep 3 
install: 
  - wget -N https://chromedriver.storage.googleapis.com/2.43/chromedriver_linux64.zip -P ~/
  - unzip ~/chromedriver_linux64.zip -d ~/
  - rm ~/chromedriver_linux64.zip
  - sudo mv -f ~/chromedriver /usr/local/bin/
  - sudo chmod +x /usr/local/bin/chromedriver

1 个答案:

答案 0 :(得分:1)

在.travis.yml上

addons:
  chrome: stable

在您需要使用chrome headless 模式或添加XVFB插件之后。官方文档here

您可以找到完整的示例here

JUnit测试正常

package com.mycompany.app;

import static org.assertj.core.api.Assertions.assertThat;

import java.io.File;
import java.util.concurrent.TimeUnit;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.test.context.junit4.SpringRunner;

import com.github.noraui.utils.Utilities.OperatingSystem;
import com.github.noraui.utils.Utilities.SystemArchitecture;

/**
 * Unit test for
 * https://stackoverflow.com/questions/53268198/how-to-make-webapp-run-on-travis-ci.
 */
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class AppTest {

    /**
     * Specific logger
     */
    private static final Logger logger = LoggerFactory.getLogger(AppTest.class);

    @LocalServerPort
    private int port;

    private WebDriver webDriver;

    @Before
    public void init() {
        final OperatingSystem currentOperatingSystem = OperatingSystem.getCurrentOperatingSystem();
        String pathWebdriver = String.format("src/test/resources/drivers/%s/googlechrome/%s/chromedriver%s",
                currentOperatingSystem.getOperatingSystemDir(),
                SystemArchitecture.getCurrentSystemArchitecture().getSystemArchitectureName(),
                currentOperatingSystem.getSuffixBinary());
        if (!new File(pathWebdriver).setExecutable(true)) {
            logger.error("ERROR when change setExecutable on " + pathWebdriver);
        }
        System.setProperty("webdriver.chrome.driver", pathWebdriver);
    }

    @After
    public void quit() {
        this.webDriver.quit();
    }

    @Test
    public void read() {
        this.webDriver = new ChromeDriver();
        webDriver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
        webDriver.get("http://localhost:" + port + "/app/login");
        logger.info(webDriver.getPageSource());
        assertThat(webDriver.getPageSource()).isEqualTo("<html xmlns=\"http://www.w3.org/1999/xhtml\"><head></head><body>Hello stackoverflow.com questions 53268198</body></html>");
    }
}

在Travis-ci上进行跟踪:

enter image description here

您可以在github here

上找到所有这些代码。