使用Gitlab CI和Junit测试媒体播放器

时间:2019-02-28 16:15:59

标签: java maven junit gitlab

我目前正在为可以加载视频并播放/暂停它们的应用程序编写测试。我们使用maven作为构建框架,并使用junit作为测试框架。测试代码如下:

public class UnitTest extends ApplicationTest {

  private MediaPlayer mediaPlayer1;
  @FXML
  private MediaView mediaView1;
  @FXML
  private Button playPauseButton;
  private static Media media;

  @Override
  public void start(Stage stage) throws Exception {
    try {
      Parent root = FXMLLoader.load(getClass().getResource("videoMain.fxml"));
      stage.setTitle("Vorprojekt");
      Scene scene = new Scene(root, 1300, 900);
      stage.setScene(scene);
      stage.show();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  @Before
  public void setup() throws Exception {
    try {
      media = new Media(getClass().getResource("small.mp4").toString());
      mediaPlayer1 = new MediaPlayer(media);
      mediaView1 = find("#mediaView1");
      mediaView1.setMediaPlayer(mediaPlayer1);
      playPauseButton = find("#playPauseButton");
    } catch (Exception e) {
    }
  }

  @After
  public void tearDown() {
  }

  @Test
  public void playPauseButtonTest() {
    mediaPlayer1.play();
    clickOn(playPauseButton);
    try {
      assertTrue(mediaPlayer1.getStatus().equals(Status.PAUSED));
    } catch (AssertionError ae) {
      fail("Status should be paused but is " + mediaPlayer1.getStatus());
    }

  }
}

如果我在设备上本地运行此测试,则不会出现任何问题。但是,如果我在Gitlab CI上运行它,则会引发以下错误:

  

[INFO]正在运行UnitTest [ERROR]测试运行:1,失败:1,错误:0,   已跳过:0,经过的时间:1.776 s <<<故障! -在UnitTest中[错误]   playPauseButtonTest(UnitTest)经过的时间:1.772 s <<<失败!   java.lang.AssertionError:状态应暂停,但在暂停   UnitTest.playPauseButtonTest(UnitTest.java:65)

     

[INFO] [INFO]结果:[INFO] [ERROR]失败:[ERROR]
  UnitTest.playPauseButtonTest:65状态应暂停但已暂停   [INFO] [ERROR]测试运行:1,失败:1,错误:0,跳过:0   [INFO] [ERROR]测试失败。

     

请参考/ builds / swp18 / tj18a / Code / target / surefire-reports   各个测试结果。请参考转储文件(如果存在)   [date] .dump,[date] -jvmRun [N] .dump和[date] .dumpstream。

我的.gitlab-ci.yml看起来像这样:

 maven_build:
 image: tomsontom/oracle-java8-mvn
 script:
  - cd Code
  - apt-get update && apt-get upgrade -y
  - DEBIAN_FRONTEND=noninteractive apt-get install openjfx xserver-xorg-   video-dummy xinit fonts-freefont-ttf libavformat-ffmpeg56 wget libswt-gtk-3-java firefox -y
- wget https://raw.githubusercontent.com/thewtex/docker-opengl/master/etc/X11/xorg.conf -O /etc/X11/xorg.conf
- xinit -- :0 -nolisten tcp vt$XDG_VTNR -noreset +extension GLX +extension RANDR +extension RENDER +extension XFIXES &
- DISPLAY=:0 mvn --batch-mode package
- cd target && ls -la

任何想法如何调整代码,以便该测试也可以在gitlab CI中使用?

我很感谢每一个提示,因为我已经不知道如何解决它。

如果您对此感兴趣,这是我pom.xml的重要部分。

      <plugin>
       <groupId>org.apache.maven.plugins</groupId>
       <artifactId>maven-surefire-plugin</artifactId>
       <configuration>
         <testFailureIgnore>true</testFailureIgnore>
       </configuration>
       <version>3.0.0-M3</version>
       <dependencies>
         <dependency>
           <groupId>org.apache.maven.surefire</groupId>
           <artifactId>surefire-junit47</artifactId>
           <version>3.0.0-M3</version>
         </dependency>
       </dependencies>
     </plugin>
     <plugin>
       <groupId>org.apache.maven.plugins</groupId>
       <artifactId>maven-compiler-plugin</artifactId>
       <version>3.8.0</version>
       <configuration>
         <source>8</source>
         <target>8</target>
       </configuration>
     </plugin>

1 个答案:

答案 0 :(得分:0)

解决方案: 我们有2个未解决的问题。首先,虚拟监视器的大小定义为1366x768,而用户界面的初始化大小为1300x900。因此,无法访问播放/暂停按钮。 第二个错误是,媒体播放器需要更多时间在跑步者中进行初始化。因此,在初始化媒体播放器之后,并且状态应该交换为播放之后,需要循环以等待媒体播放器交换。以下设置方法为我们解决了该问题:

    NPS_count <- function(x, prom=c(6,7), detr=seq(1:4)) {
      case_when(x %in% prom ~ 1,
                x %in% detr ~ -1,
                TRUE ~ 0)
    }


    df %>% 
  group_by(YY) %>% 
  summarise(X1 = sum(NPS_count(X1))/n(),
            X2 = sum(NPS_count(X2))/n(),
            X3 = sum(NPS_count(X3))/n())

需要这个gitlab ci:

  @Before
  public void setup() throws Exception {
    media = new Media(getClass().getResource("small.mp4").toString());
    mediaPlayer1 = new MediaPlayer(media);
    mediaPlayer1.setOnError(() -> fail("media error" + mediaPlayer1.getError().toString()));
    int retries = 0;
    while (mediaPlayer1.getStatus() != MediaPlayer.Status.READY) {
      System.out
          .println("media player not yet READY - current status: " + mediaPlayer1.getStatus());
      sleep(100);
      retries++;
      if (retries > 10) {
        fail("waited too long for media player to get READY");
      }   
    }   
    mediaView1 = find("#mediaView1");
    mediaView1.setMediaPlayer(mediaPlayer1);
    mediaPlayer1.play();
    retries = 0;
    while (mediaPlayer1.getStatus() != MediaPlayer.Status.PLAYING) {
      System.out
          .println("media player not yet PLAYING - current status: " + mediaPlayer1.getStatus());
      sleep(100);
      retries++;
      if (retries > 10) {
        fail("waited too long for media player to get PLAYING");
      }   
    }   
  }