如何在Selenium中只跳过一种方法
在selenium中,作为使用testng的混合框架的一部分,如果在excel表中设置了“否”,那么应该跳过这个案例。
注意 :testing用例名称和方法名称也相同。
这里是这样的情况:我正在使用TestNG的BeforeMethod和Test注解。 而在Excel表格中,我有这样的
TCID Description Runmode TestCaseA1 vvvvvvvvvvvvvvvv N TestCaseA2 vvvvvvvvvvvvvvvv Y
由于第一种情况设置为“否”,即使将其设置为“是”,也会跳过下一个案例。
以下是代码。 请提供问题的解决scheme。
@BeforeMethod public void checkTestSkip(Method method) throws IOException{ intialise();//to load the properties String testName = method.getName(); if (!testUtil.isTestCaseRunnable(SuiteAXls,testName)) { App_Logs.info("Skipping the case as set to No"); throw new SkipException("Skipping the case as set to No"); } else { App_Logs.info("Not Skipping"); } } @Test(priority=1) public void TestCaseA1(){ System.out.println("test case A1"); } @Test(priority=2) public void TestCaseA2(){ System.out.println("test case A11"); } } ******************************************** public static boolean isTestCaseRunnable(Xls_Reader xls,String testName){ boolean runmode=false; int rwcnt=xls.getRowCount("Test Cases"); for(int j=2;j<=rwcnt;j++){ //for(int i=0;i<method.length;i++){ //System.out.println(method[i].getName()); System.out.println(xls.getCellData("Test Cases","TCID",j)); if(testName.equals(xls.getCellData("Test Cases","TCID",j))){ if(xls.getCellData("Test Cases","Runmode",j).equals("Y")){ System.out.println("runmode is yes"); runmode=true; break; }else{ System.out.println("runmode is false"); runmode=false; break; } } } return runmode; }
如果你想把它作为一个TestNG函数来完成,那么这将不是一件容易的事 – 你将不得不以编程的方式来运行你的整个套件,这通常需要大量的重写。
但是,您可以使用布尔值来执行此操作,并将TestNG离开它。
在testing是否设置为“否”的if
子句中,可以将静态variables设置为电子表格单元格的值。 例如
@BeforeSuite public static boolean runTest = true; ... if (!spreadsheet.says.yes) runTest = false;
现在在另外一个class上
@Test if (firstClass.runTest) doTheThing() else System.out.println("[INFO] Skipping test...");
这是用IMethodInterceptor
很容易实现的。
想象一下,你只有两种testing方法:
@Listeners(SkipInterceptor.class) public class TestMethodCandidates { @Test public void checkAddition() { System.out.println("I am in checkAddition!"); assertEquals(1+2, 3, "1+2 should equal 3."); } @Test public void shouldBeSkipped() { System.out.println("I am in shouldBeSkipped!"); assertEquals(true, true, "This anti-pattern should be skipped."); } }
和一个方法拦截器:
public class SkipInterceptor implements IMethodInterceptor { /** * Your spreadsheet variable. */ private Reader reader; public List<IMethodInstance> intercept(List<IMethodInstance> list, ITestContext iTestContext) { return list.stream() .filter(instance -> !isSkippable(instance.getMethod().getMethodName())) .collect(Collectors.toList()); } /** * In this method you can use your spreadsheet and name of the method. * @return true, iff this method should run. */ private boolean isSkippable(String methodName) { if(methodName.equals("shouldBeSkipped")) { return true; } return false; } }
我的输出:
I am in checkAddition! Test ignored.
IMethodInterceptor
拦截你要调用的testing方法,而不是检查它们是否应该被跳过。 在isSkippable
方法中,您可以使用电子表格。 如果你喜欢较短的解决scheme,这甚至可以在intercept
方法中进行pipe理。
让我知道,如果你喜欢Java 7。
你可以在套件本身中使用排除方法。
<methods> <exclude name="Test method name to exclude"/> </methods>