【问题标题】:How to Skip only one method in Selenium如何在 Selenium 中只跳过一种方法
【发布时间】:2017-06-12 18:25:07
【问题描述】:

在 selenium 中,作为使用 testng 的混合框架的一部分,如果在 excel 表中针对该测试用例设置了“否”,则应跳过该用例。

注意:测试用例名和方法名也一样。

情况如下: 我正在使用 TestNG 的 BeforeMethod 和 Test 注释。而在excel表中,我有这样的

TCID          Description       Runmode
TestCaseA1    vvvvvvvvvvvvvvvv    N
TestCaseA2    vvvvvvvvvvvvvvvv    Y

由于第一个 case 设置为 No,即使设置为 Yes,下一个 case 也会被跳过。

以下是代码。请提供问题的解决方案。

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

【问题讨论】:

  • 粘贴日志。由于您的默认值为 false,我怀疑它甚至没有进入逻辑循环。

标签: java excel selenium testng


【解决方案1】:

您可以在套件本身中使用 exclude 方法。

<methods>
       <exclude name="Test method name to exclude"/>
</methods>

【讨论】:

    【解决方案2】:

    如果您想将其作为一个 TestNG 函数来执行,这并不容易——您必须以编程方式运行整个套件,这通常需要大量重写。

    但是,您可以只使用一个布尔值来执行此操作,并将 TestNG 排除在外。

    在测试是否设置为“否”的if 子句中,您可以将静态变量设置为电子表格单元格的值。例如

    @BeforeSuite
    public static boolean runTest = true;
    ...
    if (!spreadsheet.says.yes)
        runTest = false;
    

    现在,在另一个班级……

    @Test
    if (firstClass.runTest)
        doTheThing()
    else
        System.out.println("[INFO] Skipping test...");
    

    【讨论】:

      【解决方案3】:

      这很容易通过IMethodInterceptor 实现。
      假设您只有 2 种测试方法:

      @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 拦截你要调用的测试方法,然后检查它们是否应该被跳过。 在isSkippable 方法中,您可以使用电子表格。 如果您喜欢更短的解决方案,甚至可以在 intercept 方法中进行管理。
      如果您更喜欢 Java 7,请告诉我。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-01-04
        • 2019-11-16
        • 1970-01-01
        • 2014-06-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多