Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/397.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java @如果执行了属于此组的测试方法,则未执行BeforeMethod/@AfterMethod(仅限yForGroups)方法_Java_Selenium_Selenium Webdriver_Testng - Fatal编程技术网

Java @如果执行了属于此组的测试方法,则未执行BeforeMethod/@AfterMethod(仅限yForGroups)方法

Java @如果执行了属于此组的测试方法,则未执行BeforeMethod/@AfterMethod(仅限yForGroups)方法,java,selenium,selenium-webdriver,testng,Java,Selenium,Selenium Webdriver,Testng,我正在运行一个测试套件,其中包含属于某个组的测试方法 下面是Selenium代码: public class BaseClass { @BeforeMethod(onlyForGroups = {"P1"}) public void bmeth1() { System.out.println("Before Method1 called"); } @BeforeMethod(onlyForGrou

我正在运行一个测试套件,其中包含属于某个组的测试方法

下面是Selenium代码:

public class BaseClass
{

    @BeforeMethod(onlyForGroups = {"P1"})
    public void bmeth1()
    {
        System.out.println("Before Method1 called");
    }

    @BeforeMethod(onlyForGroups = {"P2"})
    public void bmeth2()
    {
        System.out.println("Before Method2 called");
    }

    @BeforeMethod(onlyForGroups = {"P3"})
    public void bmeth3()
    {
        System.out.println("Before Method3 called");
    }

    @AfterMethod(onlyForGroups = {"P1"})
    public void ameth1()
    {
        System.out.println("After Method1 called");
    }

    @AfterMethod(onlyForGroups = {"P2"})
    public void ameth2()
    {
        System.out.println("After Method2 called");
    }

    @AfterMethod(onlyForGroups = {"P3"})
    public void ameth3()
    {
        System.out.println("After Method3 called");
    }

}

public class TC_003 extends BaseClass
{

    @Test(groups = {"P1"})
    public void tCase6()
    {
        System.out.println("Inside testcase 6");
    }

    @Test(groups = {"P2"})
    public void tCase7()
    {
        System.out.println("Inside testcase 7");
    }

    @Test(groups = {"P3"})
    public void tCase8()
    {
        System.out.println("Inside testcase 8");
    }

}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite" verbose="10">
<test name="Test1">
    <groups>
        <run>
            <include name=".*"/>
        </run>
    </groups>
    <classes>
        <class name="testing.TC_003"/>
    </classes>
</test>
</suite>
Inside testcase 6
Inside testcase 7
Inside testcase 8
Before Method1 called
Inside testcase 6
After Method1 called
Before Method2 called
Inside testcase 7
After Method2 called
Before Method3 called
Inside testcase 8
After Method3 called
package driversetup;

import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;



public class TestBaseClass  {
    

    @BeforeMethod(onlyForGroups = {"P1"},groups = {"P1"})
    public void bmeth1()
    {
        System.out.println("Before Method1 called");
    }

    @BeforeMethod(onlyForGroups = {"P2"},groups = {"P2"})
    public void bmeth2()
    {
        System.out.println("Before Method2 called");
    }

    @BeforeMethod(onlyForGroups = {"P3"},groups = {"P3"})
    public void bmeth3()
    {
        System.out.println("Before Method3 called");
    }

    @AfterMethod(onlyForGroups = {"P1"},groups = {"P1"})
    public void ameth1()
    {
        System.out.println("After Method1 called");
    }

    @AfterMethod(onlyForGroups = {"P2"},groups = {"P2"})
    public void ameth2()
    {
        System.out.println("After Method2 called");
    }

    @AfterMethod(onlyForGroups = {"P3"},groups = {"P3"})
    public void ameth3()
    {
        System.out.println("After Method3 called");
    }
    

}
下面是testng.xml文件:

public class BaseClass
{

    @BeforeMethod(onlyForGroups = {"P1"})
    public void bmeth1()
    {
        System.out.println("Before Method1 called");
    }

    @BeforeMethod(onlyForGroups = {"P2"})
    public void bmeth2()
    {
        System.out.println("Before Method2 called");
    }

    @BeforeMethod(onlyForGroups = {"P3"})
    public void bmeth3()
    {
        System.out.println("Before Method3 called");
    }

    @AfterMethod(onlyForGroups = {"P1"})
    public void ameth1()
    {
        System.out.println("After Method1 called");
    }

    @AfterMethod(onlyForGroups = {"P2"})
    public void ameth2()
    {
        System.out.println("After Method2 called");
    }

    @AfterMethod(onlyForGroups = {"P3"})
    public void ameth3()
    {
        System.out.println("After Method3 called");
    }

}

public class TC_003 extends BaseClass
{

    @Test(groups = {"P1"})
    public void tCase6()
    {
        System.out.println("Inside testcase 6");
    }

    @Test(groups = {"P2"})
    public void tCase7()
    {
        System.out.println("Inside testcase 7");
    }

    @Test(groups = {"P3"})
    public void tCase8()
    {
        System.out.println("Inside testcase 8");
    }

}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite" verbose="10">
<test name="Test1">
    <groups>
        <run>
            <include name=".*"/>
        </run>
    </groups>
    <classes>
        <class name="testing.TC_003"/>
    </classes>
</test>
</suite>
Inside testcase 6
Inside testcase 7
Inside testcase 8
Before Method1 called
Inside testcase 6
After Method1 called
Before Method2 called
Inside testcase 7
After Method2 called
Before Method3 called
Inside testcase 8
After Method3 called
package driversetup;

import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;



public class TestBaseClass  {
    

    @BeforeMethod(onlyForGroups = {"P1"},groups = {"P1"})
    public void bmeth1()
    {
        System.out.println("Before Method1 called");
    }

    @BeforeMethod(onlyForGroups = {"P2"},groups = {"P2"})
    public void bmeth2()
    {
        System.out.println("Before Method2 called");
    }

    @BeforeMethod(onlyForGroups = {"P3"},groups = {"P3"})
    public void bmeth3()
    {
        System.out.println("Before Method3 called");
    }

    @AfterMethod(onlyForGroups = {"P1"},groups = {"P1"})
    public void ameth1()
    {
        System.out.println("After Method1 called");
    }

    @AfterMethod(onlyForGroups = {"P2"},groups = {"P2"})
    public void ameth2()
    {
        System.out.println("After Method2 called");
    }

    @AfterMethod(onlyForGroups = {"P3"},groups = {"P3"})
    public void ameth3()
    {
        System.out.println("After Method3 called");
    }
    

}
预期输出:

public class BaseClass
{

    @BeforeMethod(onlyForGroups = {"P1"})
    public void bmeth1()
    {
        System.out.println("Before Method1 called");
    }

    @BeforeMethod(onlyForGroups = {"P2"})
    public void bmeth2()
    {
        System.out.println("Before Method2 called");
    }

    @BeforeMethod(onlyForGroups = {"P3"})
    public void bmeth3()
    {
        System.out.println("Before Method3 called");
    }

    @AfterMethod(onlyForGroups = {"P1"})
    public void ameth1()
    {
        System.out.println("After Method1 called");
    }

    @AfterMethod(onlyForGroups = {"P2"})
    public void ameth2()
    {
        System.out.println("After Method2 called");
    }

    @AfterMethod(onlyForGroups = {"P3"})
    public void ameth3()
    {
        System.out.println("After Method3 called");
    }

}

public class TC_003 extends BaseClass
{

    @Test(groups = {"P1"})
    public void tCase6()
    {
        System.out.println("Inside testcase 6");
    }

    @Test(groups = {"P2"})
    public void tCase7()
    {
        System.out.println("Inside testcase 7");
    }

    @Test(groups = {"P3"})
    public void tCase8()
    {
        System.out.println("Inside testcase 8");
    }

}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite" verbose="10">
<test name="Test1">
    <groups>
        <run>
            <include name=".*"/>
        </run>
    </groups>
    <classes>
        <class name="testing.TC_003"/>
    </classes>
</test>
</suite>
Inside testcase 6
Inside testcase 7
Inside testcase 8
Before Method1 called
Inside testcase 6
After Method1 called
Before Method2 called
Inside testcase 7
After Method2 called
Before Method3 called
Inside testcase 8
After Method3 called
package driversetup;

import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;



public class TestBaseClass  {
    

    @BeforeMethod(onlyForGroups = {"P1"},groups = {"P1"})
    public void bmeth1()
    {
        System.out.println("Before Method1 called");
    }

    @BeforeMethod(onlyForGroups = {"P2"},groups = {"P2"})
    public void bmeth2()
    {
        System.out.println("Before Method2 called");
    }

    @BeforeMethod(onlyForGroups = {"P3"},groups = {"P3"})
    public void bmeth3()
    {
        System.out.println("Before Method3 called");
    }

    @AfterMethod(onlyForGroups = {"P1"},groups = {"P1"})
    public void ameth1()
    {
        System.out.println("After Method1 called");
    }

    @AfterMethod(onlyForGroups = {"P2"},groups = {"P2"})
    public void ameth2()
    {
        System.out.println("After Method2 called");
    }

    @AfterMethod(onlyForGroups = {"P3"},groups = {"P3"})
    public void ameth3()
    {
        System.out.println("After Method3 called");
    }
    

}
测试方法已执行,但@BeforeMethod/@AfterMethod未执行。只有在testng.xml文件中包含某些组时,才会出现此问题。但是,如果我们排除某些组,或者不在testng.xml文件中使用groups标记,那么它们将被执行


正如所建议的,当前的解决方法是使用alwaysRun=true标志以及onlyForGroups标志。但是,如果我们应用此解决方法,并且如果前面的/parent config方法中存在任何SkipException,那么它将被迫执行@BeforeMethod/@AfterMethod方法,即使测试方法将被跳过。当前面的/parent-config方法失败时,也会记录类似的问题。

我不确定所有组是否都有此正则表达式,所以请尝试删除以下行:

<groups>
    <run>
        <include name=".*"/>
    </run>
</groups>

以下是原始帖子中提到的问题的解决方法:

public class BaseClass
{

    @BeforeMethod(onlyForGroups = {"P1"})
    public void bmeth1()
    {
        System.out.println("Before Method1 called");
    }

    @BeforeMethod(onlyForGroups = {"P2"})
    public void bmeth2()
    {
        System.out.println("Before Method2 called");
    }

    @BeforeMethod(onlyForGroups = {"P3"})
    public void bmeth3()
    {
        System.out.println("Before Method3 called");
    }

    @AfterMethod(onlyForGroups = {"P1"})
    public void ameth1()
    {
        System.out.println("After Method1 called");
    }

    @AfterMethod(onlyForGroups = {"P2"})
    public void ameth2()
    {
        System.out.println("After Method2 called");
    }

    @AfterMethod(onlyForGroups = {"P3"})
    public void ameth3()
    {
        System.out.println("After Method3 called");
    }

}

public class TC_003 extends BaseClass
{

    @Test(groups = {"P1"})
    public void tCase6()
    {
        System.out.println("Inside testcase 6");
    }

    @Test(groups = {"P2"})
    public void tCase7()
    {
        System.out.println("Inside testcase 7");
    }

    @Test(groups = {"P3"})
    public void tCase8()
    {
        System.out.println("Inside testcase 8");
    }

}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite" verbose="10">
<test name="Test1">
    <groups>
        <run>
            <include name=".*"/>
        </run>
    </groups>
    <classes>
        <class name="testing.TC_003"/>
    </classes>
</test>
</suite>
Inside testcase 6
Inside testcase 7
Inside testcase 8
Before Method1 called
Inside testcase 6
After Method1 called
Before Method2 called
Inside testcase 7
After Method2 called
Before Method3 called
Inside testcase 8
After Method3 called
package driversetup;

import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;



public class TestBaseClass  {
    

    @BeforeMethod(onlyForGroups = {"P1"},groups = {"P1"})
    public void bmeth1()
    {
        System.out.println("Before Method1 called");
    }

    @BeforeMethod(onlyForGroups = {"P2"},groups = {"P2"})
    public void bmeth2()
    {
        System.out.println("Before Method2 called");
    }

    @BeforeMethod(onlyForGroups = {"P3"},groups = {"P3"})
    public void bmeth3()
    {
        System.out.println("Before Method3 called");
    }

    @AfterMethod(onlyForGroups = {"P1"},groups = {"P1"})
    public void ameth1()
    {
        System.out.println("After Method1 called");
    }

    @AfterMethod(onlyForGroups = {"P2"},groups = {"P2"})
    public void ameth2()
    {
        System.out.println("After Method2 called");
    }

    @AfterMethod(onlyForGroups = {"P3"},groups = {"P3"})
    public void ameth3()
    {
        System.out.println("After Method3 called");
    }
    

}
如果我们只想执行某些组,那么我们可以排除那些我们不想执行的组,而不是对我们想执行的组使用include。
类似地,如果我们想要执行所有组,那么不要使用
,而是使用exclude并给出一些测试套件中没有的组名,例如

但是请注意,在使用上述解决方法时,它还将包括并运行所有不属于任何组的测试。因此,我们必须通过为所有我们认为不属于任何特定组的测试和配置方法分配一个默认组来应用另一种解决方法

因此,如果我们应用了上述变通方法,那么我们就不必应用原始帖子中提到的变通方法,也就是说,我们不必使用
alwaysRun=true
标志以及
OnlyForGroup
。也就是说,我们可以使用
@BeforeMethod/@AfterMethod(onlyForGroups={“P2”})
而不是
@BeforeMethod/@AfterMethod(onlyForGroups={“P2”},alwaysRun=true)


请避免回答这个问题,因为我不太清楚Group和onlyForGroups标志以及如何以及何时使用这些标志。因此,我建议使用PDHide给出的简单解决方案,因为它更清楚地解释了组和onlyForGroups标志之间的区别,以及如何和何时使用它们。

如果将onlyForGroups更改为组,那么一切都会正常运行,这是一个有趣的观察结果:

public class BaseClass
{

    @BeforeMethod(onlyForGroups = {"P1"})
    public void bmeth1()
    {
        System.out.println("Before Method1 called");
    }

    @BeforeMethod(onlyForGroups = {"P2"})
    public void bmeth2()
    {
        System.out.println("Before Method2 called");
    }

    @BeforeMethod(onlyForGroups = {"P3"})
    public void bmeth3()
    {
        System.out.println("Before Method3 called");
    }

    @AfterMethod(onlyForGroups = {"P1"})
    public void ameth1()
    {
        System.out.println("After Method1 called");
    }

    @AfterMethod(onlyForGroups = {"P2"})
    public void ameth2()
    {
        System.out.println("After Method2 called");
    }

    @AfterMethod(onlyForGroups = {"P3"})
    public void ameth3()
    {
        System.out.println("After Method3 called");
    }

}

public class TC_003 extends BaseClass
{

    @Test(groups = {"P1"})
    public void tCase6()
    {
        System.out.println("Inside testcase 6");
    }

    @Test(groups = {"P2"})
    public void tCase7()
    {
        System.out.println("Inside testcase 7");
    }

    @Test(groups = {"P3"})
    public void tCase8()
    {
        System.out.println("Inside testcase 8");
    }

}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite" verbose="10">
<test name="Test1">
    <groups>
        <run>
            <include name=".*"/>
        </run>
    </groups>
    <classes>
        <class name="testing.TC_003"/>
    </classes>
</test>
</suite>
Inside testcase 6
Inside testcase 7
Inside testcase 8
Before Method1 called
Inside testcase 6
After Method1 called
Before Method2 called
Inside testcase 7
After Method2 called
Before Method3 called
Inside testcase 8
After Method3 called
package driversetup;

import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;



public class TestBaseClass  {
    

    @BeforeMethod(onlyForGroups = {"P1"},groups = {"P1"})
    public void bmeth1()
    {
        System.out.println("Before Method1 called");
    }

    @BeforeMethod(onlyForGroups = {"P2"},groups = {"P2"})
    public void bmeth2()
    {
        System.out.println("Before Method2 called");
    }

    @BeforeMethod(onlyForGroups = {"P3"},groups = {"P3"})
    public void bmeth3()
    {
        System.out.println("Before Method3 called");
    }

    @AfterMethod(onlyForGroups = {"P1"},groups = {"P1"})
    public void ameth1()
    {
        System.out.println("After Method1 called");
    }

    @AfterMethod(onlyForGroups = {"P2"},groups = {"P2"})
    public void ameth2()
    {
        System.out.println("After Method2 called");
    }

    @AfterMethod(onlyForGroups = {"P3"},groups = {"P3"})
    public void ameth3()
    {
        System.out.println("After Method3 called");
    }
    

}
但是,当testng.xml中包含多个组时,上述组中的所有before和after方法都会在每个测试方法之前执行。因此,为了避免这种情况,您必须将组和仅组混合在一起

说明: 如果您没有在testngxml中指定group,那么将调用所有方法。但若你们提到组,那个么只有那个特定组中的方法才会被执行

这是因为如果阅读组的定义:

组此类/方法所属的组的列表

因此,如果您没有提到组或始终运行true,则不会调用该方法,因此不会调用before和after方法,因为它们不在任何组中

解决办法: 您可以将两者混合为:

public class BaseClass
{

    @BeforeMethod(onlyForGroups = {"P1"})
    public void bmeth1()
    {
        System.out.println("Before Method1 called");
    }

    @BeforeMethod(onlyForGroups = {"P2"})
    public void bmeth2()
    {
        System.out.println("Before Method2 called");
    }

    @BeforeMethod(onlyForGroups = {"P3"})
    public void bmeth3()
    {
        System.out.println("Before Method3 called");
    }

    @AfterMethod(onlyForGroups = {"P1"})
    public void ameth1()
    {
        System.out.println("After Method1 called");
    }

    @AfterMethod(onlyForGroups = {"P2"})
    public void ameth2()
    {
        System.out.println("After Method2 called");
    }

    @AfterMethod(onlyForGroups = {"P3"})
    public void ameth3()
    {
        System.out.println("After Method3 called");
    }

}

public class TC_003 extends BaseClass
{

    @Test(groups = {"P1"})
    public void tCase6()
    {
        System.out.println("Inside testcase 6");
    }

    @Test(groups = {"P2"})
    public void tCase7()
    {
        System.out.println("Inside testcase 7");
    }

    @Test(groups = {"P3"})
    public void tCase8()
    {
        System.out.println("Inside testcase 8");
    }

}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite" verbose="10">
<test name="Test1">
    <groups>
        <run>
            <include name=".*"/>
        </run>
    </groups>
    <classes>
        <class name="testing.TC_003"/>
    </classes>
</test>
</suite>
Inside testcase 6
Inside testcase 7
Inside testcase 8
Before Method1 called
Inside testcase 6
After Method1 called
Before Method2 called
Inside testcase 7
After Method2 called
Before Method3 called
Inside testcase 8
After Method3 called
package driversetup;

import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;



public class TestBaseClass  {
    

    @BeforeMethod(onlyForGroups = {"P1"},groups = {"P1"})
    public void bmeth1()
    {
        System.out.println("Before Method1 called");
    }

    @BeforeMethod(onlyForGroups = {"P2"},groups = {"P2"})
    public void bmeth2()
    {
        System.out.println("Before Method2 called");
    }

    @BeforeMethod(onlyForGroups = {"P3"},groups = {"P3"})
    public void bmeth3()
    {
        System.out.println("Before Method3 called");
    }

    @AfterMethod(onlyForGroups = {"P1"},groups = {"P1"})
    public void ameth1()
    {
        System.out.println("After Method1 called");
    }

    @AfterMethod(onlyForGroups = {"P2"},groups = {"P2"})
    public void ameth2()
    {
        System.out.println("After Method2 called");
    }

    @AfterMethod(onlyForGroups = {"P3"},groups = {"P3"})
    public void ameth3()
    {
        System.out.println("After Method3 called");
    }
    

}
或启用始终运行真

package driversetup;

import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;



public class TestBaseClass {
    

    @BeforeMethod(onlyForGroups = {"P1"},alwaysRun = true)
    public void bmeth1()
    {
        System.out.println("Before Method1 called");
    }

    @BeforeMethod(onlyForGroups = {"P2"},alwaysRun = true)
    public void bmeth2()
    {
        System.out.println("Before Method2 called");
    }

    @BeforeMethod(onlyForGroups = {"P3"},alwaysRun = true)
    public void bmeth3()
    {
        System.out.println("Before Method3 called");
    }

    @AfterMethod(onlyForGroups = {"P1"},alwaysRun = true)
    public void ameth1()
    {
        System.out.println("After Method1 called");
    }

    @AfterMethod(onlyForGroups = {"P2"},alwaysRun = true)
    public void ameth2()
    {
        System.out.println("After Method2 called");
    }

    @AfterMethod(onlyForGroups = {"P3"},alwaysRun = true)
    public void ameth3()
    {
        System.out.println("After Method3 called");
    }
    

}

这可确保调用before和after方法,但仅对正确的@test方法执行

如果我删除了groups标记或使用exclude,则如原始帖子中所述,实现了预期结果。但如果我们只想包括某些群体或所有群体,那么上述问题就出现了。目前,我已经找到了一个解决方案,我已经发布了这个问题的答案。请使用PDHide给出的解决方案…我观察到,如果我们使用第二个解决方案,即
@BeforeMethod/@AfterMethod(onlyforgroup={“P1”},alwaysRun=true)
,然后,它将强制执行@BeforeMethod/@AfterMethod,即使它前面的/parent config方法和后续的测试方法已被跳过,这与记录的现有问题类似。因此,我觉得我们应该取消第二个解决办法。让我知道你的看法。让我检查一下,然后back@SandeshSawant如何跳过这些方法?在config method@BeforeMethod中使用
抛出新的SkipException()