我可以在After Test调用中获取@Test的变量名称和方法名称吗?

时间:2018-06-28 13:33:36

标签: selenium testng

@BeforeSuite
    public void reports() {
        clearreports();
    }

    @Test(priority = 0,enabled = true)
    public void C2410949_VerifyNameIsCorrectlyDisplayedOnHomePage() throws IOException, InterruptedException, ATUTestRecorderException, APIException {
        String TestCaseId = "8789740";
        ATUTestRecorder record = new ATUTestRecorder("./video",
                Thread.currentThread().getStackTrace()[1].getMethodName(), true);
        record.start();
        launchUrl();
        startResult();      
        startTestCase(Thread.currentThread().getStackTrace()[1].getMethodName().toString());
        UserNamePage usernamePage = new UserNamePage();
        usernamePage.EnterUsername(BaseClass.username.toUpperCase());
        PasswordPage passwordPage = new PasswordPage();
        passwordPage.passwordValidator(BaseClass.password);
        Thread.sleep(30000);
        HomePage homePage = new HomePage();
        String actualNickName = homePage.WelcomeMessage.getText().toString().replaceAll("hello,", "").trim();
        System.out.println("Actual Nick Name:"+actualNickName);
        homePage.Click(homePage.SettingsNHelp);
        Thread.sleep(15000);                
        SettingsAndHelp settingandhelp = new SettingsAndHelp();
        settingandhelp.Click(settingandhelp.ChangeContactInformation);
        Thread.sleep(5000);
        SettingsAndHelp.ChangeContactInformation changeContactInformation = settingandhelp.new ChangeContactInformation();
        String expecteduserNickName = changeContactInformation.UserNickName.getText().toString()+"!";       
        System.out.println(expecteduserNickName);
        AssertVerify(actualNickName, expecteduserNickName);
        homePage.Click(homePage.Logout);
        endTestcase();
        endResult();
        updateResults(TestCaseId, "PASSED");
        record.stop();
    }
@AfterTest
public void TearDown() {
    endTestcase();
    endResult();
    driver.close();
}

以上是我的测试案例之一。在这里,我为两项感到震惊。

One is Test Rail Integration:
1. I have `testcase Id` in `@ Test`. But if test case is passed, then it is all good. But In middle if it fails, I have to fail the test case. I can move that making fail part in @afterTest but test case Id will not be available from @Test.

有什么办法可以获取我在@Test中使用的测试用例ID

2. The recording is also not working if it fails in middle as it is record.stop at the end of the test case. I can initialize and start record in @BeforeTest and Stop in @AfterTest. 

但是再次,我发现很难为视频命名约定,因为我正在将当前方法名称/测试用例ID作为视频名称。.

有人可以帮我吗?

谢谢

1 个答案:

答案 0 :(得分:0)

您可以使用ITestContext界面。它允许您存储对象,然后在任意位置检索它。它将与整个测试运行保持相关。

使用方法:

1)首先将属性设置为所需的任何值,在本例中为TestCaseId-

@Test(priority = 0,enabled = true)
 public void C2410949_VerifyNameIsCorrectlyDisplayedOnHomePage(ITestContext testContext) throws IOException, InterruptedException, ATUTestRecorderException, APIException {
 String TestCaseId = "8789740";
 testContext.setAttribute("TestCaseID", TestCaseId);
 // rest of the code

2)然后在所需的函数中对其进行检索-

@AfterTest
public void TearDown(ITestContext testContext) {
    String testCaseId = testContext.getAttribute("TestCaseID");
    endTestcase();
    endResult();
    driver.close();
}

请注意我如何在两个函数中将ITestContext testContext作为参数传递。