简体   繁体   中英

How to create and use Post-condition in selenium with TestNG

I have a TC like below:

@Test
private void DAMPTC016() {
    System.out.println("DA_MP_TC016_Verify the newly added main parent page is positioned at the location specified as set with \"Displayed After\" field of \"New Page\" form on the main page bar/\"Parent Page\" dropped down menu");
    String pageName1 = Utilities.UniqueObjectString("Pg_", 6);
    LoginPage login = new LoginPage();
    MainPage mainpage = login.open().Login(REPO.SP.getValue(), Account.ID.getValue(), Account.BLANK.getValue());
    mainpage.gotoDashboardAddPage();
    mainpage.setTextToAddPageTxtPageName(pageName1);
    mainpage.clickAddPageBtn();
    String pageName2= Utilities.UniqueObjectString("Pg_", 6);
    mainpage.gotoDashboardAddPage();
    mainpage.setTextToAddPageTxtPageName(pageName2);
    mainpage.clickAddPageBtn(); 
    String actualNextName = mainpage.getNextTagNameByIndex(2);
    String expectedName = mainpage.getNextTagNameByIndex(mainpage.getTagIndexByName(pageName1)-1);      
    Assert.assertEquals(actualNextName, expectedName, "The new page is not exactly created next to the right of \"Overview\" tag");
}

And I want to run post-condition to delete two created pages during a test case with a method:

deleteMultiPage(3);  

And this method in MainPage class. How can I use it after running the testcase?
P/s : I want to put it to separate class and call it when I need that class.

I think you might want to try @AfterMethod or else @AfterTest. But they both have their ways as per requirement.

@AfterTest : Only runs once, after all of the @Test methods have run.

@AfterMethod: The annotated method will be running after each test method.

See the TestNG annotations documentation:

[ http://testng.org/doc/documentation-main.html#annotations][1]

Coming back to your query :

And this method in MainPage class. How can I use it after runing the testcase? P/s: I want to put it to separate class and call it when I need.

We must use @AfterMethod to acheive this like :

@AfterMethod
public void tearDownTest() {
    MainPage main = new MainPage();
    main.deleteMultiPage(3);
}

Hope this helped.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM