1

我试图在 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?我可以做吗?

4

3 回答 3

2

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

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

于 2013-11-13T19:17:25.610 回答
2

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

于 2015-06-09T09:37:57.843 回答
0

我的猜测是 SikuliFirefoxDriver 不能被增强,因为它没有被调用为 RemoteWebdriver。尝试将其作为具有 sikuli 所需功能的远程 webdriver 调用。如果可行,请在此处发布(如果可能,我找不到证据,但仍然值得一试)

于 2013-10-18T09:25:30.217 回答