如何使CI / CD保存浏览器控制台(Java中,TestNG的)的屏幕截图和日志?

问题描述 投票:0回答:1

我发现如何配置贝哈特,黄瓜一些文档,但没有关于如何使它工作使用Java + TestNG的信息:https://circleci.com/docs/2.0/browser-testing/#using-screenshots-and-artifacts

我们手动配置在各试验直接使用Selenium方法之后使用:

public void afterMethod(ITestResult testResult) throws IOException, InterruptedException {
        if (testResult.getStatus() == ITestResult.FAILURE) {
            Screenshots.takeScreenshot(driver, testResult.getName());
            System.out.println("======= Test Failed =======");
            File src= ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
            try {
                FileUtils.copyFile(src, new File("test_invoices/" + this.getClass().getName()));
            } catch (IOException e) {
                System.out.println(e.getMessage());
            }
        }
    }

为了使其在圈CI工作 - 我们可能需要更新YAML配置。现在它看起来像:

version: 2
jobs:
  build:
    resource_class: large
    working_directory: ~/<company>
    parallelism: 1
    shell: /bin/bash -eo pipefail
    environment:
      CIRCLE_ARTIFACTS: /tmp/circleci-artifacts
      CIRCLE_TEST_REPORTS: /tmp/circleci-test-results
    # In CircleCI 1.0 we used a pre-configured image with a large number of languages and other packages.
    # In CircleCI 2.0 you can now specify your own image, or use one of our pre-configured images.
    # The following configuration line tells CircleCI to use the specified docker image as the runtime environment for you job.
    # We have selected a pre-built image that mirrors the build environment we use on
    # the 1.0 platform, but we recommend you choose an image more tailored to the needs
    # of each job. For more information on choosing an image (or alternatively using a
    # VM instead of a container) see https://circleci.com/docs/2.0/executor-types/
    # To see the list of pre-built images that CircleCI provides for most common languages see
    # https://circleci.com/docs/2.0/circleci-images/
    docker:
        - image: circleci/openjdk:8-jdk-node-browsers
        - image: mysql:5.7.21
          environment:
            - MYSQL_ROOT_PASSWORD=<pass>
            - MYSQL_DATABASE=<db>
    steps:    
    - checkout
    - run: mkdir -p $CIRCLE_ARTIFACTS $CIRCLE_TEST_REPORTS
    - run: git clone [email protected]:<company>/<test-folder>.git
    - restore_cache:
        keys:
        # This branch if available
        - v4-dep-{{ .Branch }}-
        # Default branch if not
        - v4-dep-master-
        # Any branch if there are none on the default branch - this should be unnecessary if you have your default branch configured correctly
        - v4-dep-
    - run: sudo apt-get install libxss1 libappindicator1 libindicator7 libappindicator3-1
    - run: wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
    - run: sudo dpkg -i ./google-chrome*.deb
    - run: sudo apt-get install -f
    - run: curl https://chromedriver.storage.googleapis.com/2.43/chromedriver_linux64.zip| gzip -dc > chromedriver
    - run: chmod +x chromedriver
    - run: sudo npm install -g ember-cli
    - run: cd frontend && rm -rf tmp/
    - run: cd frontend && npm install
    - run: cd backend && ./build no-zip
    - run: sudo apt install mysql-client
    - run: mysql -h 127.0.0.1 -u root -pubuntu circle_test < test-db.sql
    - run:
        command: backend/<company>-LATEST-BUILD/stage/opt/docker/bin/backend -Dconfig.resource=prod.conf
        background: true
    - save_cache:
        key: v4-dep-{{ .Branch }}-{{ epoch }}
        paths:
        - vendor/bundle
        - ~/virtualenvs
        - ~/.m2
        - ~/.ivy2
        - ~/.bundle
        - ~/.gradle
        - ~/.cache/bower
        - ~/.sbt
        - ~/.ivy2
        - frontend/node_modules
        - backend/target/resolution-cache
        - backend/target/streams
        - backend/project/target/resolution-cache
        - backend/project/target/streams
    - run: if (git log --format=%B -n 1 $CIRCLE_SHA1) | grep -iqF release; then cd <company name> && mvn clean && mvn test -e -X -Dwebdriver.chrome.driver=../../../chromedriver -Dsurefire.suiteXmlFiles=regression.xml; elif (git log --format=%B -n 1 $CIRCLE_SHA1) | grep -iqF debug; then cd <company name>/<testfolder> && mvn clean && mvn test -e -X -Dwebdriver.chrome.driver=../../../chromedriver -Dsurefire.suiteXmlFiles=debug.xml; else cd <test folder> && mvn clean && mvn test -e -X -Dwebdriver.chrome.driver=../../../chromedriver -Dsurefire.suiteXmlFiles=smoke.xml; fi
    - run: mkdir -p $CIRCLE_TEST_REPORTS/junit/
    - run: find . -type f -regex ".*/target/surefire-reports/.*xml" -exec cp {} $CIRCLE_TEST_REPORTS/junit/ \;
    - store_test_results:
        path: /tmp/circleci-test-results
    - store_artifacts:
        path: /tmp/circleci-artifacts
    - store_artifacts:
        path: target/universal
    - store_artifacts:
        path: /tmp/circleci-test-results
java junit continuous-integration automated-tests circleci
1个回答
0
投票

你可以看看在https://github.com/datengaertnerei/TestLinkNgIntegration我的源代码。

你必须要实现这样的自己TestNG的听众:

public class TestlinkIntegrationListener implements ITestListener {

  @Override
  public void onTestStart(ITestResult result) {
    // just to comply with the interface
  }

  @Override
  public void onTestSuccess(ITestResult result) {
     takeScreenShot();
  }

  @Override
  public void onTestFailure(ITestResult result) {
     takeScreenShot();
  }

  @Override
  public void onTestSkipped(ITestResult result) {
  }

  @Override
  public void onTestFailedButWithinSuccessPercentage(ITestResult result) {
     takeScreenShot();
  }

  @Override
  public void onStart(ITestContext context) {
    // get access to the webdriver object of your test class
  }

  @Override
  public void onFinish(ITestContext context) {

  }

  private void takeScreenShot(){
     // your code here
  }
}

你的测试类的webdriver的对象必须是可访问的。无论其声明公众有规范的名称,并通过反射把它拿来,或实现与对webdriver的一个getter的接口。

您可以使用监听器在您的CI设置有到你的测试通用访问和保存截图。这是怎样的CI / CD服务器设置他们的TestNG的JUnit的或报告。

© www.soinside.com 2019 - 2024. All rights reserved.