5

I have a class with dependent methods using the TestNG annotation

dependsOnMethods

The test runs fine 100% of the time if I simply run it as a TestNG Test from the package.

When I include the test in a TestNG Suite, the methods run out of order. Yes, I am using:

<test name="Test" preserve-order="true">

in my .xml file.

Every other test in the suite respects the method ordering and run without issue. Is there any known information on why this might be occurring?

Test code:

@Test(groups={ "Administration"})
public class RoleCrudTest extends AbstractIntegrationTest
{
protected static SeleniumActionHelper action;

    @Test
    public void inactiveRole() throws Exception
    {
        SeleniumHelper helper = new SeleniumHelper();
        action = new SeleniumActionHelper(driver);

        helper.login();

        String roleUrl = navigateToUrl("role/roles.xhtml");
        driver.get(roleUrl);

        assertEquals("Role:", findElementBySelector("span.portletButtonHeader").getText());

        WebElement roleName = findElementById("roleName");
        assertFalse(roleName.isEnabled());

        WebElement deptId = findElementById("deptid");
        assertFalse(deptId.isEnabled());
    }

    @Test(dependsOnMethods = "inactiveRole")
    public void createRole() throws Exception
    {
        WebElement addButton = findElementById("add");
        addButton.click();

        waitUntilAjaxRequestCompletes();

        WebElement roleName = findElementById("roleName");
        roleName.click();
        roleName.sendKeys("AAAAAAAA");

        WebElement deptId = findElementByXpath("(//button[@type='button'])[3]");
        deptId.click();

        WebElement dept = findElementByXpath("//div[@id='department_panel']/ul/li[2]");
        dept.click();

        WebElement checkbox = findElementByXpath("//li[@id='privileges:1']/div/span/div/div");
        checkbox.click();

        Thread.sleep(1000);

        WebElement save = findElementById("save");
        save.click();

        assertEquals("Role saved successfully", findElementBySelector("div.ui-growl-message > p").getText());
    }

    @Test(dependsOnMethods = "createRole")
    public void editUndo() throws Exception
    {
        Thread.sleep(1000);

        WebElement tableSort = findElementByXpath("//th[@id='tableSection:rolesListWrapped:j_idt85']/div/span[2]");
        tableSort.click();

        Thread.sleep(1000);

        WebElement createdRole = findElementByXpath("//tbody[@id='tableSection:rolesListWrapped_data']/tr[1]/td/div");
        createdRole.click();

        Thread.sleep(1000);

        WebElement roleName = findElementById("roleName");
        roleName.click();
        roleName.clear();
        roleName.sendKeys("edited");

        WebElement deptId = findElementByXpath("(//button[@type='button'])[3]");
        deptId.click();

        WebElement dept = findElementByXpath("//div[@id='department_panel']/ul/li[3]");
        dept.click();

        WebElement checkbox = findElementByXpath("//li[@id='privileges:1']/div/span/div/div/span");
        checkbox.click();

        WebElement checkbox2 = findElementByXpath("//li[@id='privileges:0']/div/span/div/div");
        checkbox2.click();

        Thread.sleep(1000);

        WebElement undo = findElementById("cancel");
        undo.click();

        String text = findElementById("roleName").getAttribute("value");
        String oldtext = "AAAAAAAA";

        assertTrue(text.equals(oldtext));
    }

    @Test(dependsOnMethods = "editUndo")
    public void editRole() throws Exception
    {
        Thread.sleep(1000);

        WebElement createdRole = findElementByXpath("//tbody[@id='tableSection:rolesListWrapped_data']/tr[1]/td/div");
        createdRole.click();

        Thread.sleep(1000);

        WebElement roleName = findElementById("roleName");
        roleName.click();
        roleName.clear();
        roleName.sendKeys("AAAAAAAAedited");

        WebElement deptId = findElementByXpath("(//button[@type='button'])[3]");
        deptId.click();

        WebElement dept = findElementByXpath("//div[@id='department_panel']/ul/li[3]");
        dept.click();

        WebElement checkbox = findElementByXpath("//li[@id='privileges:1']/div/span/div/div/span");
        checkbox.click();

        WebElement checkbox2 = findElementByXpath("//li[@id='privileges:0']/div/span/div/div");
        checkbox2.click();

        Thread.sleep(1000);

        WebElement save = findElementById("save");
        save.click();

        assertEquals("Role saved successfully", findElementBySelector("div.ui-growl-message > p").getText());
    }

    @Test(dependsOnMethods = "editRole")
    public void deleteRole() throws Exception
    {
        Thread.sleep(1000);

        WebElement deleteButton = findElementById("tableSection:delete");
        deleteButton.click();

        WebElement deleteConfirm = findElementById("confirmDelete:yes");
        deleteConfirm.click();

        Thread.sleep(500);

        assertEquals("Role deleted successfully", findElementBySelector("div.ui-growl-message > p").getText());

        waitUntilAjaxRequestCompletes();
    }
}

And here is the entire TestNG XML file (please note that this is a simple one with only 3 classes, the behavior is the same if I run an XML with 20 classes). No matter the amount of classes or ordering, the roleCrud test always runs the methods out of order, every other class runs fine:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="none">
  <test name="Test" preserve-order="true">
    <classes>
      <class name="com.x.c4i.view.security.RoleCreateErrorCheckTest"/>
      <class name="com.x.c4i.view.security.GroupCrudTest"/>
      <class name="com.x.c4i.view.security.RoleCrudTest"/>
    </classes>
  </test> <!-- Test -->
</suite> <!-- Suite -->
9
  • test code snippet, more of your testng.xml may help others in helping you
    – Tarun
    Commented Jul 20, 2012 at 7:06
  • test code attached
    – squeemish
    Commented Jul 20, 2012 at 13:16
  • Don't see any thing obviously wrong, can you post testng.xml file and order in which you see method execution in this class. Is the order always random for this class?
    – Tarun
    Commented Jul 23, 2012 at 18:38
  • Updated with the entire TestNG.xml file. The method execution I see is inactiveRole->editUndo->editRole->createRole. However, sometimes editUndo and editRole get switched in position
    – squeemish
    Commented Jul 23, 2012 at 20:06
  • 2
    I have reported this on GitHub github.com/cbeust/testng/issues/261. This issue github.com/cbeust/testng/issues/97 might be related too, if not duplicate. In my opinion it is a bug waiting to be fixed.
    – yurloc
    Commented Dec 18, 2014 at 16:08

1 Answer 1

2

Per the resolution of the GitHub issue yurloc filed, it appears that using group-by-instances will prevent tests from being interleaved when using dependsOnMethods.

2
  • 2
    Sigh. Try to button up an unanswered question and get a downvote with no explanation. I would hope the downvoter knows more about TestNG than I do, in which case they might have also known enough to provide a better answer.
    – c32hedge
    Commented Oct 10, 2017 at 13:26
  • 1
    4 years later.. Thanks a lot for your answer! I already had a headache from this one... puh
    – Klaus
    Commented Apr 13, 2021 at 12:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.