小编典典

Jenkins将参数传递给testNG Java

java

我正在使用testNG,Selenium和Jenkins建立测试自动化框架。该代码工作正常,它读取一个或多个csv文件并将其用作测试数据。我从詹金斯进行测试。

package test;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
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.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.AfterTest;

public class runTest {
private WebDriver myDriver = null;

    @BeforeTest
    public void openBrowser(){
        System.out.println("This test automation framework is created by --- for ---");
        //Set browser
        //System.setProperty("webdriver.chrome.driver", "C://Temp//chromedriver.exe");
        //System.setProperty("webdriver.ie.driver", "C://Temp//IEDriverServer.exe");

        //Instantiate new WebDriver object with browser of choice
        myDriver = new FirefoxDriver();
        //myDriver = new InternetExplorerDriver();
        //myDriver = new ChromeDriver();
        myDriver.get("http://localhost/tests/");
    }


    @Test (dataProvider="provideData")
    public void csvTest(String stepNr, String timeWaitMil, String waitForElement, String clickOnCssNameXpathLink, 
                        String valueInTextBox, String backspaceText, String assertReturnTrueFalse, 
                        String assertBy, String getUrl, 
                        String deleteCookie, String snapshot, String specialSnapshot){



        // Click on a something based on css, name xpath or link
        if (!"-".equals(clickOnCssNameXpathLink)){
            myDriver.findElement(By.name(clickOnCssNameXpathLink)).click();
        }

        // Enter value in textbox
        if (!"-".equals(valueInTextBox)){
            myDriver.findElement(By.name(clickOnCssNameXpathLink)).sendKeys(valueInTextBox);
        }


        // Delete cookies
        if (!"-".equals(deleteCookie)){
            myDriver.manage().deleteAllCookies();           
        }

        // Make snapshot of whole page
        if (snapshot.equalsIgnoreCase("true")){
            // take the screenshot of full page
            File scrFile = ((TakesScreenshot)myDriver).getScreenshotAs(OutputType.FILE);
            // prepare date to use in filename
            Date d = new Date();
            SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy HH-mm-ss");
            // Save screenshot
            try {
                FileUtils.copyFile(scrFile, new File("c:\\Temp\\screenshots\\full_page_" +dateFormat.format(d)+ ".png"));
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                System.out.println("Could not make screenshot");
            }       
        }

    }


    @DataProvider
    public Iterator<Object []> provideData() throws InterruptedException, IOException
    {
        //Array with multiple csv's
        String csvFiles[] = {"C:/Users/---/Desktop/JES2.0/testingSelenium/testNG/data.csv", "C:/Users/---/Desktop/JES2.0/testingSelenium/testNG/data2.csv"};

        List<Object []> testCases = new ArrayList<>();

        //loop through csv files
        for(String csvFile:csvFiles){
                String[] data= null;
                //read csv file
                BufferedReader br = new BufferedReader(new FileReader(csvFile));

                //Skip first line in the csv file, because that only contains the column names
                String line = br.readLine();

                //loop through csv and split parameters by comma sign ,
                while ((line = br.readLine()) != null) {
                    // use comma as separator
                    data= line.split(",");
                    testCases.add(data);
                }//end of while loop

            }// end of for loop
        return testCases.iterator();        
    }


    @AfterTest
    public void closeBrowser(){
        //close browser
        myDriver.quit();
    }

}

如您所见,浏览器,URL和CSV文件是硬编码的。我希望能够将这些作为参数传递。做这个的最好方式是什么?是否可以让他们通过詹金斯?

我正在考虑构建一个仪表板,在其中可以指定要使用哪种浏览器运行哪些测试(csv文件)。

这是我正在运行的Jenkins批处理命令

java -cp C:\Users\---\Desktop\JES2.0\testingSelenium\testNG\libs\selenium-server-standalone-2.43.1.jar;C:\Users\---\Desktop\JES2.0\testingSelenium\testNG\bin org.testng.TestNG testng.xml

阅读 369

收藏
2020-11-30

共1个答案

小编典典

Jenkins具有内置的参数处理功能,它以自己的方式非常灵活。但是在这种情况下,由于要传递文件名作为参数,因此可以轻松地将该功能与Filesystem
List Parameter
结合使用,后者可以基于将解析文件列表的regexp构建列表。

如果您使用Maven或Ant,则可以将该参数嵌入到构建过程中,形式类似于以下形式:

  <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <configuration>
            <systemPropertyVariables>
                <environment>${env.PARAM}</environment>
            </systemPropertyVariables>
            <suiteXmlFiles> 
                <suiteXmlFile>testng.xml</suiteXmlFile>
            </suiteXmlFiles>
        </configuration>
    </plugin>

有了这个,您可以读取传递给Maven的参数-在Jenkins中使用其内部调用程序,并在命令行上输入:

mvn install -Denv.PARAM=VALUE

所以无论哪种方式都可以工作…

2020-11-30