java—在使用同一数据提供程序的两个不同类中有两个测试用例时,在执行第二个用例时,超类中的所有值都被重置为null

jucafojl  于 2021-06-27  发布在  Java
关注(0)|答案(1)|浏览(266)

目前,我有3个班
testbase->负责配置运行套件中所需的所有全局变量(例如:webdriver config)
在一个单独的类中,每个类中有2个测试用例扩展了测试库
问题在于,当第二个测试用例开始调用其数据提供程序时,通过运行testng.xml,testbase中的所有全局变量都会被重置为null
下面是实现和输出的示例

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite">
    <test thread-count="5" name="Test">
        <classes>
        <class
                name="Visual_Automation_Team.Visual_Automation_DevOps.ValidateLinks">
                <methods>
                    <include name="CheckURLValidity" />
                </methods>
            </class>
            <class
                name="Visual_Automation_Team.Visual_Automation_DevOps.ConsoleCheck">
                <methods>
                    <include name="CheckConsoleErrors" />
                </methods>
            </class>            
        </classes>
    </test> <!-- Test -->
</suite> <!-- Suite -->

testbase.class->用于测试套件配置

public class TestBase 
{
    //Those variables get reset to null with the second call of the data provider
    WebDriver driver;
    ConfigurationsController configurationController;
    ResolutionsInfo resolutionInfo;
    SetupEnvironment setupEnvironment;
    String automatedBrowser;
    ValidWebpagesContainer validPagesContainer;
    ExcludedWebpagesContainer excludedPagesContainer;
    IncludedWebpagesContainer includedPagesContainer;
    Webpage basePage;

    @BeforeTest
    public void Initialize()
    {
        System.out.println("Inside Initialize");
        configurationController = new ConfigurationsController();
        excludedPagesContainer = new ExcludedWebpagesContainer();
        includedPagesContainer = new IncludedWebpagesContainer(); .....
    }

    //Data provider that get called before executing each testcase we have
    @DataProvider(name="validLinksDP")
    public Object[] ValidLinksDataProvider()
    {   
        System.out.println("inside Data Provider");
        return validPagesContainer.getValidPages().toArray();
    }
}

第一个测试用例

public class ValidateLinks extends TestBase
{
    @Test(priority = 1, enabled = true , dataProvider = "validLinksDP")
    public void CheckURLValidity(Object currentPage) 
    {
        String currentPageUrl = ((Webpage)currentPage).getPageUrl();
        driver.navigate().to(currentPageUrl);
        System.out.println("Inside 'ValidateLinks'"); .....
}

第二个测试用例

public class ConsoleCheck extends TestBase
{   

    @Test(priority = 2, enabled = true , dataProvider = "validLinksDP")
    public void CheckConsoleErrors(Object currentPage) 
    {
        String currentPageUrl = ((Webpage)currentPage).getPageUrl();
        driver.navigate().to(currentPageUrl);
        System.out.println("Inside 'CheckConsoleErrors'"); .....
}

使用第一个测试用例(正常工作)调用数据提供程序时,请在此处输入图像描述
使用第二个测试用例(不工作)调用数据提供程序时的变量值请在此处输入图像描述

rseugnpd

rseugnpd1#

上述问题与数据提供程序无关。您需要更改testng.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite" >
    <test  name="Test1">
        <classes>
        <class
                name="Visual_Automation_Team.Visual_Automation_DevOps.ValidateLinks">
                <methods>
                    <include name="CheckURLValidity" />
                </methods>
            </class>           
        </classes>
    </test> <!-- Test -->

    <test name="Test2">
        <classes>

            <class
                name="Visual_Automation_Team.Visual_Automation_DevOps.ConsoleCheck">
                <methods>
                    <include name="CheckConsoleErrors" />
                </methods>
            </class>            
        </classes>
    </test> <!-- Test -->
</suite> <!-- Suite -->


使全局变量为静态。我不推荐这种方法。

相关问题