在Selenium的网格概念

时间:2016-06-16 11:03:20

标签: selenium-grid

我已经用网格概念编写了浏览器打开的代码。我写了以下代码。我需要知道它的正确与否。

properties
----------
HUB=localhost
PORT=4444
Browser = chrome
Url=http://demo.guru99.com/v4/index.php

Code
----
package processor;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

import org.openqa.selenium.Platform;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

public class Browser {

    public static void main(String[] args) throws FileNotFoundException, IOException {

        RemoteWebDriver driver;
        Properties prop;
        prop = new Properties();
        prop.load(new FileInputStream("./config.properties"));

        String hub = prop.getProperty("HUB");
        String port = prop.getProperty("PORT");
        String browser = prop.getProperty("Browser");
        String url = prop.getProperty("Url");


        if (browser.equalsIgnoreCase("chrome") || browser.equalsIgnoreCase("ie")
                || browser.equalsIgnoreCase("firefox")) {



            if (browser.equalsIgnoreCase("chrome")) {
                try {
                    DesiredCapabilities dc;
                    dc = new DesiredCapabilities();
                    dc.setBrowserName(browser);
                    dc.setPlatform(Platform.WINDOWS);
                    System.setProperty("webdriver.chrome.driver", "./drivers/chromedriver.exe");
                    driver = new ChromeDriver();
                    System.out.println("Chrome Browser is Initialising..........");
                    driver.manage().window().maximize();
                    driver.get(url);
                } catch (Exception e) {
                    System.out.println("Problem Occurred while Initialising Chrome Browser. Check for the Driver Name & Path !!!!!!!!!!");
                }
            }

            if (browser.equalsIgnoreCase("ie")) {
                try {
                    DesiredCapabilities dc;
                    dc = new DesiredCapabilities();
                    dc.setBrowserName(browser);
                    dc.setPlatform(Platform.WINDOWS);
                    System.setProperty("webdriver.ie.driver", "./Drivers/IEDriverServer.exe");
                    driver = new InternetExplorerDriver();
                    System.out.println("Internet Explorer is Initialising..........");
                    driver.manage().window().maximize();
                    driver.get(url);
                } catch (Exception e) {
                    System.out.println("Problem Occurred while Initialising Internet Explorer. Check for the Driver Name & Path !!!!!!!!!!");
                }
            }

            if (browser.equalsIgnoreCase("firefox")) {
                try {
                    DesiredCapabilities dc;
                    dc = new DesiredCapabilities();
                    dc.setBrowserName(browser);
                    dc.setPlatform(Platform.WINDOWS);
                    driver = new FirefoxDriver();
                    System.out.println("Firefox is Initialising..........");
                    driver.manage().window().maximize();
                    driver.get(url);
                } catch (Exception e) {
                    System.out.println("Problem Occurred while Initialising Firefox. Check for the Driver Name & Path !!!!!!!!!!");
                }
            }
        } 

        else {
            System.out.println("Invalid Browser. Check Browser Name in Properties File.......... ");
        }       
    }
}

请查看代码并告诉我正确的代码。网格概念我需要申请。

1 个答案:

答案 0 :(得分:1)

要在网格中使用selenium,您需要启动远程 webdriver。你在这里做的是启动一个类型的本地webdriver:

driver = new ChromeDriver();
driver = new InternetExplorerDriver();
driver = new FirefoxDriver();

这不是您从网格请求浏览器的方式。 此外,驱动程序的路径(System.setProperty("webdriver.chrome.driver", "./drivers/chromedriver.exe");)必须提供给网格,因此您无法在此处的代码中设置此路径。 您可以按如下方式执行此操作:

java -jar selenium-server-standalone-2.53.0.jar -role hub -Dwebdrivers.chrome.driver=chromedriver.exe

您目前正在做的是创建本地网络驱动程序,您根本没有连接到网格。连接到网格的过程如下:

driver = new RemoteWebDriver(hub, dc);