如何并行运行testng测试方法?

时间:2018-11-08 22:32:03

标签: selenium testng

我正在尝试通过使用TestNg概念并行运行测试方法,但无法正常工作。它启动了多个浏览器并为每个浏览器创建了每个线程,但是只在一个浏览器中输入了所有值。请帮帮我。浏览器对象和范围报告对象在这里是线程安全的。请找到测试方法。

这是Test类,需要并行执行,但无法正常工作。它只是启动多个浏览器,所有线程值将数据馈送到一个浏览器本身。

  package com.sonata.tests;

import java.util.Map;
import java.util.Properties;

import org.testng.SkipException;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

import com.sonata.utility.DataReader;
import com.sonata.utility.DriverCreation;
import com.sonata.pages.ComplaintPage;
import com.sonata.pages.HomePage;
import com.sonata.pages.LoginPage;

public class ComplainTest extends DriverCreation {

    private String browser;
    private String url;

    HomePage homeObject;
    LoginPage loginObject;
    ComplaintPage compObject;
    DataReader dataReader = new DataReader();

    @Parameters({ "url", "browser" })
    @BeforeMethod
    public void readProperties(String url, String browser) {
        /*
         * Properties prop = PropertiesReader.getProperty(); browser =
         * prop.getProperty("browser.name"); url = prop.getProperty("app.url");
         */
        this.browser = browser;
        this.url = url;
    }

    @Test
    public void createComplaintCaseWithDefault() {

        setTestCaseName("TC-10672");
        initExtent(testCaseName);

        if (!dataReader.isTestCaseExecute(xlsReader, testCaseName)) {
            logger.info("Test Case not executed as its run mode is NO");
            throw new SkipException("");
        }
        Object[][] testData = dataReader.getTestData(xlsReader, testCaseName);
        for (int t = 0; t < testData.length; t++) {
            initDriver(browser);
            homeObject = new HomePage(driver, report, wait, logger);
            homeObject.goToHomepge(url);

            @SuppressWarnings("unchecked")
            Map<String, String> testDataMap = (Map<String, String>) testData[t][0];
            loginObject = new LoginPage(driver, report, wait, logger);
            loginObject.login(testDataMap.get("UserName"),
                    testDataMap.get("Password"));

            compObject = new ComplaintPage(driver, report, wait, logger);
            compObject.createCase(testDataMap.get("ComplaintType"),
                    testDataMap.get("CustomerName"),
                    testDataMap.get("ComplaintCategory"));
            compObject.complaintPageVerification();
        }
    }

    @Test
    public void createComplaintCaseWithSensitive() {

        setTestCaseName("TC-10692");
        initExtent(testCaseName);

        if (!dataReader.isTestCaseExecute(xlsReader, testCaseName)) {
            logger.info("Test Case not executed as its run mode is NO");
            throw new SkipException("");
        }

        Object[][] testData = dataReader.getTestData(xlsReader, testCaseName);
        for (int t = 0; t < testData.length; t++) {
            initDriver(browser);
            homeObject = new HomePage(driver, report, wait, logger);
            homeObject.goToHomepge(url);

            @SuppressWarnings("unchecked")
            Map<String, String> testDataMap = (Map<String, String>) testData[t][0];

            loginObject = new LoginPage(driver, report, wait, logger);
            loginObject.login(testDataMap.get("UserName"),
                    testDataMap.get("Password"));

            compObject = new ComplaintPage(driver, report, wait, logger);
            compObject.createCase(testDataMap.get("ComplaintType"),
                    testDataMap.get("CustomerName"),
                    testDataMap.get("ComplaintCategory"));
            compObject.complaintPageVerification();
        }
    }

    @Test
    public void reopenComplaintFieldVerfication() {

        setTestCaseName("TC-10958");
        initExtent(testCaseName);

        if (!dataReader.isTestCaseExecute(xlsReader, testCaseName)) {
            logger.info("Test Case not executed as its run mode is NO");
            throw new SkipException("");
        }
        Object[][] testData = dataReader.getTestData(xlsReader, testCaseName);
        for (int t = 0; t < testData.length; t++) {
            initDriver(browser);
            homeObject = new HomePage(driver, report, wait, logger);
            homeObject.goToHomepge(url);

            @SuppressWarnings("unchecked")
            Map<String, String> testDataMap = (Map<String, String>) testData[t][0];
            loginObject = new LoginPage(driver, report, wait, logger);
            loginObject.login(testDataMap.get("UserName"),
                    testDataMap.get("Password"));

            compObject = new ComplaintPage(driver, report, wait, logger);
            compObject.reopenReasonFieldVerification(testDataMap
                    .get("ComplaintID"));
        }
    }

    @AfterMethod
    public void closeBrowser() {
        if (driver != null) {
            homeObject.tearDownTest();
        }
        if (report != null) {
            report.endTest();
        }
    }
}

这是Driver类,它将为每个Test方法创建驱动程序实例。

package com.sonata.utility;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;

import org.apache.log4j.Logger;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.Platform;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriverService;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;

import com.relevantcodes.extentreports.ExtentReports;
import com.relevantcodes.extentreports.LogStatus;
import com.sonata.constants.Constants;

public class DriverCreation {

    public ExtentReports report;
    public WebDriver driver;
    public DesiredCapabilities cap1;
    public WebDriverWait wait;
    protected String testCaseName;

    ExtentReport extent;
    public Logger logger = Logger.getLogger(this.getClass().toString());
    public ExcelReader xlsReader = new ExcelReader(getProjectPath()
            + Constants.XLS_FILE_PATH);

    /*
     * public DriverCreation(String browser,ExtentReports report,Logger logger){
     * this.browser = browser; this.report = report; this.logger = logger; }
     */

    public void initExtent(String reportName) {
        extent = new ExtentReport();
        report = extent.getExtentReportInstance(reportName);
    }

    public void setTestCaseName(String testCaseName){
        this.testCaseName = testCaseName;
    }

    public WebDriver initDriver(String browser) {
        try {
            cap1 = new DesiredCapabilities();
            if (!Constants.CONSTANTS_GRIDEXECUTION) {
                if (browser.equalsIgnoreCase("Firefox")) {
                    System.setProperty("webdriver.gecko.driver",
                            Constants.CONSTANTS_GECKO_DRIVER_PATH);
                    cap1.setCapability("marionette", true);
                /*  ThreadLocal<WebDriver> driver1 = new ThreadLocal<WebDriver>(){
                        @Override
                        protected WebDriver initialValue(){
                            return new FirefoxDriver(cap1);
                        }
                    };*/
                    //driver = driver1.get();*/
                    driver= new FirefoxDriver(cap1);
                    driver.manage().timeouts()
                            .implicitlyWait(2, TimeUnit.SECONDS);
                    wait = new WebDriverWait(driver, 50);
                    report.log(LogStatus.INFO, "Firefox driver has started");
                    logger.info("Firefox driver has started");

                } else if (browser.equalsIgnoreCase("Chrome")) {
                    System.setProperty(Constants.CONSTANTS_CHROME_PROPERTY,
                            Constants.CONSTANTS_CHROME_DRIVER_PATH);
                    /*driver1 = new ThreadLocal<WebDriver>(){
                        @Override
                        protected WebDriver initialValue(){
                            return new ChromeDriver();
                        }
                    };
                    driver = driver1.get();*/
                    driver = new ChromeDriver();
                    driver.manage().timeouts()
                            .implicitlyWait(2, TimeUnit.SECONDS);
                    wait = new WebDriverWait(driver, 50);
                    report.log(LogStatus.INFO, "Chrome driver has started");
                    logger.info("Chrome driver has started");
                } else if (browser.equalsIgnoreCase("PhantomJS")) {
                    System.setProperty(Constants.CONSTANTS_PHANTOM_PROPERTY,
                            Constants.CONSTANTS_PHANTOM_DRIVER_PATH);
                    cap1.setJavascriptEnabled(true);
                    cap1.setCapability("takesScreenshot", true);
                    cap1.setCapability(
                            PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,
                            Constants.CONSTANTS_PHANTOM_DRIVER_PATH);
                    /*ThreadLocal<WebDriver> driver1 = new ThreadLocal<WebDriver>(){
                        @Override
                        protected WebDriver initialValue(){
                            return new PhantomJSDriver(cap1);
                        }
                    };
                    driver = driver1.get();*/
                    driver = new PhantomJSDriver(cap1);
                    driver.manage().window().setSize(new Dimension(1920, 1200));
                    wait = new WebDriverWait(driver, 50);
                    report.log(LogStatus.INFO,
                            "PhantomJS/Ghost driver has started");
                    logger.info("PhantomJS/Ghost driver has started");
                } else if (browser.equalsIgnoreCase("IE")) {
                    System.setProperty(Constants.CONSTANTS_IE_PROPERTY,
                            Constants.CONSTANTS_IE_DRIVER_PATH);
                    // cap1.internetExplorer();
                    cap1.setCapability(
                            InternetExplorerDriver.IE_ENSURE_CLEAN_SESSION,
                            true);
                    /*ThreadLocal<WebDriver> driver1 = new ThreadLocal<WebDriver>(){
                        @Override
                        protected WebDriver initialValue(){
                            return new InternetExplorerDriver(cap1);
                        }
                    };
                    driver = driver1.get();*/
                    driver = new InternetExplorerDriver(cap1);
                    driver.manage().timeouts()
                            .implicitlyWait(2, TimeUnit.SECONDS);
                    wait = new WebDriverWait(driver, 50);
                    report.log(LogStatus.INFO,
                            "Internet Explorer driver has started");
                    logger.info("Internet Explorer driver has started");
                }
            } else {
                String nodeUrl = Constants.CONSTANTS_IPDETAILS;
                if (browser.equalsIgnoreCase("Firefox")) {
                    System.setProperty("webdriver.gecko.driver",
                            Constants.CONSTANTS_GECKO_DRIVER_PATH);
                    cap1.setCapability("marionette", true);
                    cap1 = DesiredCapabilities.firefox();
                    cap1.setBrowserName("firefox");
                    cap1.setPlatform(Platform.WINDOWS);
                } else if (browser.equalsIgnoreCase("Chrome")) {
                    System.setProperty(Constants.CONSTANTS_CHROME_PROPERTY,
                            Constants.CONSTANTS_CHROME_DRIVER_PATH);
                    cap1 = DesiredCapabilities.chrome();
                    cap1.setBrowserName("chrome");
                    cap1.setPlatform(Platform.WINDOWS);
                } else if (browser.equalsIgnoreCase("IE")) {
                    System.setProperty(Constants.CONSTANTS_IE_PROPERTY,
                            Constants.CONSTANTS_IE_DRIVER_PATH);
                    // cap1.internetExplorer();
                    cap1 = DesiredCapabilities.internetExplorer();
                    cap1.setBrowserName("internet explorer");
                    cap1.setPlatform(Platform.WINDOWS);
                    cap1.setCapability(
                            InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,
                            true);
                    cap1.setCapability(
                            InternetExplorerDriver.IE_ENSURE_CLEAN_SESSION,
                            true);
                }
                ThreadLocal<WebDriver> driver1 = new ThreadLocal<WebDriver>(){
                    @Override
                    protected WebDriver initialValue(){
                            try {
                                return new RemoteWebDriver(new URL(nodeUrl), cap1);
                            } catch (MalformedURLException e) {
                                e.printStackTrace();
                            }
                            return null;
                    }
                };
                driver = driver1.get();
                driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);
                wait = new WebDriverWait(driver, 50);
                report.log(LogStatus.INFO, browser + " driver has started");
                logger.info(browser + " driver has started");
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                Assert.assertTrue(true, "Set up Test");
            } catch (Exception e) {
                logger.error("Try and catch block while assert " + e);
            }
        }
        return driver;
    }

    private String getProjectPath() {
        File currentDirFile = new File("");
        String path = currentDirFile.getAbsolutePath();
        return path;
    }

}




TESTNG SUITE XML:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
    <suite name="C4C" thread-count="3" parallel="methods">
        <parameter name="url" value="https://abc.xyz.ondemand.com/"/>
        <parameter name="browser" value="Chrome"/>
        <test name="Case" allow-return-values="true" group-by-instances="true">
            <classes>
                <class name="com.sonata.tests.ComplainTest" >
                </class>
            </classes>
        </test>
    </suite>

1 个答案:

答案 0 :(得分:3)

您将驱动程序存储在类级别。但是您基于方法进行并行化。在这种情况下,将有一个驱动程序对象。我看到线程本地行被注释了。

有两种方法可以解决此问题,

一个:在方法级别运行并浏览每个方法的HomePage时,您可以在方法级别维护驱动程序对象,

        Webdriver driver = initDriver(browser);
        homeObject = new HomePage(driver, report, wait, logger);
        homeObject.goToHomepge(url);

其他方式:您可以在驱动程序创建类的本地线程中维护驱动程序对象,

 public class DriverCreation {
    private static ThreadLocal<WebDriver> WEBDRIVER = new ThreadLocal<WebDriver>();

   public WebDriver getWebDriver(String browser){
      WebDriver driver= WEBDRIVER.get();
      if (driver== null) {
        driver = initDriver(browser);
        WEBDRIVER.set(driver);
      }
      return driver;
   }

}

并以相同的方式在测试方法中使用它,

        Webdriver driver = getWebDriver(browser);
        homeObject = new HomePage(driver, report, wait, logger);
        homeObject.goToHomepge(url);

第二个将为每个线程仅创建一个驱动程序,而第一个将为每个方法创建一个驱动程序。相应地处理驾驶员退出