RemoteWebDriver无法转换为SikuliWebDriver

时间:2013-10-16 13:51:16

标签: java selenium webdriver sikuli remotewebdriver

我试图在Sauce On Demand基础设施上运行基于Sikuli WebDriver的测试。 但是我遇到了RemoteWebDriver的问题。

我有这个BaseSikuliWebDriver类

package com.pitito.sikuli.base;

import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import org.openqa.selenium.Platform;
import org.openqa.selenium.remote.Augmenter;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.ITestResult;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;

import com.pitito.core.basetests.BaseLoggingTest;
import com.pitito.selenium.webdriver.RemoteWebDriverSession;
import com.pitito.selenium.webdriver.WebDriverScreenshooter;
import com.pitito.sikuli.webdriver.SikuliFirefoxDriver;

/**
 * Base class for all Sikuli WebDriver tests.
 *
 * @author guillem.hernandez
 */
public abstract class BaseSikuliWebDriverTest {

  Map<String, Object> sauceJob = new HashMap<String, Object>();

  private static SikuliFirefoxDriver sikuliDriver;

  protected SikuliFirefoxDriver driver() {
    return getDriver();
  }

  public static SikuliFirefoxDriver getDriver() {
    return sikuliDriver;
  }

  public static void setDriver(SikuliFirefoxDriver driver) {
    BaseSikuliWebDriverTest.sikuliDriver = driver;
  }

  @Override
  @BeforeMethod(alwaysRun = true)
  protected void setup(Method method, Object[] testArguments) {
    super.setup(method, testArguments);
    String sessionId = method.getName() + "_" + testArguments.hashCode();
    DesiredCapabilities caps = DesiredCapabilities.firefox();
    caps.setCapability("id", sessionId);
    caps.setCapability("name", sessionId);
    caps.setCapability(CapabilityType.BROWSER_NAME, "firefox");
    caps.setCapability("platform", Platform.XP);
    caps.setCapability("version", "21");
    try {
    sikuliDriver = (SikuliFirefoxDriver) new Augmenter().augment(new RemoteWebDriver(new URL("http://"
          + RemoteWebDriverSession.USER + ":" + RemoteWebDriverSession.APIKEY
          + "@ondemand.saucelabs.com:80/wd/hub"), caps));
    } catch (MalformedURLException e) {
      e.printStackTrace();
    }
    setDriver(sikuliDriver);
  }

  @Override
  @AfterMethod(alwaysRun = true)
  protected void teardown(ITestResult tr, Method method) {
    if ((logger() != null) && (tr.getStatus() == ITestResult.FAILURE)) {
      logUnexpectedException(tr.getThrowable());
    }
    super.teardown(tr, method);
    sikuliDriver.quit();
  }

  @Override
  protected void logScreenshot(String screenshotName) {
    logResource(new WebDriverScreenshooter(driver(), screenshotName).getScreenshot());
  }
}

我实施的测试是Sikuli WebDriver示例,代码如下:

package com.pitito.sikuli.tests;

import java.net.MalformedURLException;
import java.net.URL;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement;
import org.testng.annotations.Test;

import com.pitito.sikuli.base.BaseSikuliWebDriverTest;
import com.pitito.sikuli.webdriver.ImageElement;

/**
 * Sikuli Firefox WebDriver Automated Test Example.
 *
 * @author guillem.hernandez
 */
public class SikuliGoogleCodeTest extends BaseSikuliWebDriverTest {

  @Test(groups = { "ES" }, description = "Use Sikuli to search on Google Maps")
  public void testSikuliWebDriverPassingExample_ES() {
    verifySikuliWebDriverPassingTest();
  }

  private void verifySikuliWebDriverPassingTest() {
    // visit Google Map
    driver().get("https://maps.google.com/");

    // enter "Denver, CO" as search terms
    WebElement input = driver().findElement(By.id("gbqfq"));
    input.sendKeys("Denver, CO");
    input.sendKeys(Keys.ENTER);

    ImageElement image;

    // find and click on the image of the lakewood area
    try {
      image = driver().findImageElement(new URL("https://dl.dropbox.com/u/5104407/lakewood.png"));

      image.doubleClick();

      // find and click on the image of the kendrick lake area
      image =
          driver().findImageElement(new URL("https://dl.dropbox.com/u/5104407/kendrick_lake.png"));
      image.doubleClick();

      // find and click the Satellite icon to switch to the satellite view
      image = driver().findImageElement(new URL("https://dl.dropbox.com/u/5104407/satellite.png"));
      image.click();

      // find and click the plus button to zoom in
      image = driver().findImageElement(new URL("https://dl.dropbox.com/u/5104407/plus.png"));
      image.click();

      // find and click the link button
      WebElement linkButton = driver().findElement(By.id("link"));
      linkButton.click();
    } catch (MalformedURLException e) {
      e.printStackTrace();
    }
  }
}

当我尝试运行测试时,我得到的错误就是这个:

[Invoker 18958118] Invoking @BeforeMethod BaseSikuliWebDriverTest.setup(java.lang.reflect.Method, [Ljava.lang.Object;)[pri:0, instance:com.pitito.sikuli.tests.SikuliGoogleCodeTest@137008a]
Failed to invoke configuration method com.pitito.sikuli.base.BaseSikuliWebDriverTest.setup:org.openqa.selenium.remote.RemoteWebDriver$$EnhancerByCGLIB$$52a1cf6f cannot be cast to com.pitito.sikuli.webdriver.SikuliFirefoxDriver

问题在于:

sikuliDriver = (SikuliFirefoxDriver) new Augmenter().augment(new RemoteWebDriver(new URL("http://"
          + RemoteWebDriverSession.USER + ":" + RemoteWebDriverSession.APIKEY
          + "@ondemand.saucelabs.com:80/wd/hub"), caps));

如何远程使用SikuliFirefoxDriver?如何使用SikuliFirefoxDriver投射RemoteWebDriver?我可以这样做吗?

3 个答案:

答案 0 :(得分:2)

据我所知,Selenium Grid服务器无法通过其JSON api传递Sikuli命令(以及用于比较目的的二进制截图)。甚至SauceLabs也没有这种能力。希望有一天它能在雷达上实现。在SauceLabs论坛上,有人问过这个问题(我也用同样的答案回答了这个问题)。

我知道有一个名为Marionette的项目正在进行中,它应该能够自动化浏览器/ Firefox菜单和本机对话框。

答案 1 :(得分:2)

我实现了sikuli驱动程序的远程驱动程序版本。您可以使用它来执行此操作。 请随意分叉: https://github.com/AJ-72/SikuliRemoteWebdriver

答案 2 :(得分:0)

我的猜测是SikuliFirefoxDriver无法扩充,因为它没有被调用为RemoteWebdriver。尝试使用sikuli作为远程webdriver调用它作为所需的功能。 请在这里发布是否有效(如果有可能,我找不到证据,但仍值得一试)