Selenium Automation - Cucumber和JUnit - 没有公共静态参数方法

时间:2018-05-25 15:15:46

标签: java exception intellij-idea junit cucumber

嗨社区:尝试将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 个答案:

答案 0 :(得分:0)

不要试图将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 !!!