首页 > 解决方案 > 如何在没有硒网格的情况下在多个浏览器实例中并行运行单个测试用例

问题描述

我正在建立一个框架并希望在多个浏览器实例中并行运行单个测试方法(点击 url 然后执行一些操作)(例如:通过同时打开〜5 个 chrome 浏览器实例来点击一个 url)

我之前已经能够并行运行不同的测试方法,但我想一次多次运行单个测试用例(并行)

GoogleTest.java

@Test(invocationCount=2)
public void hitUrl() throws Exception {
    WebDriver driver = getDriver();
    driver.get("https://google.com");
}

测试NG.xml

<suite thread-count="2" verbose="2" name="Gmail Suite"
    annotations="JDK" parallel="methods">

    <test name="Google_Test">
        <classes>
            <class name="big.GoogleTest">
                <methods>
                    <include name="hitUrl" />
                </methods>
            </class>
        </classes>
    </test>

我希望一次打开两个 chrome 浏览器实例,但它们一个接一个地在单个浏览器实例中运行。

标签: javamultithreadingseleniumselenium-webdrivertestng

解决方案


使用@Test(invocationCount= int Values)它将在同一浏览器中针对指定值运行您的代码。

您可以在每次要运行该类时创建一个节点,然后按test. 您还希望将并行化属性移动到<suite>节点。例如:

测试NG.xml

<suite name="ParallelTestingGoogle" verbose="1" parallel="tests" thread-count="5">
    <test name="1st">
        <classes>
            <class name="packageName.className"/>
        </classes>
    </test>
    <test name="2nd">
        <classes>
            <class name="packageName.className" />
        </classes>
    </test>
    <test name="3rd">
        <classes>
            <class name="packageName.className" />
        </classes>
    </test>
    <test name="4th">
        <classes>
            <class name="packageName.className" />
        </classes>
    </test>
    <test name="5th">
        <classes>
            <class name="packageName.className" />
        </classes>
    </test>
</suite>

在此处输入图像描述

爪哇:

public class TC1 {
    WebDriver driver;

    @Test
    public void testCaseOne() {
        // Printing Id of the thread on using which test method got executed
        System.setProperty("webdriver.chrome.driver", "your ChromeDriver path");
        driver = new ChromeDriver();
        driver.get("https://www.google.com");
    }
}

推荐阅读