power_settings_new

Hi all,

First of all, Merry Christmas and Happy new year!

I am writing an Web automation project using Cucumber + Java + Maven, and i want to run tests on both Chrome and IE.

With a previous project in C#, we used Autofac to configure which test scenario will be executed on both browsers, or only Chrome or IE.  

Example scenario:

  Feature: Demo

@Chrome @IE   # Will be executed on both

Scenario: Test 1

Given I test

##

@Chrome  # Will be executed only on Chrome

Scenario: Test 1

Given I test

 And an autofac.dll file to implement code behind which handles the tags (@Chrome and @IE). 

But i don't know the detail implementation, so i wanna ask your help to find out a solution in Java and Cucumber.

Please help to provide your idea!

Thank you so much!

MinMin

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:
To avoid this verification in future, please log in or register.

2 Answers

by
+1 vote

I don't think the solution presented by Sanjeev is particularly good. There is no reason couple your tests with your browsers. Tests should be browser agnostic. Imagine the mess you will have to have Chrome tests, Firefox tests, Safari tests, and then platform tests such as Samsung browser for mobile, etc.

The solution I created for my project resolves the browser (and associated driver) as well as the test environment by using runtime arguments passed through the Maven options.

To process these runtime parameters, I used Cucumber's @BeforeClass hook to initialize the driver and set the test environment based on the passed arguments. This hook must be used in a static method in your TestRunner class; otherwise it will not work. @BeforeClass executes before the @Before hook which it is used to call the setup for each scenario declared on the tests you're running.

My solution looks something like this:

@RunWith(Cucumber.class)
@CucumberOptions(
 features= {"features"},
 glue="stepDefinition"
 ,plugin= {"..."}
 ,dryRun = false
,tags= {"@MyTests"}
 )
public class TestRunner {

  @BeforeClass
  public static void setup() throws Exception {

     String browser = System.getProperty("browser");
     String env = System.getProperty("myEnvironment");
     WebDriver driver = TestUtils.initializeDriver(browser);
  }
}

"browser": You don't have to define "browser" as a system property. All you do is pass the value as a runtime argument in the Maven options. I will show you how later.
"myEnvironment": There is no such system property. Therefore, you must define it somewhere. Since you are using Maven, you can define this property in the POM under the maven-surefire-plugin configuration node.  This is how you do this:

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<includes/>
<systemPropertyVariables>
<environment>${myEnvironment}</environment>
</systemPropertyVariables>

</configuration>
</plugin>

Now, to pass the parameters, you will add the following to your Maven options:
 

-Dbrowser=chrome -DmyEnvironment=dev

The beauty of this approach is that you can define many custom properties on your POM and use the "-D" option to pass them to your program dynamically.

Lastly, initialize the driver.  my "initializeDriver" method simply takes the String and looks up the path to the driver that I have stored in a config file. For example, if I pass "chrome" as argument as shown above, in the method I do the following:
 

if ("chrome".equals(key)) {
   System.setProperty("webdriver.chrome.driver", path);
   return new ChromeDriver();
} else if ...

The returned WebDriver I then store in a utility class that holds the driver that is used throughout my tests.

When I am running my tests from an IDE, such as Eclipse, I add the Maven options to the VM Arguments. Therefore, I can run @MyTests with any browser and environment combination.

by
0 votes
In case of you are using JUnit to run your scenarios, you can use @CucumberOptions to specify tags, and use separate test classes for the two tags:

@RunWith(Cucumber.class)
@CucumberOptions(tags = "@Chrome")
public class ChromeTest { }

@RunWith(Cucumber.class)
@CucumberOptions(tags = "@Firefox")
public class FirefoxTest { }
Launching of the browser should be possible with scenario hooks, which can also specify to which tags they apply:

@Before("@Chrome")
public void launchChrome() {
}
Apart from this, you can even create a .feature file that passes browser as a parameter.

E.g.

Examples:

| browser | driver | property |
|chrome| "webdriver.chrome.driver" |"\\chromedriver.exe" |
|Mozilla| |     |
| ie | "webdriver.ie.driver" |"\\IEDriverServer.exe" |
This will generate 3 given statements ,then one can fill the required code,example is below

@Given("^open chrome set \"(.*?)\" and \"(.*?)\"$")
public void open_chrome_set_and(String arg1, String arg2) throws Throwable {
    // Write code here that turns the phrase above into concrete actions

    System.setProperty(arg1,arg2);
    driver  = new ChromeDriver();

    driver.get(url);
}

@Given("^open Mozilla set  and $")
public void open_Mozilla_set_and() throws Throwable {
    // Write code here that turns the phrase above into concrete actions

    driver = new FirefoxDriver();

    driver.get(url);
}

@Given("^open ie set \"(.*?)\" and \"(.*?)\"$")
public void open_ie_set_and(String arg1, String arg2) throws Throwable {
    // Write code here that turns the phrase above into concrete actions

    System.setProperty(arg1,arg2);
    driver = new InternetExplorerDriver();   

    driver.get(url);
}
Hope that helps!
add
...