Search code examples
javaseleniumtestng

How should I run the same test case without running the @before method from TestNG?


How should I run the same test case without running the @before method?

public class CommissionHistorySingledownloadTest extends TestBase
{
    @FindBy(id = "UserId")
    WebElement UserID;
    LoginPage loginPage;
    HomePage homepage;
    CommissionHistorySingledownload CommissionHistory;

    public CommissionHistorySingledownloadTest()
    {
        super();   
    }

    @BeforeMethod
    public void setUp()
    {
        initialization();
        loginPage = new LoginPage();
        homepage = loginPage.login(prop.getProperty("email"), prop.getProperty("password"));
        CommissionHistory = homepage.navigate();
        TestUtil.setusername();
    }



    @DataProvider(name="getusername")
    public Iterator<Object[]> getusername() {
        ArrayList<Object[]> testdata =  TestUtil.getusername();
        return testdata.iterator();
    }

    @Test(dataProvider="getusername")
    public void GrandTotal_CommissionComparision(String UserName) throws InterruptedException
    {

        {
        Double  value1= null;
        Double  value2= null;
        Double  value3 =0.00;
        Double  sum=null;
        Select user = new Select (driver.findElement(By.id("UserId")));
        String u =UserName;
        System.out.println(u);
        user.selectByValue(u);
        CommissionHistory.Common();
            /*
             * Select user = new Select (UserID); String u =""; user.selectByValue(u);
             */
        Boolean c = CommissionHistory.verifytablepresent();
        if (c==true)
        {
            value1 = CommissionHistory.totalCommission1();
            String g= CommissionHistory.verifycommissiontables();
            if (g=="0")
            {
                value2 = CommissionHistory.rowcount0();
            }   
            if (g=="1")
            {
                value2 = CommissionHistory.rowcount0();
                value3 = CommissionHistory.rowcount1();
            }
            else 
                System.out.println("g value not found");

            System.out.println("Value 2: "+value2 );
            System.out.println("Value 3 "+value3 );
            sum =value2+value3;
            System.out.println("Value 1: "+ value1);
            System.out.println("Sum of Value 2 and 3 is:  "+sum );
            Assert.assertEquals(value1, sum,"GrandTotal_CommissionComparision Match Failed");
        }
        else 
        {
            System.out.println("No data found");
        }
        }
    }

    @AfterMethod
    public void tearDown()
    {
        driver.quit();

    }

}

initialization() method is opening the browser.

@DataProvider(name="getusername") will give data from excel one by one in Iteration

So I want to run it for multiple users and it is opening the browser multiple times because of @before class.

Is there any way in which I can test the multiple data without opening the browser again and again //?


Solution

  • @BeforeMethod will run for every test in the suite. Use @BeforeClass to run the method once before all tests.

    @BeforeClass
    public void setUp() { }