从 jar 动态加载步骤定义文件

Dynamically load step definition file from jar

我有一个 Gherkin 执行程序,我可以在其中执行我的功能文件。我想做的是从另一个 jar 添加一个 StepDefinition 文件。用户可以将我的项目与我已经编写的步骤定义一起使用,但他也可以从他自己的 jar 文件中添加自定义定义。

目前我有一个 JavaClassLoader,我从我的 jar 中加载我的 class 并在我的 main

中使用它
public class JavaClassLoader<C> extends ClassLoader {

public C LoadClass(String directory, String classpath, Class<C> parentClass) throws ClassNotFoundException {
    File pluginsDir = new File(System.getProperty("user.dir") + directory);
    for (File jar : pluginsDir.listFiles()) {
      try {
        ClassLoader loader = URLClassLoader.newInstance(
            new URL[] { jar.toURL() },
            getClass().getClassLoader()
        );
        Class<?> clazz = Class.forName(classpath, true, loader);
        Class<? extends C> newClass = clazz.asSubclass(parentClass);
        // Apparently its bad to use Class.newInstance, so we use 
        // newClass.getConstructor() instead
        Constructor<? extends C> constructor = newClass.getConstructor();
        return constructor.newInstance();

      } catch (ClassNotFoundException e) {
        // There might be multiple JARs in the directory,
        // so keep looking
        continue;
      } catch (MalformedURLException e) {
        e.printStackTrace();
      } catch (NoSuchMethodException e) {
        e.printStackTrace();
      } catch (InvocationTargetException e) {
        e.printStackTrace();
      } catch (IllegalAccessException e) {
        e.printStackTrace();
      } catch (InstantiationException e) {
        e.printStackTrace();
      }
    }
    throw new ClassNotFoundException("Class " + classpath
        + " wasn't found in directory " + System.getProperty("user.dir") + directory);
  }

}

JavaClassLoader<AbstractStepDefs> loader = new JavaClassLoader<AbstractStepDefs>();  
    loader.LoadClass("/", "stepDef.dynamicClass", AbstractStepDefs.class);  

问题是 Cucumber 无法读取我在其他 jar 中编写的方法。有没有办法使用项目中没有的 step def 文件?

有没有办法使用项目中没有的 step def 文件?

是,也不是。是的部分更接近"sort of"。 git 支持多种在其他项目中引用子项目的方法。 common "subprojects" 在自己的项目中维护,然后拉入using项目。看here for a discussion. I looked into doing submodules once. I even had it working. But I couldn't convince the TeamCity owners to support it. It works but you have to be careful about how you use it. There are haters.

我最后做的是创建一个共享的 "global" 项目,其中包含公共登录和导航页面的页面文件。这个全球项目还包含对不同浏览器和 SauceLabs 远程执行的所有启动和关闭支持。

必须重复步骤定义(糟糕;我更喜欢 DRY),但它们很小,因为它们大多只是调用页面文件方法。所有这些网页都在其自己的 class 文件中的全局项目中定义。 class WebDriverManager 中定义了通用的管理代码。

@Given("^I navigate to the public ACME WebPage and select Login$")
public void iNavigateToTheAcmePublicWebPage() {
    pageFactory.AcmePublicWebPage().navigateTo(
      WebDriverManager.instance().getAcmeUrl());

    pageFactory.AcmePublicWebPage().closeNotificationPopUp(); //If there is one
    pageFactory.AcmePublicWebPage().selectLoginLink();
}

@When("^I close the browser$")
public void iCloseTheBrowser() {
    WebDriverManager.instance().closeBrowser();
}

我减少了大部分,但不是所有的重复。只要我维护全局 git 项目并在他们需要从 TeamCity 下载新的全局 jar 时通知他们,大多数初级测试自动化工程师就不必担心繁重的工作。