同一类中的依赖方法只有在包含在“testng 套件”中时才会乱序运行;运行类独立尊重依赖关系

软件测试 网络驱动程序 硒2 爪哇 测试
2022-01-24 20:41:37

我有一个使用 TestNG 注释的具有依赖方法的类

dependsOnMethods

如果我只是从包中将其作为 TestNG 测试运行,则该测试 100% 的时间运行良好。

当我将测试包含在 TestNG Suite 中时,这些方法会出现乱序。是的,我正在使用:

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

在我的 .xml 文件中。

套件中的所有其他测试都尊重方法排序并毫无问题地运行。是否有任何已知信息说明为什么会发生这种情况?

测试代码:

@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();
    }
}

这是整个 TestNG XML 文件(请注意,这是一个只有 3 个类的简单文件,如果我运行一个包含 20 个类的 XML,行为是相同的)。无论类的数量或顺序如何,roleCrud 测试总是无序地运行方法,其他所有类都运行良好:

<?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 -->
1个回答

根据GitHub 问题 yurloc 提交的解决方案,似乎 usinggroup-by-instances会阻止测试在 using 时被交错dependsOnMethods