Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BeforeAWebdriverScenario not being called? - Serenity 3.1.20 #2705

Closed
xescuder opened this issue Feb 11, 2022 · 20 comments
Closed

BeforeAWebdriverScenario not being called? - Serenity 3.1.20 #2705

xescuder opened this issue Feb 11, 2022 · 20 comments

Comments

@xescuder
Copy link

Hi. After upgrading to 3.1.20, I am debugging my extensions and it seems they are not being called.

serenity.conf:

serenity {
  extension.packages = com.infinity.fixtures

class:

package com.infinity.fixtures;

public class BrowserExtensionEnhancer implements BeforeAWebdriverScenario {
    @Override
    public MutableCapabilities apply(EnvironmentVariables environmentVariables, SupportedWebDriver supportedWebDriver, TestOutcome testOutcome, MutableCapabilities mutableCapabilities) {

Thanks a lot.

@wakaleo
Copy link
Member

wakaleo commented Feb 11, 2022

Could you try with 3.2.0, it seems to work fine with that version.

@xescuder
Copy link
Author

xescuder commented Feb 11, 2022

I've upgraded to 3.2.0 but it seems is not being called.

@wakaleo
Copy link
Member

wakaleo commented Feb 11, 2022

Put a breakpoint in the PreScenarioFixtures class to see why it is not loading the classes for your project.

@xescuder
Copy link
Author

PreScenarioFixtures neither is being called :(

@wakaleo
Copy link
Member

wakaleo commented Feb 11, 2022

Sound's like you mght have a version conflict - check that all of the Serenity libraries are using the same version.

@xescuder
Copy link
Author

Maybe, but I am checking it and it seems all are using the same version:

dependencies

@wakaleo
Copy link
Member

wakaleo commented Feb 11, 2022

I can't reproduce the issue - can you produce a sample project that reproduces this behaviour? What driver are you using?

@xescuder
Copy link
Author

Using Chrome:

webdriver {
    driver=chrome
    # Implicit Waits are used to ensure that Serenity does not fail a test if a web element is not immediately
    # present on the page when you first try to use it.
    # Fluent Waits make Serenity wait for an element that is already present on the page to become visible or
    # enabled, before interacting with it
    timeouts {
      implicitlywait = 5000
      fluentwait = 10000
    }
}

@wakaleo
Copy link
Member

wakaleo commented Feb 11, 2022

It should work fine; I suspect a project-specific issue of some kind, which I can't troubleshoot without seeing your project. Put a breakpoint in the newInstance() method of the ChromeDriverProvider class and step through the code - it should call your custom classes in the enhancedCapabilitiesConfiguredIn() method. Step into that method and see what it does.

@xescuder
Copy link
Author

Sorry, but I've already debugged and I don't see any code where is calling the extensions. I've not seen any code to do it (see below):

    private MutableCapabilities enhancedCapabilitiesConfiguredIn(EnvironmentVariables environmentVariables, String options) {
        MutableCapabilities capabilities = this.chromeDriverCapabilitiesDefinedIn(environmentVariables, options).getCapabilities();
        CapabilityEnhancer enhancer = new CapabilityEnhancer(environmentVariables, this.fixtureProviderService);
        return enhancer.enhanced(capabilities, SupportedWebDriver.CHROME);
    }

    private ChromeDriverCapabilities chromeDriverCapabilitiesDefinedIn(EnvironmentVariables environmentVariables, String options) {
        return new ChromeDriverCapabilities(environmentVariables, options);
    }
}



   public MutableCapabilities enhanced(MutableCapabilities capabilities, SupportedWebDriver driver) {
        CapabilitySet capabilitySet = new CapabilitySet(this.environmentVariables);
        this.addExtraCapabilities(capabilities, capabilitySet);
        if (ThucydidesSystemProperty.ACCEPT_INSECURE_CERTIFICATES.booleanFrom(this.environmentVariables, false) && capabilities instanceof DesiredCapabilities) {
            ((DesiredCapabilities)capabilities).acceptInsecureCerts();
        }

        this.addCapabilitiesFromFixtureServicesTo(capabilities);
        AddEnvironmentSpecifiedDriverCapabilities.from(this.environmentVariables).forDriver(driver).to(capabilities);
        if (StepEventBus.getEventBus() != null && StepEventBus.getEventBus().isBaseStepListenerRegistered()) {
            Optional<TestOutcome> currentTestOutcome = StepEventBus.getEventBus().getBaseStepListener().latestTestOutcome();
            if (currentTestOutcome != null && currentTestOutcome.isPresent()) {
                AddCustomDriverCapabilities.from(this.environmentVariables).withTestDetails(driver, (TestOutcome)currentTestOutcome.get()).to(capabilities);
            }
        }

        if (driver.isW3CCompliant()) {
            capabilities = capabilities.merge(W3CCapabilities.definedIn(this.environmentVariables).withPrefix("webdriver"));
        }

        return capabilities;
    }

@xescuder
Copy link
Author

I only see this reference to the extensions package in the debugging, but it's expecting my class implements CustomChromeOptions, not BeforeAWebdriverScenario

capture

@wakaleo
Copy link
Member

wakaleo commented Feb 11, 2022

That's the wrong class - this is where you should be looking:

ChromeDriverProvider.java
image

CapabilitiesEnhancer.java
image

AddCustomDriverCapabilities.java
image

PreScenarioFixtures.java
image

@xescuder
Copy link
Author

I've seen the problem it that it doesn't enter in the code because the next condition is false (no base step listener is registered).

if (StepEventBus.getEventBus() != null && StepEventBus.getEventBus().isBaseStepListenerRegistered()) {
            Optional<TestOutcome> currentTestOutcome = StepEventBus.getEventBus()

I don't see why...

@xescuder
Copy link
Author

This happens because I am debugging from feature. It works if I debug from TestRunner.

@wakaleo
Copy link
Member

wakaleo commented Feb 16, 2022

You can't run a Serenity test directly from the feature, you need to run it through a test runner.

@xescuder
Copy link
Author

Well, except for the BeforeAWebDriver, everything else was working executing/debugging directly from the feature.

@wakaleo
Copy link
Member

wakaleo commented Feb 16, 2022

It will work partially but the classes will not be all configured, so it won't work the way you expect.

@xescuder
Copy link
Author

Thanks!

@wakaleo wakaleo closed this as completed Feb 16, 2022
@AlexanderGamarra
Copy link

HI @xescuder, I have troubles to call BeforeAWebdriverScenario.

This is my code:

************ serenity.properties ************

webdriver.driver = chrome
serenity.extension.packages=net.serenitybdd.browserstack.BeforeABrowserStackScenario

*********** BeforeABrowserStackScenario ***********

    @Override
public MutableCapabilities apply(EnvironmentVariables environmentVariables,
                                 SupportedWebDriver driver,
                                 TestOutcome testOutcome,
                                 MutableCapabilities capabilities) {

    if (driver == SupportedWebDriver.CHROME) {
        String chromeExtensionPath = "src/test/resources/extension/prc1/prc1.crx";

        if (isNotEmpty(chromeExtensionPath)) {
            URL dirUrl = ClassLoader.getSystemResource(chromeExtensionPath);

            ChromeOptions options = new ChromeOptions();

            try {
                options.addExtensions(new File(dirUrl.toURI()));
                capabilities.setCapability(ChromeOptions.CAPABILITY, options);
            } catch (URISyntaxException e) {
                e.printStackTrace();
            }
        }
    }

    return capabilities;

}

My serenity conf don't recognize my class that implements Before Webdriver Scenario.
Please if u can help me let me know.

I'm using serenit-bdd 3.2.0

@xescuder
Copy link
Author

xescuder commented Jun 30, 2022

Hi. I finally solved using FixtureService. This is my code using chrome and firefox extensions:

import net.thucydides.core.fixtureservices.FixtureException;
import net.thucydides.core.fixtureservices.FixtureService;
import net.thucydides.core.util.PathProcessor;
import org.openqa.selenium.MutableCapabilities;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.firefox.FirefoxProfile;

import java.io.File;

public class BrowserFixtureService implements FixtureService {
    private static final String CHROME_EXTENSION = "classpath:plugins/chrome-extension.crx";
    private static final String FIREFOX_EXTENSION = "classpath:plugins/firefox-extension.xpi";

    @Override
    public void addCapabilitiesTo(MutableCapabilities capabilities) {
        if (capabilities.getBrowserName().equals("chrome")) {
            ChromeOptions options = new ChromeOptions();

            PathProcessor pathProcessor = new PathProcessor();
            String extensionPath = pathProcessor.normalize(CHROME_EXTENSION);
            options.addExtensions(new File(extensionPath));
            capabilities.setCapability(ChromeOptions.CAPABILITY, options);
        }

        if (capabilities.getBrowserName().equals("firefox")) {
            FirefoxOptions options = new FirefoxOptions();
            options.setProfile(buildFirefoxProfile());
            capabilities.setCapability(FirefoxOptions.FIREFOX_OPTIONS, options);

        }
    }

    private FirefoxProfile buildFirefoxProfile() {
        PathProcessor pathProcessor = new PathProcessor();
        String extensionPath = pathProcessor.normalize(FIREFOX_EXTENSION);

        FirefoxProfile profile = new FirefoxProfile();
        profile.addExtension(new File(extensionPath));
        return profile;
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants