Java Selenium pararel测试用例运行

时间:2017-05-26 22:11:44

标签: java maven selenium testing testng

无法并行运行TC。我在testng.xml文件中设置并行TC运行。它打开4个浏览器实例,但测试用例失败。看起来它只创建一个浏览器会话实例(或类似的东西),因此它尝试在一个实例中进行所有4个测试。

如何修改我的设置以成功并行运行TC?

我已按以下方式设置驱动程序:

bool AlmostEqual(double v1, double v2)
    {
        return (std::fabs(v1 - v2) < std::fabs(std::min(v1, v2)) * std::numeric_limits<double>::epsilon());
    }

我的testng.xml文件如下:

package MainSettings;

import Pages.StartPage;
//import TrackReporting.CaptureScreenShotOnFailureListener;
import TrackReporting.LoggingEventListener;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.events.EventFiringWebDriver;
import org.openqa.selenium.support.events.WebDriverEventListener;
import org.testng.annotations.*;

/**
 * Created by serhii.kaihorodov on 1/4/2016.
 */
//@Listeners(CaptureScreenShotOnFailureListener.class)
public class Settings {
    protected static WebDriver driver;
    protected String baseURL = "https://www.ukr.net";
    private static final WebDriverEventListener eventListener = new LoggingEventListener();
    protected StartPage mainPage;

    @BeforeMethod
    public void setUp()
    {
        System.setProperty("webdriver.chrome.driver", "D:\\Programming\\Java Automation\\ukr.net_test_cases-master\\chromedriver.exe");
//        driver = new EventFiringWebDriver(new FirefoxDriver()).register(eventListener);
        driver = new EventFiringWebDriver(new ChromeDriver()).register(eventListener);
        getDriver().get(baseURL);
        mainPage = new StartPage();
        driver.manage().window().maximize();
    }

    @AfterMethod
    public void tearDown()
    {
        driver.quit();
    }

    public static WebDriver getDriver()
    {
        return driver;
    }
}

pom文件:

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >

<suite name="My_auto_test" verbose="2" parallel="methods" thread-count="4">

    <test name="ukr.net">
        <classes>
            <class name="TestCases.TestCases_block_1"/>
        </classes>
    </test>

</suite>

我的几个测试用例是:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>MySel20Proj</groupId>
    <artifactId>MySel20Proj</artifactId>
    <version>1.0</version>
    <dependencies>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.3.1</version>
        </dependency>

        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.9.6</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.9.8</version>
            <scope>test</scope>
        </dependency>

    </dependencies>

    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.20</version>
                    <configuration>
                        <parallel>tests</parallel>
                        <threadCount>4</threadCount>
                        <suiteXmlFiles>
                            <suiteXmlFile>KayTC/src/testng.xml</suiteXmlFile>
                        </suiteXmlFiles>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>

1 个答案:

答案 0 :(得分:0)

考虑使用线程安全变量,也许所有线程都使用相同的WebDriver实例进行所有测试,这就是崩溃的原因。 尝试声明这样的变量:

在你的父类中你应该有这样的东西,这样你就可以存储几个WebDriver实例

private Map<String, ThreadLocal> drivers = new HashMap<String, ThreadLocal>(); 

然后在TestCase类中,您应该有一种安全的方式来存储WebDriver对象(java.lang.ThreadLocal为WebDriver提供了一个线程安全的包装器。)

ThreadLocal<WebDriver> threadedDriver = new ThreadLocal<WebDriver>();
threadedDriver.set(/* method returning instance of WebDriver*/);

drivers.put(testName, threadedDriver); //so it can be saved in memory
WebDriver driver = drivers.get(testName).get(); //this is how you get the object for regular use

希望它有所帮助!