Aakash paliwal
Aakash paliwal

Reputation: 83

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 //?

Upvotes: 2

Views: 491

Answers (3)

roudlek
roudlek

Reputation: 385

create a global boolean :

    private boolean executeSetUp = true;

in @BeforeTest method wich is probably called setUp() or init() :

 // your setup code then
 executeSetUp = true;

and in you test method:

        if(executeSetUp == false) {
            setUp();
        }
        //your test code here, then
        executeSetUp = false;
         

Example:

    private boolean executeSetUp = true;
@BeforeTest
public void setUp() throws IOException {
    WebDriverManager.chromedriver().setup();
    option = new ChromeOptions();
    option.addArguments("--remote-allow-origins=*");
    option.addArguments("--disable-blink-features=AutomationControlled");
    option.setPageLoadStrategy(PageLoadStrategy.EAGER);

    driver = new ChromeDriver(option);
    driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
    nespressoHomePage = new NespressoHomePage(driver);
    nesspressoCapsulesPage = new NesspressoCapsulesPage(driver);
    driver.manage().window().maximize();
    String domain = "https://www.nespresso.com/fr/en";
    executeSetUp = true;
    driver.get(domain);
}
    @Test(dataProvider = "getCapsulesAndValidQuantity", dataProviderClass = ReadCapsuleData.class)
public void addProductToCartWithValidQuantityOneByOne(String productName, String quantity) throws IOException {
    if(!executeSetUp) {
        setUp();
    }
        //remove beforeTest
        nespressoHomePage.acceptCookie();
        // reinistialiser la session(new solution to reduce ressources)
        nespressoHomePage.goToCapsulesPage();
        nesspressoCapsulesPage.addProductToCartWithValidQuantity(productName,quantity);
        nesspressoCapsulesPage.clickOnCart();
        String quantityInSpan = nesspressoCapsulesPage.getQuantityOfSelectedProductInCartSpan(productName);
        Assert.assertEquals(quantity, quantityInSpan,"Quantity in cart does not match expected value.");
        nesspressoCapsulesPage.closeCart();
        executeSetUp = false;
        shutDown();

}

Upvotes: 0

Sameera De Silva
Sameera De Silva

Reputation: 2000

You can use invocationCount feature in Testng, so here it's running 5 times in a single thread . As guy suggested replace @beforeMethod with @BeforeClass , if you are running as a Suite , better to change it to @BeforeSuite accordingly .

 @Test(invocationCount = 5, threadPoolSize = 1)
    public void GrandTotal_CommissionComparision(){

    }

Upvotes: 1

Guy
Guy

Reputation: 51009

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

@BeforeClass
public void setUp() { }

Upvotes: 1

Related Questions