如何让Webapp在Travis CI上运行? [英] How to make webapp run on Travis CI?

查看:0
本文介绍了如何让Webapp在Travis CI上运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗯,我有一个基于Tomcat的Web应用程序,它是用Java和Spring-MVC框架(以及Maven)编写的,其中我使用Selify来测试一些页面。

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

@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);
}

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

问题是,如果我希望使用Selify测试应用程序,它必须处于运行状态(否则,我如何连接到本地主机?)。但在应用程序开始检查测试之前,如何使WebApp在Travis CI上运行?

也许有一些我应该使用的第三方资源?或者仅使用Travis CI即可完成?

我知道Heroku上有WebApp-Runner可以启动您的WebApp,但有什么工具可以让Travis使用吗?

已更新。

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

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

推荐答案

.travis.yml

addons:
  chrome: stable

在您需要使用ChromeHeadless模式或添加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上的痕迹:

您可以在GitHubhere

上找到所有这些代码

这篇关于如何让Webapp在Travis CI上运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆