我正在使用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();
}
}
以下是我的项目结构:
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>