java - 注释未在带有 cucumber-jvm 的 SharedDriver 中触发

标签 java selenium annotations webdriver cucumber-jvm

这让我抓狂。我正在使用 cucumber-jvm 运行一个测试框架,并试图让它截屏。 我查看了提供的 java-webbit-websockets-selenium 示例,并实现了使用 SharedDriver 模块调用我的 webdriver 的相同方法。 出于某种原因,我的@Before 和@After 方法没有被调用(我在其中放置了打印语句)。任何人都可以阐明吗?

共享驱动程序:

package com.connectors;

import cucumber.api.java.After;
import cucumber.api.java.Before;
import cucumber.api.Scenario;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.support.events.EventFiringWebDriver;

import java.util.concurrent.TimeUnit;


public class SharedDriver extends EventFiringWebDriver {




    private static final WebDriver REAL_DRIVER = new FirefoxDriver();

        private static final Thread CLOSE_THREAD = new Thread() {
            @Override
            public void run() {
                REAL_DRIVER.close();
            }
        };

        static {
            Runtime.getRuntime().addShutdownHook(CLOSE_THREAD);
        }

        public SharedDriver() {
            super(REAL_DRIVER);
            REAL_DRIVER.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
            REAL_DRIVER.manage().window().setSize(new Dimension(1200,800));
            System.err.println("DRIVER");
        }

        @Override
        public void close() {
            if(Thread.currentThread() != CLOSE_THREAD) {
                throw new UnsupportedOperationException("You shouldn't close this WebDriver. It's shared and will close when the JVM exits.");
            }
            super.close();
        }

        @Before()
        public void deleteAllCookies() {
            manage().deleteAllCookies();
            System.err.println("BEFORE");
        }

        @After
        public void embedScreenshot(Scenario scenario) {
            System.err.println("AFTER");
            try {
                byte[] screenshot = getScreenshotAs(OutputType.BYTES);
                scenario.embed(screenshot, "image/png");
            } catch (WebDriverException somePlatformsDontSupportScreenshots) {
                System.err.println(somePlatformsDontSupportScreenshots.getMessage());
            }
        }


    }

步骤文件:

package com.tests;

import com.connectors.BrowserDriverHelper;
import com.connectors.SharedDriver;
import com.connectors.FunctionLibrary;

import cucumber.api.Scenario;
import cucumber.api.java.After;
import cucumber.api.java.Before;
import cucumber.api.java.en.And;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
import cucumber.runtime.PendingException;
import org.openqa.selenium.*;


import java.io.File;
import java.util.List;
import java.util.concurrent.TimeUnit;
import static org.junit.Assert.*;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.support.events.EventFiringWebDriver;

/**
 * Created with IntelliJ IDEA.
 * User: stuartalexander
 * Date: 23/11/12
 * Time: 15:18
 * To change this template use File | Settings | File Templates.
 */
public class addComponentsST {
    String reportName;
    String baseUrl = BrowserDriverHelper.getBaseUrl();
    private final WebDriver driver;

    public addComponentsST(SharedDriver driver){
        this.driver = driver;
        driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
    }

    @Given("^I have navigated to the \"([^\"]*)\" of a \"([^\"]*)\"$")
    public void I_have_navigated_to_the_of_a(String arg1, String arg2) throws Throwable {
        // Express the Regexp above with the code you wish you had
        assertEquals(1,2);
        throw new PendingException();
    }

CukeRunner:

package com.tests;

import org.junit.runner.RunWith;

import cucumber.api.junit.Cucumber;

@RunWith(Cucumber.class)
@Cucumber.Options(tags = {"@wip"}, format = { "pretty","html:target/cucumber","json:c:/program files (x86)/jenkins/jobs/cucumber tests/workspace/target/cucumber.json" }, features = "src/resources/com/features")
public class RunCukesIT {
}

最佳答案

SharedDriver放在与RunCukesIT相同的包中,即com.tests

当使用 JUnit runner 时,胶水变成了单元测试包(此处为 com.tests),这是 cucumber-jvm 将“扫描”类以获取注释的地方。

关于java - 注释未在带有 cucumber-jvm 的 SharedDriver 中触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13532825/

相关文章:

java - Eclipse SWT GUI - StyledText 控件 - 每个字符等宽

java - Selenium webdriver webelemenet.isSelected() 返回不正确的值

java - 如何限制Java公共(public)类在其Jar之外的使用?

java - 算法类(class): Output of int sort and method to sort Strings

Java Swing : how to reflect changes made to xml file immediately to visual tree?

java - 在 Apache Beam 中使用 defaultNaming 进行动态窗口写入

python - 热图 Seaborn fmt ='d' 错误

python - Selenium 不适用于为避免检测而修改的 chromedriver

java - Selenium Webdriver 代码中的这些额外方法是什么?

java - 不受支持的 SuppressWarnings CloneDoesntCallSuperClone、FieldCanBeLocal、ObjectAllocationInLoop、UnusedAssignment、WeakerAccess