Running MVN test from cmd

Running test


Go to your project directory from terminal and hit following commands

  • mvn test (defualt will run on local firefox browser)
  • mvn test "-Dbrowser=chrome" (to use any other browser)
  • mvn test -Dcucumber.options="classpath:features/my_first.feature" to run specific feature.
  • mvn test -Dcucumber.options="–-plugin html:target/result-html" to generate a HTML report.
  • mvn test -Dcucumber.options="–-plugin json:target/result-json" to generate a JSON report.

Browser is specified in Hooks.java

package step_definitions;

import java.net.MalformedURLException;

import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.chrome.ChromeDriver;

import cucumber.api.Scenario;
import cucumber.api.java.After;
import cucumber.api.java.Before;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriverService;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.safari.SafariDriver;


public class Hooks{
    public static WebDriver driver;

    
    @Before
    /**
     * Delete all cookies at the start of each scenario to avoid
     * shared state between tests
     */
    public void openBrowser() throws MalformedURLException {
        String browser = System.getProperty("BROWSER");
        //String phantomjs_dir = System.getenv("PHANTOMJS_PATH");

        switch (browser)
        {
            case "chrome":
                driver = new ChromeDriver();
                break;
            case "firefox":
                driver = new FirefoxDriver();
                break;
            case "ie":
                driver = new InternetExplorerDriver();
                break;
            case "safari":
                driver = new SafariDriver();
                break;
                /*
            case "phantomjs":
                DesiredCapabilities caps = new DesiredCapabilities();
                caps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,phantomjs_dir+"//phantomjs.exe");
                driver = new PhantomJSDriver(caps);
                break;
                */
            default:
                driver = new ChromeDriver();
                break;
        }

           System.out.println("Opening Browser...."+browser);
        driver.manage().deleteAllCookies();
    }

 
    
    @After
    /**
     * Embed a screenshot in test report if test is marked as failed
     */
    public void embedScreenshot(Scenario scenario) {
       
        if(scenario.isFailed()) {
        try {
             scenario.write("Current Page URL is " + driver.getCurrentUrl());
//            byte[] screenshot = getScreenshotAs(OutputType.BYTES);
            byte[] screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.BYTES);
            scenario.embed(screenshot, "image/png");
        } catch (WebDriverException somePlatformsDontSupportScreenshots) {
            System.err.println(somePlatformsDontSupportScreenshots.getMessage());
        }
        
        }
        driver.quit();
        
    }
    
}

Report format is specified in the RunCukesTest.java

package step_definitions;

import org.junit.runner.RunWith;
import org.w3c.dom.views.AbstractView;

import cucumber.api.CucumberOptions;
import cucumber.api.testng.AbstractTestNGCucumberTests;
import cucumber.api.junit.Cucumber;


@RunWith(Cucumber.class)
@CucumberOptions(
        features = "classpath:features",
        plugin = {"pretty", "html:target/cucumber-html-report","json:cucumber.json"},
        tags = {}
        )
public class RunCukesTest{
    
}

 

posted @ 2017-12-25 18:37  thisissweetcandy  阅读(120)  评论(0)    收藏  举报