Commits

Kostya Marchenko  committed d63cae9

Added support for Remote WebDriver

  • Participants
  • Parent commits 390ec2c

Comments (0)

Files changed (4)

File src/main/java/sandbox/JsTestHelper.java

 import org.openqa.selenium.firefox.FirefoxDriver;
 import org.openqa.selenium.htmlunit.HtmlUnitDriver;
 import org.openqa.selenium.ie.InternetExplorerDriver;
+import org.openqa.selenium.remote.DesiredCapabilities;
+import org.openqa.selenium.remote.RemoteWebDriver;
 
+import java.net.MalformedURLException;
+import java.net.URL;
 import java.util.ArrayList;
 
 /**
 
 
     /**
+     * Creates instance of remote Web Driver for specified browser type
+     *
+     * @param browserType Type of the browser for which to create driver
+     * @return WebDriver instance
+     */
+    public static WebDriver getRemoteDriver(BrowserType browserType) {
+        DesiredCapabilities capability = null;
+
+        if (browserType == BrowserType.FIREFOX) {
+            capability = DesiredCapabilities.firefox();
+        }
+
+        if (browserType == BrowserType.IE) {
+            capability = DesiredCapabilities.internetExplorer();
+        }
+
+        if (browserType == BrowserType.GOOGLE_CHROME) {
+            capability = DesiredCapabilities.chrome();
+        }
+
+        if (browserType == BrowserType.HTMLUNIT) {
+            capability = DesiredCapabilities.htmlUnit();
+        }
+
+        WebDriver driver;
+        try {
+            driver = new RemoteWebDriver(new URL("http://192.168.35.181:4444/wd/hub"), capability);
+        } catch (MalformedURLException e) {
+            throw new RuntimeException("Can't get remote driver: " + e.toString());
+        }
+
+
+        return driver;
+
+    }
+
+    /**
      * Creates instance of WebDriver of specified type
      *
      * @param browserType defines what type of Driver to create

File src/main/java/sandbox/JsTestRig.java

      * @throws Exception if test rig can not be created
      */
     public static JavascriptExecutor getRig(String pageUrl, String scriptUnderTest, JsTestHelper.BrowserType browserType) throws Exception {
-        driver = JsTestHelper.getDriver(browserType);
+        driver = JsTestHelper.getRemoteDriver(browserType);
         driver.get(pageUrl);
 
         try {
      * @throws Exception if test rig can not be created
      */
     public static JavascriptExecutor getRig(String pageUrl, ArrayList<String> scriptsUnderTest, JsTestHelper.BrowserType browserType) throws Exception {
-        driver = JsTestHelper.getDriver(browserType);
+        driver = JsTestHelper.getRemoteDriver(browserType);
         driver.get(pageUrl);
 
         try {

File src/main/resources/sandbox/ScriptInjector.js

 
 //Append script to the tag
 function appendScript(script, tag) {
-    alert("Appending script");
     if (tag != null) {
         var scriptTag = document.createElement("script");
         scriptTag.setAttribute("type", "text/javascript");
 
 //Write script tag to the document overwriting it's content
 function writeScript(script) {
-    alert("Writing script");
     document.write("<script type=\"text/javascript\" language=\"javascript\">" + script + "</script>");
 }
 

File src/test/java/sandbox/JsTestRigTest.java

     @Test
     public void testIsolatedRigWithSingleScript() throws Exception {
         String scriptUnderTest = "function helloWorld(){ return \"Hello, World!\"}";
-        JavascriptExecutor testRig = JsTestRig.getIsolatedRig(scriptUnderTest, JsTestHelper.BrowserType.HTMLUNIT);
+        JavascriptExecutor testRig = JsTestRig.getIsolatedRig(scriptUnderTest, JsTestHelper.BrowserType.IE);
         String result = (String) testRig.executeScript("return helloWorld();");
         assertThat(result, equalTo("Hello, World!"));
     }