0

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: https://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();

        }
} 

Suggestions? Samples?

  • "Click on a point feature on the map (I'm not able to do this ....)" - what problem are you running into? Why are you not able to do this? Do you get an error? – GKS1 Jun 26 '15 at 18:47
  • Hi @GKS1: no, no errors but nothing happens, no attributes are shown. Did you try to execute the code? 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 .... Do you have any or can you fix my code? Thank you in advance!!! – Cesare Jun 26 '15 at 19:27
1

I've solved!!

Here you're the code that works!

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

        }
} 

In this case the solution is about you've to consider that the markers are directly on the map image, so they are components of the browser page, so you've to refer to the right element.

The "core" code is about this rows

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

The commented row is the wrong code row referring to the viewport, the right code row refers to the object "olAlphaImg".

That's all!

I hope this could be useful for others!

Cesare

1

From your code example and the url you provided I figure that you just want to find a specific Element on your map click it and if the popup window opens your test is successful.

Here's how you can do this in a much simpler way:

public class ClickMarkerOnMapOpensCorrectPopupTest {

private static WebDriver driver = null;
private static final By orangeMarkerLocator = By.id("OL_Icon_58");
private static final By orangePopupLocator = By.id("OpenLayers_Feature_61_popup_contentDiv");

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

    driver = new FirefoxDriver();
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

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

    // Because you are searching for only one element you will search by id 
    // (given that your html is consistent regarding usage of ids)
    WebElement orangeMarker = driver.findElement(orangeMarkerLocator);    


    // in your example you don't need actions, WebElement's click method is enough
    orangeMarker.click();

    // now just make sure, that your popup is really displayed, then your test is successful
    WebElement orangePopup = driver.findElement(orangePopupLocator);

    if (orangePopup.isDisplayed()) {
        System.out.println("TEST = OK !!");
    }
    else {
        System.out.println("TEST = NOT OK !!");
    }

    // Step 3 in your desciption says you also want to close the popup again, which is possible via clicking your marker
    orangeMarker.click();

    driver.quit();
} 
}

If you really need to "test" functionality for your product, then I would advise you use a test-framework such as testNG, which makes it way easier to handle several tests and correct behavior in case of failing tests.

My example is not the way I would have written that text regarding Assertions, Exceptions etc., I used your example to make it easier to understand/follow.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.