1

I have a .properties file that gives input data for instantiating the WebDriver inside the ServiceHooks class. How can I change this logic to use a cucumber .feature file to drive the parameters that I provide to the WebDriver and implement such a scenario?

Feature: Login page navigation
  Scenario Outline: User navigates to url
    Given I open the browser <browser> on operatingSystem <platform>
    When I navigate to url <url>
    Then I see the login page

    Examples:
      |browser|platform| url |
      |"firefox"|"windows"|"https://loginpage.com"|
      |"chrome"| "windows" |"https://loginpage.com"|

I have some code that evaluates if the driver is remote or not and instantiates the driver (thus opens the browser already) before the test scenario gets executed.

public class ServiceHooks {
    public static WebDriver driver;

    public void getDriverInstance() {

    //if isRemoteDriver evaluests to true or false base on a .properties file
    if (isRemoteDriver.equals("yes")){
                driver = getRemoteDriverInstance(platform, browser, url);
            } else {
                driver = getLocalDriverInstance(platform, browser, url);        
    }


    //platform, browser, url are served from the .properties file as well

        public WebDriver getLocalDriverInstance(String platform, String browser, String url)
            throws MalformedURLException {
        WebDriver localDriver = null;
        DesiredCapabilities capabilities = new DesiredCapabilities();
        //ChromeOptions options = new ChromeOptions();

        // Platforms
        if (platform.equalsIgnoreCase("Windows")) {
            capabilities.setPlatform(Platform.WINDOWS);
        }
        if (platform.equalsIgnoreCase("MAC")) {
            capabilities.setPlatform(Platform.MAC);
        }
        // Browsers
        if (browser.equalsIgnoreCase("chrome")) {
            capabilities = DesiredCapabilities.chrome();
            localDriver = new ChromeDriver();

        }
        if (browser.equalsIgnoreCase("firefox")) {
            capabilities = DesiredCapabilities.firefox();
            localDriver = new FirefoxDriver();

        }
        if (browser.equalsIgnoreCase("ie")) {
            capabilities = DesiredCapabilities.internetExplorer();
            localDriver = new InternetExplorerDriver();

        }

        localDriver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        localDriver.manage().window().maximize();
        localDriver.get(url);
        return localDriver;

    }


        public static WebDriver getRemoteDriverInstance(String platform, String browser, String url)
            throws MalformedURLException {

        String nodeURL = "http://localhost:4444/wd/hub";

        WebDriver remoteDriver = null;
        DesiredCapabilities capabilities = new DesiredCapabilities();

        // Platforms
        if (platform.equalsIgnoreCase("Windows")) {
            capabilities.setPlatform(Platform.WINDOWS);
        }
        if (platform.equalsIgnoreCase("MAC")) {
            capabilities.setPlatform(Platform.MAC);
        }
        // Browsers
        if (browser.equalsIgnoreCase("chrome")) {
            capabilities = DesiredCapabilities.chrome();
        }
        if (browser.equalsIgnoreCase("firefox")) {
            capabilities = DesiredCapabilities.firefox();
        }
        if (browser.equalsIgnoreCase("ie")) {
            capabilities = DesiredCapabilities.internetExplorer();

        }

        // Open the Application
        remoteDriver = new RemoteWebDriver(new URL(nodeURL), capabilities);
        remoteDriver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        remoteDriver.manage().window().maximize();

        remoteDriver.get(url);

        return remoteDriver;
    }


}

I want to be able to run the mentioned cucumber test scenario locally using WebDriver an well as remotely with Selenium Grid.

JustNatural
  • 375
  • 7
  • 19

0 Answers0