关键字驱动框架和 Selenium 网格以及单个测试用例的报告
Keyword Driven Framework and Selenium Grid and reports for individual test cases
我已经使用 Selenium WebDriver 开发了一个关键字驱动的框架 Java.Basically 我已经开发了一个 master TC sheet 它将使用 运行 模式读取 TC 为是并转到测试步骤 sheet 并读取关键字并根据这些关键字执行操作。
我正在使用驱动程序脚本来读取这些测试用例。
现在我想 运行 这些测试用例并行使用 selenium 网格。我在 Whosebug 上找到了两个关于这个的 posts。
- Is it possible to convert a keyword driven framework using Selenium WebDriver to Selenium grid?
但是在这个post中没有提到如何实现。
- Executing parallel tests in a Selenium keyword driven framework
这里提出了一个解决方案,但报告生成为一个测试用例通过或失败。
我在读取上述测试用例的驱动程序脚本中为该方法添加了@Test 注释。
public class DriverScript {
@Test
public void startExecution() throws Exception{
//public static void main(String[] args) throws Exception {
excelUtilities eu = new excelUtilities();
Properties gldata = new Properties();
InputStream input = new FileInputStream("src/executionEngine/config.properties");
gldata.load(input);
List<List<String>> testcases = new ArrayList<List<String>>();
testcases = eu.getTestCases(gldata.getProperty("WB_PATH_TESTS"), gldata.getProperty("WB_PATH_TESTS_SHEET"));
//System.out.println(testcases);
DriverScript.prepareKeywords(testcases);
}
上述方法逐一读取测试用例,并根据这些关键字从各个测试用例中获取关键字。但是由于我使用 @Test 方法作为读取主要测试用例的方法,它认为它是一个测试用例,因此报告生成为 1 个测试用例通过,即使我们上面有两个测试用例。
===============================================
Suite
Total tests run: 1, Failures: 0, Skips: 0
===============================================
那么当我们有关键字驱动框架时如何运行并行测试用例?
我想到的唯一解决方案是,在第二个 post 的问题部分中提到的方式,例如为每个测试用例创建单独的方法并阅读测试用例步骤。
关于如何在 selenium 网格上 运行 关键字驱动的框架,是否有任何替代方案?
谢谢。
使用 TestNg 中的 DataProvider 选项对 运行 进行测试。这样每个数据都将被视为单独的测试。
将您的数据提供者指向@Factory 选项,以便为每个数据创建新实例。现在我们可以 运行 测试 parallel= intances
。所以每个测试用例将运行在不同的线程中并行。
我们可以如下重写您的驱动程序脚本,
public class DriverScript {
List<String> testcase;
@Factory(dataProvider = "testCases")
public DriverScript(List<String> testcase) {
this.testcase = testcase;
}
@Test
public void runTestCase() {
// change this method run single with List<String>. Previously you passed List<List<String>>
DriverScript.prepareKeywords(testcase);
}
@DataProvider
public Object[][] testCases(ITestContext context) throws InterruptedException {
excelUtilities eu = new excelUtilities();
Properties gldata = new Properties();
InputStream input = new FileInputStream("src/executionEngine/config.properties");
gldata.load(input);
List<List<String>> testcases = new ArrayList<List<String>>();
testcases = eu.getTestCases(gldata.getProperty("WB_PATH_TESTS"), gldata.getProperty("WB_PATH_TESTS_SHEET"));
List<List<String>> testcases = new ArrayList<>();
Object[][] testCasedata= new Object[testcases.size()][1];
for (int i = 0; i < testcases.size() ; i++) {
testCasedata[i][0]=testcases.get(i);
}
return testCasedata;
}
为了 运行 上述使用 parallel=instances 的测试,我们必须像下面这样创建 testuite xml 和 运行 testng 使用这个 xml 套件,
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="KeywordDrivenSuite" thread-count="2" parallel="instances"
preserve-order="true" configfailurepolicy="continue">
<test name="KeywordDrivenTest">
<classes>
<class
name="com.package.DriverScript" />
</classes>
</test>
</suite>
以下是我的尝试方法,效果很好。
@DataProvider(name = "maintestcases",parallel = true)
public static String[][] startexecution() throws Exception{
excelUtilities eu = new excelUtilities();
Properties gldata = new Properties();
InputStream input = new FileInputStream("src/executionEngine/config.properties");
gldata.load(input);
List<List<String>> testcases = new ArrayList<List<String>>();
testcases = eu.getTestCases(gldata.getProperty("WB_PATH_TESTS"), gldata.getProperty("WB_PATH_TESTS_SHEET"));
int no_test_cases = testcases.size();
String testcasesobject[][] = new String[no_test_cases][3];
for(int i=0; i<testcases.size(); i++) {
testcasesobject[i][0] = testcases.get(i).get(0);
testcasesobject[i][1] = testcases.get(i).get(1);
testcasesobject[i][2] = testcases.get(i).get(2);
}
System.out.println(Arrays.toString(testcasesobject));
return testcasesobject;
}
@Test(dataProvider = "maintestcases")
public static void prepareKeywords(String testcase, String wbbook, String sheet) throws IOException, ClassNotFoundException, NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException, InterruptedException {
Properties gldata = new Properties();
InputStream input = new FileInputStream("src/executionEngine/config.properties");
gldata.load(input);
//int total_testcases = testcases.size();
excelUtilities ecu = new excelUtilities();
LogUtilities.startTestCase(testcase);
List<String> keywords = new ArrayList<String>();
keywords = ecu.getKeywordsFromTestCase(testcase,gldata.getProperty("WB_PATH_TEST_CASES")+wbbook+".xlsx", sheet);
getDependencies(keywords);
}
testngxml,我配置如下
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="methods" data-provider-thread-count="2" thread-count="5">
<test name="Test" >
<classes>
<class name="executionEngine.DriverScript2"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
请检查此解决方案并告诉我是否可以?
谢谢。
您也可以 运行 测试而不使用 xml 文件。使用@Navarasu 提供的 DriverScript 并从下面的函数
中执行它们
void executionDriver(){
TestNG test = new TestNG();
XmlSuite suite = new XmlSuite();
suite.setName("KeywordDrivenSuite");
XmlTest xmlTest = new XmlTest(suite);
xmlTest.setName("KeywordDrivenTest");
List<XmlClass> xmlClasses = new ArrayList<XmlClass> ();
String packageName = "com.package.DriverScript";
XmlClass xmlclass = new XmlClass(packageName);
xmlClasses.add(xmlclass);
xmlTest.setXmlClasses(xmlClasses);
List<XmlTest> testList = new ArrayList<XmlTest>();
testList.add(xmlTest);
suite.setTests(testList);
List<XmlSuite> suiteList = new ArrayList<XmlSuite>();
suiteList.add(suite);
test.setXmlSuites(suiteList);
test.test.setThreadCount(3);//Thread count can be controlled from properties file also.
test.run();
}
我已经使用 Selenium WebDriver 开发了一个关键字驱动的框架 Java.Basically 我已经开发了一个 master TC sheet 它将使用 运行 模式读取 TC 为是并转到测试步骤 sheet 并读取关键字并根据这些关键字执行操作。
我正在使用驱动程序脚本来读取这些测试用例。
现在我想 运行 这些测试用例并行使用 selenium 网格。我在 Whosebug 上找到了两个关于这个的 posts。
- Is it possible to convert a keyword driven framework using Selenium WebDriver to Selenium grid?
但是在这个post中没有提到如何实现。
- Executing parallel tests in a Selenium keyword driven framework
这里提出了一个解决方案,但报告生成为一个测试用例通过或失败。
我在读取上述测试用例的驱动程序脚本中为该方法添加了@Test 注释。
public class DriverScript {
@Test
public void startExecution() throws Exception{
//public static void main(String[] args) throws Exception {
excelUtilities eu = new excelUtilities();
Properties gldata = new Properties();
InputStream input = new FileInputStream("src/executionEngine/config.properties");
gldata.load(input);
List<List<String>> testcases = new ArrayList<List<String>>();
testcases = eu.getTestCases(gldata.getProperty("WB_PATH_TESTS"), gldata.getProperty("WB_PATH_TESTS_SHEET"));
//System.out.println(testcases);
DriverScript.prepareKeywords(testcases);
}
上述方法逐一读取测试用例,并根据这些关键字从各个测试用例中获取关键字。但是由于我使用 @Test 方法作为读取主要测试用例的方法,它认为它是一个测试用例,因此报告生成为 1 个测试用例通过,即使我们上面有两个测试用例。
===============================================
Suite
Total tests run: 1, Failures: 0, Skips: 0
===============================================
那么当我们有关键字驱动框架时如何运行并行测试用例?
我想到的唯一解决方案是,在第二个 post 的问题部分中提到的方式,例如为每个测试用例创建单独的方法并阅读测试用例步骤。
关于如何在 selenium 网格上 运行 关键字驱动的框架,是否有任何替代方案?
谢谢。
使用 TestNg 中的 DataProvider 选项对 运行 进行测试。这样每个数据都将被视为单独的测试。
将您的数据提供者指向@Factory 选项,以便为每个数据创建新实例。现在我们可以 运行 测试 parallel= intances
。所以每个测试用例将运行在不同的线程中并行。
我们可以如下重写您的驱动程序脚本,
public class DriverScript {
List<String> testcase;
@Factory(dataProvider = "testCases")
public DriverScript(List<String> testcase) {
this.testcase = testcase;
}
@Test
public void runTestCase() {
// change this method run single with List<String>. Previously you passed List<List<String>>
DriverScript.prepareKeywords(testcase);
}
@DataProvider
public Object[][] testCases(ITestContext context) throws InterruptedException {
excelUtilities eu = new excelUtilities();
Properties gldata = new Properties();
InputStream input = new FileInputStream("src/executionEngine/config.properties");
gldata.load(input);
List<List<String>> testcases = new ArrayList<List<String>>();
testcases = eu.getTestCases(gldata.getProperty("WB_PATH_TESTS"), gldata.getProperty("WB_PATH_TESTS_SHEET"));
List<List<String>> testcases = new ArrayList<>();
Object[][] testCasedata= new Object[testcases.size()][1];
for (int i = 0; i < testcases.size() ; i++) {
testCasedata[i][0]=testcases.get(i);
}
return testCasedata;
}
为了 运行 上述使用 parallel=instances 的测试,我们必须像下面这样创建 testuite xml 和 运行 testng 使用这个 xml 套件,
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="KeywordDrivenSuite" thread-count="2" parallel="instances"
preserve-order="true" configfailurepolicy="continue">
<test name="KeywordDrivenTest">
<classes>
<class
name="com.package.DriverScript" />
</classes>
</test>
</suite>
以下是我的尝试方法,效果很好。
@DataProvider(name = "maintestcases",parallel = true)
public static String[][] startexecution() throws Exception{
excelUtilities eu = new excelUtilities();
Properties gldata = new Properties();
InputStream input = new FileInputStream("src/executionEngine/config.properties");
gldata.load(input);
List<List<String>> testcases = new ArrayList<List<String>>();
testcases = eu.getTestCases(gldata.getProperty("WB_PATH_TESTS"), gldata.getProperty("WB_PATH_TESTS_SHEET"));
int no_test_cases = testcases.size();
String testcasesobject[][] = new String[no_test_cases][3];
for(int i=0; i<testcases.size(); i++) {
testcasesobject[i][0] = testcases.get(i).get(0);
testcasesobject[i][1] = testcases.get(i).get(1);
testcasesobject[i][2] = testcases.get(i).get(2);
}
System.out.println(Arrays.toString(testcasesobject));
return testcasesobject;
}
@Test(dataProvider = "maintestcases")
public static void prepareKeywords(String testcase, String wbbook, String sheet) throws IOException, ClassNotFoundException, NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException, InterruptedException {
Properties gldata = new Properties();
InputStream input = new FileInputStream("src/executionEngine/config.properties");
gldata.load(input);
//int total_testcases = testcases.size();
excelUtilities ecu = new excelUtilities();
LogUtilities.startTestCase(testcase);
List<String> keywords = new ArrayList<String>();
keywords = ecu.getKeywordsFromTestCase(testcase,gldata.getProperty("WB_PATH_TEST_CASES")+wbbook+".xlsx", sheet);
getDependencies(keywords);
}
testngxml,我配置如下
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="methods" data-provider-thread-count="2" thread-count="5">
<test name="Test" >
<classes>
<class name="executionEngine.DriverScript2"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
请检查此解决方案并告诉我是否可以?
谢谢。
您也可以 运行 测试而不使用 xml 文件。使用@Navarasu 提供的 DriverScript 并从下面的函数
中执行它们void executionDriver(){
TestNG test = new TestNG();
XmlSuite suite = new XmlSuite();
suite.setName("KeywordDrivenSuite");
XmlTest xmlTest = new XmlTest(suite);
xmlTest.setName("KeywordDrivenTest");
List<XmlClass> xmlClasses = new ArrayList<XmlClass> ();
String packageName = "com.package.DriverScript";
XmlClass xmlclass = new XmlClass(packageName);
xmlClasses.add(xmlclass);
xmlTest.setXmlClasses(xmlClasses);
List<XmlTest> testList = new ArrayList<XmlTest>();
testList.add(xmlTest);
suite.setTests(testList);
List<XmlSuite> suiteList = new ArrayList<XmlSuite>();
suiteList.add(suite);
test.setXmlSuites(suiteList);
test.test.setThreadCount(3);//Thread count can be controlled from properties file also.
test.run();
}