Pull to refresh

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

Приветствую! В этой заметке привожу пример реализации перезапуска 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); 
    }
}
Tags:
Hubs:
You can’t comment this publication because its author is not yet a full member of the community. You will be able to contact the author only after he or she has been invited by someone in the community. Until then, author’s username will be hidden by an alias.