如何在pom.xml中提到的特定浏览器上运行Selenium + Maven测试

时间:2016-07-12 07:36:58

标签: java maven selenium pom.xml

我正在使用Cucumber Java + Maven编写测试脚本,我想通过使用以下代码从browserType获取pom.xml

String browserType = System.getProperty("browser");

但它不起作用。下面是我在常用函数中的代码,用于启动pom.xml

中指定的浏览器
package com.cucumber.utils;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class commonFunctions {

    static String driverPath="D:\\chromedriver";

    public static WebDriver launchBrowser(){

    WebDriver driver;
//  String browserType = "firefox";
    String browserType = System.getProperty("browser");
    String appURL = "http://www.thetestroom.com/webapp";

    switch (browserType){

    case "chrome":
        driver = initChromeDriver(appURL);
        break;

    case "firefox":
        driver = initFirefoxDriver(appURL);
        break;

    default:
        System.out.println(("browser : " + browserType + " is invalid, Launching Firefox as browser of choice.."));
        driver = initFirefoxDriver(appURL);
    }

    return driver;  
}

private static WebDriver initChromeDriver(String appURL) {
    System.out.println("Launching google chrome with new profile..");
    System.setProperty("webdriver.chrome.driver", driverPath
            + "chromedriver.exe");
    WebDriver driver = new ChromeDriver();
    driver.manage().window().maximize();
    driver.navigate().to(appURL);
    return driver;
}

private static WebDriver initFirefoxDriver(String appURL) {
    System.out.println("Launching Firefox browser..");
    WebDriver driver = new FirefoxDriver();
    driver.manage().window().maximize();
    driver.navigate().to(appURL);
    return driver;
}

public static void closeBrowser(WebDriver driver){
    driver.quit();
}

}  

以下是我的项目结构:

  • 我的项目
    • SCR /测试/ JAVA
      • 包1
        • steps.java
      • 包2
        • ContactPage.java
        • LandingPage.java
      • utils的
        • commonFunctions.java
    • SCR /测试/资源     + myFeature.feature
    • 的pom.xml

pom.xml:

<project>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <browser>firefox</browser>
        <appURL>https://www.example.com/webapp</appURL>
        <threads>1</threads>
        <remote>false</remote>
        <seleniumGridURL/>
        <platform/>
        <browserVersion/>
    </properties>
    <dependencies>
        ...
    </dependencies>
</project>

0 个答案:

没有答案