2

I have 2 classes. (Super Class & Child Class)

Child class having 10 @Test Methods. Super Class having @AfterMethod & @ BeforeMethod.

I am running the child class in Selenium using TestNG.

If 1st or any of the test method is failing Remaining all the test are skipped.

Can you please post the related answers.

Suman
  • 21
  • 4
  • Is there any error message occurred? If yes then please let us know. – ChayanC Feb 15 '16 at 05:53
  • Getting error message as Failed Configuration, Skipped Configuration..... – Suman Feb 15 '16 at 06:46
  • Can you please see the below code.. – Suman Feb 15 '16 at 07:15
  • As per your posted error message its unable to locate element at @AfterMethod. What I suggest please check the xpath once again is it given correctly or not? – ChayanC Feb 15 '16 at 07:21
  • think that i have given wrong xpath so it failed the test case..then y it has not been continued the test execution...it showing skipped. – Suman Feb 15 '16 at 07:26

3 Answers3

0

You can add each method with try...catch Block. put your test code in try Block and if something fails it will be caught in Catch Block.

     @Test
        public void method1{
          try{
//your code including assertion
          }catch(Exception e){
        //exception message
        }
  }

Implementing Assertion is working Fine for me,

   boolean Flag = False;
//your code evaluate value of flag, if it is true test case will be passed otherwise failed and execution will be continue with other Test method

        try {
                        Assert.assertTrue("Not Working", Flag);
                    } catch (Exception ex) {
                        LOGGER.error("Exception in Method", ex);
                    }
SacTan
  • 379
  • 2
  • 5
  • 18
  • Hi Thanks for your immediate response. If we use try & catch block if some test cases failed then in the report it will not show the failed test cases number....it will show all passed. – Suman Feb 15 '16 at 05:27
  • @Suman Could you please share your code, May be you haven't put assertion in your test method. – SacTan Feb 15 '16 at 06:52
  • @Suman, I am going through your code and not understood about the verification point. So What do you want to verify from your test, at your second Test what it does, // click on transformation pack radio button in initial order page new InitialOrderPage(driver).Transformation(); Does the transformation() Method have assertion define in it?? – SacTan Feb 15 '16 at 07:41
  • I am not doing any verification in any test case. I have 4 test cases means selecting each pack in each test method & after test method it is common for all pack means payment page. – Suman Feb 15 '16 at 07:54
  • As you are selecting each pack in each test cases, how do you know that whether the correct pack got selected or not? My suggestion for this you should add Assertion with try catch Block for Each Test method and include BeforeMethod and AfterMethod in try catch Block. – SacTan Feb 15 '16 at 09:11
  • Okie, let me check with assertion. If we use try catch in the report getting all are passed.. – Suman Feb 15 '16 at 09:39
  • Sachin its getting the same error, the error is because if any thing is failing in @test it will run but if any thing is failing in BeforeMethod it will not run... – Suman Feb 15 '16 at 10:48
  • So is there any solution calling a class in a test method..because in my case i have 1000 line of code in After method.. – Suman Feb 15 '16 at 10:49
0

Not sure why does it fails for you..but below works well for me:

import org.testng.Assert;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.AfterMethod;
class Base {
    @BeforeMethod
    public void beforeMethod() {
        System.out.println("Before method");
    }

    @AfterMethod
    public void afterMethod() {
        System.out.println("After method");
    }

}
public class SO35401581 extends Base{
    @Test
    public void test1() {
        System.out.println("Run test1!!");
        Assert.fail("Fail");
    }

    @Test
    public void test2() {
        System.out.println("Run test2!!");
    }

    @Test
    public void test3() {
        System.out.println("Run test3!!");
    }
}

OUTPUT

[TestNG] Running:

      <Path>\AppData\Local\Temp\testng-eclipse-583895345\testng-customsuite.xml

    Before method
    Run test1!!
    After method
    Before method
    Run test2!!
    After method
    Before method
    Run test3!!
    After method
    PASSED: test2
    PASSED: test3
    FAILED: test1
    java.lang.AssertionError: Fail
        at org.testng.Assert.fail(Assert.java:94)
        at practise.SO35401581.test1(SO35401581.java:12)
===============================================
    Default test
    Tests run: 3, Failures: 1, Skips: 0
===============================================


===============================================
Default suite
Total tests run: 3, Failures: 1, Skips: 0
===============================================

I m using TestNG: 6.9.10 version..and trying to run the above test using eclipse-testng plugin.

Mrunal Gosar
  • 4,595
  • 13
  • 48
  • 71
0

Child Class

public class US_FranchisePartner_English extends SuperTestNG {

@Test(retryAnalyzer = Retry.class)
public void testFP_US_English()
{   


    System.out.println("***********************FranchisePartner_US_English_TransformationManager Pack***********************");

    Select country = new Select(driver.findElement(By.xpath("//select[@ng-model='selectedCountry']")));
    country.selectByIndex(7);


    Select language = new Select(driver.findElement(By.xpath("//select[@ng-model='settings.locale']")));
    language.selectByIndex(0);

    new HomePage(driver).Selectbusinessfp();

    new BasePage(driver).Continue();

    new InitialOrderPage(driver).TransformationManager();

}



@Test(retryAnalyzer = Retry.class)
public void testFP_US_Spanish()
{   

    System.out.println("***********************FranchisePartner_US_Spanish_TransformationJr.Manager Pack***********************");

    Select country = new Select(driver.findElement(By.xpath("//select[@ng-model='selectedCountry']")));
    country.selectByIndex(7);

    Select language = new Select(driver.findElement(By.xpath("//select[@ng-model='settings.locale']")));
    language.selectByIndex(1);

    // click continuar button in INICIO page
    new BasePage(driver).Continuar();

    // click on Gerente Jr. de Transformacion pack in ORDEN INICIAL
    new InitialOrderPage(driver).GerenteJrdeTransformacion();
}

@Test(retryAnalyzer = Retry.class)
public void testPC_US_English() 
{
    System.out.println("***********************PreferredCustomer_US_English_Transformation Pack***********************");

    Select country = new Select(driver.findElement(By.xpath("//select[@ng-model='selectedCountry']")));
    country.selectByIndex(7);

    Select language = new Select(driver.findElement(By.xpath("//select[@ng-model='settings.locale']")));
    language.selectByIndex(0);

    // click on preffered customer radio button
    new HomePage(driver).Selectbusinesspc();

    // click on continue button in homepage
    new BasePage(driver).Continue();

    // click on transformation pack radio button in initial order page
    new InitialOrderPage(driver).Transformation();
}

}

Parent Class

@BeforeMethod(alwaysRun=true)
public void beforeMethod() {
    System.setProperty("webdriver.chrome.driver","C:/Users/sumancb/Downloads/chromedriver_win32_19/chromedriver.exe");   
    driver = new ChromeDriver(); 

    // maximize the chrome browser
    driver.manage().window().maximize();
    // Time to wait for visible elements
    driver.manage().timeouts().implicitlyWait(20,TimeUnit.SECONDS);
    // enter the test url
    driver.get("http://enroll.ng.unicityqa.com/master/build/#/enroll/start");
}

@AfterMethod(alwaysRun=true)
public void afterMethod() {
    JavascriptExecutor IOpage2 = (JavascriptExecutor)driver;
    IOpage2.executeScript("scroll(0, 250)");

    JavascriptExecutor IOpage4 = (JavascriptExecutor)driver;
    IOpage4.executeScript("scroll(0, 250)");

    driver.findElement(By.xpath("(//button[@ng-click='searchProducts = search_all'])[2]")).click();

    JavascriptExecutor IOpage1 = (JavascriptExecutor)driver;
    IOpage1.executeScript("scroll(0, 250)");

    driver.findElement(By.xpath("(//span[text()='ADD'])[1]")).click();  

    JavascriptExecutor IOpage3 = (JavascriptExecutor)driver;
    IOpage3.executeScript("scroll(0, 250)");

    driver.findElement(By.xpath("(//span[text()='Suggesfdsgsted products'])[1]")).click();

    JavascriptExecutor IOpage43 = (JavascriptExecutor)driver;
    IOpage43.executeScript("scroll(0, 250)");  

    driver.findElement(By.xpath("(//span[text()='ADD'])[1]")).click();
    driver.close();
}
Suman
  • 21
  • 4