Автоматический перезапуск тестов на JUnit

в 16:01, , рубрики: java

Приветствую! В этой заметке привожу пример реализации перезапуска selenide тестов из-под JUnit. Enjoy!

В отдельном кассе, назовём его RetryRule.java, описываем логику самого перезапуска. Вот она.

package ru.slava.tests;

import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;

public class RetryRule implements TestRule {
    private int retryCount;

    public RetryRule(int retryCount) {
        this.retryCount = retryCount;
    }

    @Override
    public Statement apply(final Statement base, final Description description) {
        return new Statement() {
            @Override
            public void evaluate() throws Throwable {
                Throwable caughtThrowable = null;

                // implement retry logic here
                for (int i = 0; i < retryCount; i++) {
                    try {
                        base.evaluate();
                        return;
                    } catch (Throwable t) {
                        caughtThrowable = t;
                        System.err.println(description.getDisplayName() + ": run " + (i+1) + " failed");
                    }
                }
                System.err.println(description.getDisplayName() + ": giving up after " + retryCount + " failures");
                throw caughtThrowable;
            }
        };
    }
}

В самом же классе с тестами, назовём его Testing.java, определяем вызов этого правила. Смотрим.

package ru.slava.tests;

import com.codeborne.selenide.Configuration;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.openqa.selenium.By;

import static com.codeborne.selenide.Condition.text;
import static com.codeborne.selenide.Selenide.*;
import static com.codeborne.selenide.Selenide.open;
import static com.codeborne.selenide.Condition.visible;
import static com.codeborne.selenide.CollectionCondition.size;

import org.openqa.selenium.Keys;
import org.openqa.selenium.NoSuchElementException;


public class Testing {

    @Rule
    public RetryRule rule = new RetryRule(3);

    @Before
    public void setUp() throws Exception {
        System.setProperty("webdriver.chrome.driver", "c:\selenium\chromedriver.exe");
        Configuration.browser = "chrome";
    }

    @Test
    public void TestPrivateOffice() throws Exception {
        open("http://rosreestr.ru/wps/portal/p/PrivateOffice");
        $(By.linkText("ЛИЧНЫЙ КАБИНЕТ")).click();
        $(By.linkText("Войти")).shouldBe(visible); 
    }
}

Автор: рыцарь со стволом

Источник


* - обязательные к заполнению поля


https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js