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

Answered

I’ve had to test a web mapping application that use OpenLayers 2.x, using Selenium Web Driver in Java and using Firefox (I’m on Windows 7).

I’ve found only this issue: http://stackoverflow.com/questions/28682195/how-to-use-openlayers-drawfeature-with-selenium-webdriver-in-java-double-click that doesn’t solve my problem.

I’ve had to test the identify function on features on the map, so:

1) Select the identify button on my toolbar (I’m able to do this … so no problem …)

2) Click on a point feature on the map (I’m not able to do this ….)

3) Close the dialog that shows the felatire descriptive data (I’m not able to do this ….)

I can’t give the url of my application because it’s not public but I can use this simple test case:

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

that shows my use case.

Clicking on the map, you’ll see the feature details and then close the dialog.

Here you’re my code that doesn’t work

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();

}
}

I think I’m wrong to enter the coordinates of the point: I’m using Firefox Web Developer Extension to choose them but I’m not sure that they are right: are they absolute or relative to OpenLayers_Map_2_OpenLayers_ViewPort? I can not find examples that make me understand it how it works ….

Suggestions? Samples?

Thank you very much in advance!

Cesare

cesare Train Asked on June 26, 2015 in Selenium WebDriver.
Add Comment
3 Answer(s)
Best answer

Cesare,

I am not able to figure out which element you want to click in the sample url that you have given. You have used coordinates there instead of element locator. I am assuming that you want to click on the red icons that appear on the map. Here is one simple script which clicks on the red icon on map


WebDriver driver = new FirefoxDriver();
driver.get("http://dev.openlayers.org/releases/OpenLayers-2.13.1/examples/markers.html");
/*

* First get the coordinates of the point (The red marker) Point to

* note: These red markers are coming as image elements in the map

* viewport. To find these element coordinates we can do this. 1. First

* find the element using by ID or any such attribute 2. Get the

* Rectangle of the element 3. Click in the middle of the element

*

* This is how we can do this

*/

// *[@id="OL_Icon_61"]/img

// Lets just take the example of img element with id =

// OL_Icon_61_innerImage

Thread.sleep(40000);

WebElement element = driver.findElement(By.className("olAlphaImg"));

Actions act = new Actions(driver);

act.moveToElement(element, 10, 10).click().perform();

Do give us more details if this is not what you are looking for. Also, the map on the page that you have given loads in lot of time. That is the reason of that huge sleep.

Virender

Virender Singh Professor Answered on June 29, 2015.
Add Comment

First of all … thank you for the reply!
Second … sorry for my english!!

Yes i’d like to click on the red icon on the map …..

I’m trying to obtain help on this stuff on different ways …. more details on the same question you can find here http://sqa.stackexchange.com/questions/13597/selenium-web-driver-and-openlayers-2-x-how-to-do-an-identify-on-a-map

If you run my code using this OpenLayers sample

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

you’ll see that it works, so the the problem seems (I think ….),  NOT to be about coordinates.  Click in some point on the green area and you’ll see come up a little baloon wuth some data inside. Everything is working as it should in this case.

Note that in this case I don’t have the marker on the map, because the map is built as images completely server generate, so I think that I need to use the coordinate: it will be the OpenLayers javascript libraries that knows how to manage this coordinates.

I think that the problem is that using this sample

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

the description data of the features is put in a DIV as you can see in the picture …. and I don’t know how to make it appear

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

How can I show this DIV after my click?

Thank you very much in advance ….

Cesare

cesare Train Answered on June 29, 2015.
Add Comment

Hi Virender Singh: I’ve tried your suggested code and it’s working.

I’ve opened a new request on this stuff on a “real” test case that I’ve to do, similar at this one but a little more complex.

Please, could you try to suggest my or fix my code? Here you’re the new request http://forumsqa.com/question/selenium-web-driver-and-openlayers-2-x-request-number-2-how-to-do-an-identify-on-a-map/.

Thank you very much in advance!!!

Cesare

cesare Train Answered on June 30, 2015.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.