retryAnalyzer不重新启动浏览器

时间:2019-01-10 15:04:31

标签: java selenium automation testng ui-automation

如果尝试失败,我将尝试使用重试分析器重新启动测试,但是,我需要使用@BeforeMethod和@AfterMethod才能使测试正确运行,但是当前我正在使用@Test(priority = 5,retryAnalyzer = Retry.class)。我如何重试包括之前和之后方法在内的整个测试?

public class join_game_already_logged_in extends ConditionsWebDriverFactory {
    private CreateGameSD firstGame;

    @BeforeMethod
    public void create_test()throws Exception{
        //Use the fixtures api to return the fixture ids for a certain date range
        GetFixtures getfixtures = new GetFixtures();
        //check that the correct status is returned for the fixture api
        String gameid = getfixtures.get_fixtures_between_dates();
        getfixtures.correct_status_returned_for_fixtures();

        //create a single fixture game using one of the fixture ids (gameid is the string returned, then changed into an  array to fit the create game call)
        this.firstGame = new CreateGameSD();
        this.firstGame.create_single_fixture_game(TestGames.test_single_game,gameid);
        this.firstGame.game_code();
        this.firstGame.created_status_is_returned_for_create_single_fix_game();
    }

    @Test (priority=5,retryAnalyzer = Retry.class)
    public void join_game_already_logged_in () throws Exception {
        Drivers.getDriver().get(Links.gameUrl+this.firstGame.game_code());
        Header header = new Header();
        header.guest_select_login();
        Login login = new Login();
        login.LoginAction(Accounts.web_user_username,Accounts.web_user_password,Accounts."ffa");
        leaderboardPlaceholder leaderboard= new leaderboardPlaceholder();
        GameId gameid = new GameId();
        gameid.game_id();
        //leaderboard.numberOfUsers();

        leaderboard.joinGame();
        FixturesScreen fixscreen = new FixturesScreen();
        fixscreen.four_picks_make();
        Thread.sleep(4000);
        fixscreen.submit_picks();
        PickReceipt pickreceipt = new PickReceipt();
        pickreceipt.your_in_the_game();
        pickreceipt.select_leaderboard();

        PickSelection pick = new PickSelection();
        pick.selectionVerification();
    }

    @AfterMethod
    public void delete_game()throws Exception{
        this.firstGame.delete_single_fixture_game();
        //Games are deleted upon test
    }
}

当前重试甚至没有关闭并重新打开浏览器,这非常令人沮丧

1 个答案:

答案 0 :(得分:0)

那是不对的。当发生故障并且涉及重试时,TestNG会重试@BeforeMethod@AfterMethod

这是一个演示此的示例。我使用的是TestNG 7.0.0-beta3,是截至目前的TestNG的最新版本。

import org.testng.Assert;
import org.testng.IRetryAnalyzer;
import org.testng.ITestResult;
import org.testng.Reporter;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class TestClassSample {
  private int value = 0;

  @BeforeMethod
  public void beforeMethod(ITestResult result) {
    System.err.println("beforeMethod");
    if (value++ == 2) {
      result.setAttribute("pass", true);
    }
  }

  @Test(retryAnalyzer = IRetryYourTest.class, priority = 5)
  public void testMethod() {
    ITestResult result = Reporter.getCurrentTestResult();
    if (!result.getAttributeNames().contains("pass")) {
      System.err.println("testMethod will fail because of missing attribute ");
      Assert.fail("pass attribute is missing.");
    }
    System.err.println("testMethod passed.");
  }

  @AfterMethod
  public void afterMethod() {
    System.err.println("afterMethod");
  }

  public static class IRetryYourTest implements IRetryAnalyzer {

    @Override
    public boolean retry(ITestResult result) {
      return !result.getAttributeNames().contains("pass");
    }
  }
}

这是执行输出

beforeMethod
testMethod will fail because of missing attribute 

Test ignored.
afterMethod

Test ignored.

beforeMethod
testMethod will fail because of missing attribute 

afterMethod
beforeMethod
testMethod passed.
afterMethod

===============================================
Default Suite
Total tests run: 3, Passes: 1, Failures: 0, Skips: 0, Retries: 2
===============================================


Process finished with exit code 0