在使用TestNG执行并行测试(Firefox和chrome)时遇到问题,

时间:2018-07-16 11:14:11

标签: java testng

在执行过程中,将在两个不同的线程中初始化Web驱动程序并正确执行步骤。

当一个线程完成其执行时,下一线程将停止其执行而不会引发任何错误。 我没有使用静态变量, 请帮我解决问题,我的xml如下所示。

<test name="chrometest">
    <parameter name="browser" value="firefox" />
    <classes>
        <class name="AutomationEngine.Driver" />
    </classes>
</test>

<test name="test2">
    <parameter name="browser" value="chrome" />
    <classes>
        <class name="AutomationEngine.Driver" />

    </classes>
</test>

`

Before方法中的驱动程序实例化,after方法中的driver.quit

公共类TestBase {     私有WebDriver驱动程序;

long firefox, chrome;

@Parameters("browser")
@BeforeTest
public void beforeTest(final String browser) throws Exception
{

    if (browser.equals("firefox"))
    {
        System.setProperty("webdriver.gecko.driver",
                "E:\\old laptop\\workspace\\mobile-automation\\TestMyBlood\\References\\geckodriver.exe");

    }
    else
    {
        System.setProperty("webdriver.chrome.driver",
                "C:\\Users\\divya\\Downloads\\chromedriver_win32\\chromedriver.exe");

    }
    setUpWebDriver(browser);
}

private synchronized void setUpWebDriver(final String browser)
{
    DesiredCapabilities capabilities = browser.equals("firefox") ? DesiredCapabilities.firefox()
            : DesiredCapabilities.chrome();
    capabilities.setBrowserName(browser);
    // capabilities.setPlatform(Platform.WIN10);
    driver = browser.equals("firefox") ? new FirefoxDriver(capabilities)
            : new ChromeDriver(capabilities);
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    driver.manage().window().maximize();

    printer("beforeTest : " + browser);
    Thread t = Thread.currentThread();
    if (browser.equals("firefox"))
    {

        firefox = t.getId();
    }
    else
    {
        chrome = t.getId();

    }
}

@AfterTest
public synchronized void AfterTest() throws Exception
{
    if ((Thread.currentThread().getId()) == (chrome))
    {
        System.out.println("closing driver is chrome");
        driver.quit();
    }
    else
    {
        System.out.println("closing driver is firefox");
        driver.quit();
    }

}

public WebDriver getDriver()
{
    return driver;
}

protected void printer(final String methodName)
{
    final String msg = String.format("%s() is running on Thread id [%d]", methodName,
            Thread.currentThread().getId());
    System.err.println("[DEBUGGER] :" + methodName);
    System.out.println(msg);

}

}

0 个答案:

没有答案