我想根据属性文件中设置的浏览器名称在不同的浏览器上运行我的selenium测试
我有一个名为initiateDriver()
的方法,我在其中获取属性文件中设置的浏览器名称(有效值为ff,chrome或ie),并为每个Web驱动程序类型执行必要的设置。此方法将WebDriver对象返回到我的方法。
public WebDriver initiateDriver()
{
// Created webdriver instance
WebDriver _drv = null;
String IEDriverPath, ChromeDriverPath;
try
{
//Get the Browser Name set in the properties file
String browserType = loadPropertiesFile("BrowserName");
Log4j.logger.warn("Browser name-----------"+browserType);
//Test if the browser is IE
if (browserType.equalsIgnoreCase("ie"))
{
//Currently, IEDriverServer.exe is copied on to Drivers folder of framework
IEDriverPath= "\\Drivers\\IEDriverServer.exe";
//Set the required properties to instantiate IE driver. Place any latest IEDriverServer.exe files under Drivers folder
System.setProperty("webdriver.ie.driver", IEDriverPath);
DesiredCapabilities cap= new DesiredCapabilities();
cap.setCapability("ignoreProtectedModeSettings", true);
_drv = new InternetExplorerDriver(cap);
}
//Check if BrowserType is set to Firefox
else if (browserType.equalsIgnoreCase("ff") || browserType.equalsIgnoreCase("firefox"))
{
//Getting the default Firefox with some settings
FirefoxProfile fp = new FirefoxProfile();
fp.setAcceptUntrustedCertificates(false);
//setting the Firefox preference "auto upgrade browser" to false and to prevent compatibility issues
fp.setPreference("app.update.enabled", false);
_drv = new FirefoxDriver();
}
//Check if BrowserType is set to Chrome
else if (browserType.equalsIgnoreCase("chrome"))
{
//Currently, chromedriver.exe is copied on to Drivers folder of framework
ChromeDriverPath= "\\Drivers\\chromedriver.exe";
//Set the required properties to instantiate Chrome driver. Place any latest Chromedriver.exe files under Drivers folder
System.setProperty("webdriver.chrome.driver", ChromeDriverPath);
ChromeOptions options = new ChromeOptions();
options.addArguments("--start-maximized");
_drv= new ChromeDriver(options);
}
else
{
Reporter.log("Invalid browser name. Please check the Resources/PropertiesLocation.properties file.");
System.out.println("Invalid browser name. Please check the Resources/PropertiesLocation.properties file.");
System.exit(0);
}
}
catch(Exception e)
{
System.out.println(e.getMessage());
Reporter.log("Enter valid browser name---");
System.exit(0);
}
return _drv;
}`
我在下面的课程中调用此方法。
public class SmokeTest1
{
WebDriver d;
@BeforeClass
public void setUp() throws Exception
{
d = gm.initiateDriver();
d.manage().window().maximize();
d.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
@Test
public void firstSmokeTest() throws Exception
{
loginTest(d);
}
}
我正在通过ant build.xml文件运行我的测试。但是我收到错误 -
无法启动新会话。可能的原因是远程服务器的无效地址或浏览器启动失败。“
有人可以说明出了什么问题,或者我错过了什么?