bugfix> java > 投稿

こんにちはコミュニティ:CucumberをJUnitに統合しようとして、次のクラスがあります。

import cl.cukes.ParameterizedRunnerFactory;
import cl.test.AutomatedWebTest;
import cl.util.report.Report;
import cucumber.annotation.en.Given;
import cucumber.annotation.en.Then;
import cucumber.annotation.en.When;
import cucumber.api.junit.Cucumber;
import org.junit.runner.RunWith;
import org.junit.runner.Runner;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.UseParametersRunnerFactory;
import org.junit.runners.model.InitializationError;
import org.junit.runners.parameterized.TestWithParameters;
import org.openqa.selenium.By;
import org.openqa.selenium.ie.InternetExplorerDriver;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
//JAVA
@RunWith(Parameterized.class)
@UseParametersRunnerFactory(ParameterizedRunnerFactory.class)
public class CucumberStepDefs extends AutomatedWebTest {
    @Parameterized.Parameter(value = 0)
    public String user;
    @Parameterized.Parameter(value = 1)
    public String pass;

   public static class CucumberRunnerFactor extends ParameterizedRunnerFactory {
       public Runner createRunnerForTestWithParameters(TestWithParameters test)
               throws InitializationError {
           try {
               return new Cucumber(test.getTestClass().getJavaClass());
           } catch (IOException e) {
               throw new InitializationError(e);
           }
       }
    }
    @Given("^Enter to the QA URL environment")
    public void Login() throws Exception {
        baseUrl = "http://www.miprivado.cl/";
        driver = new InternetExplorerDriver();
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        driver.get(baseUrl + "ucachaca/test.htm");
        driver.switchTo().defaultContent();
        driver.switchTo().frame("frmbody");
    }
    @When("^Fields Username y Passwordare displayed so enter parameters$")
    public void UsuarioPass() throws Exception {
        driver.findElement(By.id("TxbTELLUSERID")).clear();
        driver.findElement(By.id("TxbTELLUSERID")).sendKeys(user);
        driver.findElement(By.id("TxbUSERPASSW")).clear();
        driver.findElement(By.id("TxbUSERPASSW")).sendKeys(pass);

        screenshot.take(this, driver, "LoginR C01");
        driver.findElement(By.id("BtnSubmit")).click();
        driver.switchTo().defaultContent();
        driver.switchTo().frame("frmwebteller");
        driver.manage().window().maximize();
    }
    @Then("^User gets into the system and do Logout Logout$")
    public void Logout() throws Exception {
        screenshot.take (this, driver, "LoginR C02");
        driver.switchTo ().defaultContent ();
        driver.switchTo ().frame ("frmbody").switchTo ().frame ("menu"); 
        driver.findElement(By.cssSelector("b")).click();
        screenshot.take (this, driver, "LoginR C03");
        driver.findElement(By.linkText("Log Off")).click();
        screenshot.take (this, driver, "LoginR C04");
        driver.quit();
    }
}

ParameterizedRunnerFactoryは次です:

import org.junit.runner.Runner;
import org.junit.runners.model.InitializationError;
import org.junit.runners.parameterized.ParametersRunnerFactory;
import org.junit.runners.parameterized.TestWithParameters;
/**
 *
 */
public class ParameterizedRunnerFactory implements ParametersRunnerFactory {
    @Override
    public Runner createRunnerForTestWithParameters(TestWithParameters test) throws InitializationError {
        return new ParameterizedRunner(test);
    }
}

パラメーター化されたRunnerクラスは次です:

import org.junit.After;
import org.junit.Before;
import org.junit.runners.model.*;
import org.junit.runners.parameterized.BlockJUnit4ClassRunnerWithParameters;
import org.junit.runners.parameterized.TestWithParameters;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
public class ParameterizedRunner extends BlockJUnit4ClassRunnerWithParameters {
        public ParameterizedRunner(TestWithParameters test) throws InitializationError {
            super(test);
        }
        // workaround for: https://github.com/junit-team/junit/issues/1046
        private static final ConcurrentHashMap<Class<?>, TestClass> testClasses = new ConcurrentHashMap<>();
        @Override
        protected TestClass createTestClass(Class<?> clazz) {
            TestClass testClass = testClasses.get(clazz);
            if (testClass == null) {
                testClasses.put(clazz, testClass = new TestClass(clazz));
            }
            return testClass;
        }
        // playing whack-a-mole with new TLAB allocations by re-defining with{Befores,Afters}...
        @Override
        protected Statement withBefores(FrameworkMethod method, Object target, Statement statement) {
            List<FrameworkMethod> list = getTestClass().getAnnotatedMethods(Before.class);
            if (list.isEmpty()) {
                return statement;
            }
            return new BeforesStatement(target, statement, list);
        }
        @Override
        protected Statement withAfters(FrameworkMethod method, Object target, Statement statement) {
            List<FrameworkMethod> list = getTestClass().getAnnotatedMethods(After.class);
            if (list.isEmpty()) {
                return statement;
            }
            return new AftersStatement(target, statement, list);
        }
        private static final class BeforesStatement extends Statement {
            private static final Object[] EMPTY_ARGS = new Object[0];
            private final Object target;
            private final Statement statement;
            private final List<FrameworkMethod> list;
            BeforesStatement(Object target, Statement statement, List<FrameworkMethod> list) {
                this.target = target;
                this.statement = statement;
                this.list = list;
            }
            @Override
            public void evaluate() throws Throwable {
                // (1) Avoid ImmutableCollections#iterator()
                for (int i = 0, size = list.size(); i < size; ++i) {
                    list.get(i).invokeExplosively(target, EMPTY_ARGS);
                }
                statement.evaluate();
            }
        }
        private static final class AftersStatement extends Statement {
            private static final Object[] EMPTY_ARGS = new Object[0];
            private final Object target;
            private final Statement statement;
            private final List<FrameworkMethod> list;
            AftersStatement(Object target, Statement statement, List<FrameworkMethod> list) {
                this.target = target;
                this.statement = statement;
                this.list = list;
            }
            @Override
            public void evaluate() throws Throwable {
                // (2) Lazily create ArrayList
                ArrayList<Throwable> throwables = null;
                try {
                    statement.evaluate();
                } catch (Throwable e) {
                    throwables = new ArrayList<Throwable>();
                    throwables.add(e);
                } finally {
                    for (int i = 0, size = list.size(); i < size; ++i) {
                        try {
                            list.get(i).invokeExplosively(target, EMPTY_ARGS);
                        } catch (Throwable e) {
                            if (throwables == null) {
                                throwables = new ArrayList<Throwable>();
                            }
                            throwables.add(e);
                        }
                    }
                }
                if (throwables != null) {
                    MultipleFailureException.assertEmpty(throwables);
                }
            }
        }
    }

Cucumber Runner Testクラスは次です:

import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
import org.junit.runner.RunWith;
@RunWith(Cucumber.class)
@CucumberOptions(features={"//src/features/Login.feature"}
                 ,format = {"pretty", "html:target/cucumber"}
                 ,glue = {"cucumber.CucumberStepDefs"}
)
/*@Suite.SuiteClasses({
        CucumberStepDefs.class,
})*/
public class CucumberRunnerTest {
}

CucumberStepDefsを実行すると、IntelliJで次の問題が表示されます。

java.lang.Exception: No public static parameters method on class cl.cucumber.CucumberStepDefs

すべてのクラスにエラーは表示されませんが、このクラスを実行することはできません。

誰もこれで私を助けることができますか?

回答 1 件
  • JUnitとMavenを混在させないでください。

    最高の答えはGrasshopperによってなされました:

    1.-機能ファイルを作成します。

    2.-シナリオ概要を使用し、データを例のセクションに配置します。

    3.-次のようなランナーを使用します。

       @CucumberOptions(features = {"src/test/resource/features"},
                     glue={"stepdefs"},
                     monochrome = true,
                     tags = {"~@Ignore"},
                     plugin = {"pretty","html:target/cucumber-reports/cucumber-pretty",
                               "json:target/cucumber-reports/CucumberTestReport.json",
                               "rerun:target/cucumber-reports/rerun.txt",
                               "usage:target/cucumber-usage.json"}
    )
    public class TestRunner extends ExtendedTestNGRunner{
        private TestNGCucumberRunner testNGCucumberRunner;
        @BeforeClass(alwaysRun = true)
        public void setUpClass() throws Exception {
            testNGCucumberRunner = new TestNGCucumberRunner(this.getClass());
        }
        @Test(groups = "cucumber", description = "Runs Cucumber Feature", dataProvider = "features")
        public void feature(CucumberFeatureWrapper cucumberFeature) {
            testNGCucumberRunner.runCucumber(cucumberFeature.getCucumberFeature());
        }
        @DataProvider
        public Object[][] features() {
            return testNGCucumberRunner.provideFeatures();
        }
        @AfterClass(alwaysRun = true)
        public void tearDownClass() throws Exception {
            testNGCucumberRunner.finish();
        }
    }
    
    

    そして、機能を実行します。

    どうもありがとう、Grasshopper !!!

あなたの答え