简体   繁体   中英

Java Selenium pararel test cases running

Can not run TC in parallel. I set up parallel TC running in testng.xml file. It opens 4 instances of browser but test cases are failed. Looks like it creates just one instance of browser session (or something like that) so it tries to do all 4 test in one instance.

How to modify my settings to run TCs in parallel successfully?

I have set up driver in the following way:

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;
    }
}

My testng.xml file is the following:

<!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>

pom file:

<?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>

My several test cases are:

package TestCases;

import MainSettings.Settings;
import org.testng.annotations.Test;

import java.io.IOException;

/**
 * Created by serhii.kaihorodov on 1/4/2016.
 */
public class TestCases_block_1
{
    public class TestCases extends Settings
    {
        @Test (priority=0)
        public void login()
        {
            mainPage.login("kay444415@ukr.net", "06011988");
        }

        @Test (priority=0)
        public void sinoptik_link_is_present1()
        {
            mainPage.login("kay444415@ukr.net", "06011988").sinoptik_link_is_present();
        }

        @Test (priority=0)
        public void autosale_link_is_present1()
        {
            mainPage.login("kay444415@ukr.net", "06011988").autosale_link_is_present();
        }
  }

Consider using thread safe variables, maybe all threads are using the same instance of WebDriver for all test and that's why is crashing. Try declaring your variables like this:

In your parent class you should have something like this so you can store several instances of WebDriver

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

Then in the TestCase class you should have a safe way to store the WebDriver object (java.lang.ThreadLocal gives us a thread safe wrapper for 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

Hope it helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM