Search code examples
javaseleniumtestngreportassertion

Make test cases in a sequence so I can remove multiple Login calls in TestNG


I'm looking for a way to make test cases in order so I can use login and logout one time.

@WebTest
@Test(groups = {"main_feature"} )
public void Check_Home_page () {
    flow.login();  //Inside login() we do assertion to find login was successful
    flow.ValidateProfilePic(); //Inside ValidateProfilePic() we do assertion to find picture section is available
}

we will run xml file as follows

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Smoke Result" parallel="false" thread-count="0">
<parameter name="seleniumHub" value="false" />
<parameter name="hubUrl" value="http://someurl:1144/wd/hub" />

<test name="Test Run">
    <parameter name="webPageUrl" value="http://mttesting.com" />
    <parameter name="browser" value="chrome" />
    <groups>
        <run>
            <include name="main_feature" />
        </run>
    </groups>
    <classes>
        <class name="test.HomeTest" />
    </classes>
</test>

<listeners>
    <listener class-name="selenium.DefaultCapability"></listener>
    <listener class-name="selenium.WebCapability"></listener>
    </listeners>
</suite>

Once the execution get over in the report we should be able to see like

  • Testcases Run : 2, failure : 0 Check_Home_page

  • login(passed)

  • ValidateProfilePic(passed)


Solution

  • To use one time login and logout, Simply you can use priority = 1 for testHomePage and dependsOnMethods for testProfilePicture. This way you can achieve one time login and logout. Please have a look on below code:

    Test -1 :

    @Test(priority = 1, groups = { "main_feature" })
    public void testHomePage () {
        flow.login();  //we do assertion to find login was successful
    }
    

    Test -2 :

        @Test(dependsOnMethods = { "testHomePage" }, groups = { "main_feature" })
        public void testProfilePicture () {
            flow.ValidateProfilePic(); //we do assertion to find picture section is available
    
    // Call logout function here.
        }
    

    I hope it will help.