Selenium Web Driver 和 OpenLayers 2.x:如何在地图上识别?

Selenium Web Driver and OpenLayers 2.x: how to do a identify on a map?

我要测试一个使用 OpenLayers 2.x、在 Java 中使用 Selenium Web Driver 并使用 Firefox(我在 Windows 7 上)的网络地图应用程序。

我发现只有这个问题 不能解决我的问题。

我必须测试地图上要素的识别功能,所以:

1) select 我工具栏上的识别按钮(我可以这样做...所以没问题...)

2) 点击地图上的点要素(我做不到....)

3) 关闭显示特征描述数据的对话框(我不能这样做....)

我不能告诉我的应用程序 url 它不是 public 但我可以使用这个简单的测试用例

http://dev.openlayers.org/releases/OpenLayers-2.13.1/examples/markers.html

这显示了我的用例。

点击地图,您将看到地图项的详细信息,然后关闭对话框。

这是我的代码,但它不起作用

package myTestProjects;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;

public class identifyOpenLayersTest_02 {

private static WebDriver driver = null;

public static void main(String[] args)  throws InterruptedException {

    // Create a new instance of the Firefox driver
    System.out.println("Create a new instance of the Firefox driver ...");
    driver = new FirefoxDriver();

    //Put a Implicit wait, this means that any search for elements on the page could take the time the implicit wait is set for before throwing exception
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

    // It is always advisable to Maximize the window before performing DragNDrop action
    System.out.println("Maximize the window ...");
    driver.manage().window().maximize();
    Thread.sleep(3000L);        

    // Launch the OpenLayers 2.x marker sample 
    System.out.println("Launch the OpenLayers 2.x marker sample  ...");
    Thread.sleep(3000L); 
    driver.get("http://dev.openlayers.org/releases/OpenLayers-2.13.1/examples/markers.html");

    // Create a new Action instance 
    System.out.println("Create a new Action instance ...");
    Actions act = new Actions(driver);

    // Find the viewport inside in witch there is the map   
    System.out.println("Find the viewport inside in witch there is the map ...");
    Thread.sleep(3000L);
    WebElement el = driver.findElement(By.id("OpenLayers_Map_2_OpenLayers_ViewPort"));

    // Start the action sequence 
    System.out.println("Start the action sequence  ...");
    Thread.sleep(3000L);
    act.click().perform();

    // Identify marker
    System.out.println("Identify marker at 285, 111 ...");
    Thread.sleep(3000L);
    act.moveToElement(el, 285, 111).click().build().perform();            

    // Print TEST = OK!!
    System.out.println("TEST = OK !!");
    //driver.quit();

        }
} 

建议?样品?

编辑:
我有一些关于这个问题的消息(不幸的是还不是解决方案....)。

如果您运行我的代码使用这个 OpenLayers 示例

http://dev.openlayers.org/releases/OpenLayers-2.13.1/examples/getfeatureinfo-popup.html

你会发现它有效,所以问题似乎与坐标无关。

我认为问题在于使用这个示例

http://dev.openlayers.org/releases/OpenLayers-2.13.1/examples/markers.html

特征的描述数据放在DIV中,如图所示....

如何在点击后显示 DIV?

对此有任何帮助吗?

非常感谢您!!!

我解决了!!

这是有效的代码!

package myTestProjects;

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.firefox.internal.ProfilesIni;
import org.openqa.selenium.interactions.Actions;


public class identifyOpenLayersTest_02 {

private static WebDriver driver = null;

public static void main(String[] args)  throws InterruptedException {

    //Create a new profile and load my Firefox default profile 
    System.out.println("Creo un nuovo profilo e vi carico il profilo Firefox di default ...");
    Thread.sleep(3000L);
    ProfilesIni profile = new ProfilesIni();        
    FirefoxProfile ffProfile = profile.getProfile("default");

    // Create a new instance of the Firefox driver using my new Firefox profile  
    System.out.println("Creo una nuova sessione del browser Firefox ...");
    Thread.sleep(3000L);
    driver = new FirefoxDriver(ffProfile);

    //Put a Implicit wait, this means that any search for elements on the page could take the time the implicit wait is set for before throwing exception
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

    // It is always advisable to Maximize the window before performing DragNDrop action
    System.out.println("Maximize the window ...");
    driver.manage().window().maximize();
    Thread.sleep(3000L);        

    // Launch the OpenLayers 2.x marker sample 
    System.out.println("Launch the OpenLayers 2.x marker sample  ...");
    Thread.sleep(3000L); 
    driver.get("http://dev.openlayers.org/releases/OpenLayers-2.13.1/examples/markers.html");

    // Create a new Action instance 
    System.out.println("Create a new Action instance ...");
    Actions act = new Actions(driver);

    // Find the viewport inside in witch there is the map   
    System.out.println("Find the viewport inside in witch there is the map ...");
    Thread.sleep(3000L);
    //WebElement el = driver.findElement(By.id("OpenLayers_Map_2_OpenLayers_ViewPort"));
    WebElement el = driver.findElement(By.className("olAlphaImg"));

    // Start the action sequence 
    System.out.println("Start the action sequence  ...");
    Thread.sleep(3000L);
    act.click().perform();        

    // Perform the click operation that opens new window
    // Identify marker
    System.out.println("Identify marker at 285, 111 ...");
    Thread.sleep(3000L);
    act.moveToElement(el, 285, 111).click().build().perform();  

    // Print TEST = OK!!
    System.out.println("TEST = OK !!");
    //driver.quit();

        }
} 

在这种情况下,解决方案是考虑到标记直接位于地图图像上,因此它们是浏览器页面的组成部分,因此您必须引用正确的元素。

"core" 代码是关于这行的

//WebElement el = driver.findElement(By.id("OpenLayers_Map_2_OpenLayers_ViewPort"));
    WebElement el = driver.findElement(By.className("olAlphaImg"));

注释行是指视口的错误代码行,正确代码行是指对象"olAlphaImg"。

就这些了!

我希望这对其他人有用!

切萨雷