2

我想根据属性文件中设置的浏览器名称在不同的浏览器上运行我的 selenium 测试。
我有一个名为 as 的方法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 文件运行我的测试。但是我收到错误 -

无法开始新会话。可能的原因是远程服务器地址无效或浏览器启动失败。”

有人可以建议出了什么问题还是我错过了什么?

4

0 回答 0