我有一个场景 运行 来自我当前测试的 test1 在 java selenium 中称为 test2

I have a scenario to run a test1 from my current test called test2 in java selenium

以下是我的要求。我怎样才能在 Java Selenium 中做到这一点。请分享您的建议。

第 1 步:从 "GetInfo" 选项卡获取计数

第 2 步:然后 运行 来自当前测试 (test2) 的另一个 test1。 [测试1是另一个class 我如何 运行 这个 class]

第 3 步:再次完成 test1 后,我需要从 "getInfo" 选项卡中获取计数。

第 4 步:然后我必须比较 "GetInfo" 详细信息

我们在项目中使用了 TestNG、maven。请帮我。提前致谢。

如果我的理解是正确的,你正在从 GetInfo 选项卡中获取初始值。然后你想从你的 test2 中调用 test1,它再次进行一些处理并更新 GetInfo。最后你要比较GetInfo的值的细节。

据我所知,您不能从另一个测试中调用一个测试。但是我确实有解决方法。您需要将您在 test1 中执行的任务合并到您的 test2 中,以便 test2 现在将执行 test1 的验证以及测试 2。

示例:-

test1(){
//does some processing on getInfo
}

保存getInfo的初始值

test2(){
//does some processing on getInfo (which was done in test1)
//do the task for test 2
//get the final value of the getInfo and compare with initial test
}

您的问题与 Selenium 或 TestNG 甚至 Java 无关。你需要能够离开并审视你的问题。这只是正确构建逻辑的问题。

将所有逻辑提取到一个新的(助手)class,例如:

public class GetInfoLogic {

    public int count() {

        int count = 0;
        // get the count
        return count;
    }

    public void test1_logic() {

        // whatever is the logic for test1

        // maybe this returns some stuff?
    }
}

你的两个测试 classes 将如下所示:

public class TestOne {

    @Test
    public void test1() {

        GetInfoLogic getInfo = new GetInfoLogic();
        getInfo.test1_logic();
        // maybe do something with the returned "stuff"
    }
}

public class TestTwo {

    @Test
    public void test2() {

        GetInfoLogic getInfo = new GetInfoLogic();
        int count_before = getInfo.count();
        getInfo.test1_logic();
        int count_after = getInfo.count();

        // compare the "GetInfo" details, maybe something like:
        Assert.assertEquals(count_before, count_after);
    }
}

直接从一个测试调用另一个测试总是一个坏主意!