ZK Testing with Selenium IDE"

From Documentation
Line 123: Line 123:
 
[[Image:nicer_ids_with_blur_events.PNG]]
 
[[Image:nicer_ids_with_blur_events.PNG]]
  
Replay the script :D finally works, and we see the overview page.
+
Replaying the script finally works :D, and we see the overview page.
  
 
Verify the page url, and some text on the page, to make sure the application redirects correctly after successful login.
 
Verify the page url, and some text on the page, to make sure the application redirects correctly after successful login.

Revision as of 08:16, 20 June 2013

DocumentationSmall Talks2013JulyZK Testing with Selenium IDE
ZK Testing with Selenium IDE

Author
Robert Wenzel, Engineer, Potix Corporation
Date
July 2013
Version
ZK 6.5 (or later)

WarningTriangle-32x32.png This page is under construction, so we cannot guarantee the accuracy of the content!

Introduction

About this article

nice application, how to test ... refer to zats... some things need to be tested in a real browser, so selenium comes into play... how to create tests

programmatically... link to older Small Talk?? or use Selenium-IDE to record/tweak/maintain!! and replay/debug -- this article :D

Environment

  • Firefox (used with 21.0)
  • Selenium IDE 2.0.0
  • JDK 6/7
  • ZK 6.5.3 6/7
  • Maven 3.0

Steps

Initialize your testing environment

  • download link (test project)
  • maven: mvn jetty:run
  • localhost:8080

(- war file ) ???

Initial Situation

  1. Launch Selenium IDE CTRL+ALT+S
  2. open example app localhost:8080
  3. Record login-test and replay it

File:Initially recorded test.PNG

nothing works :'(

Looking at the recorded commands and comparing with page source we notice ids are generated and changing with every request.

File:Changing ids.PNG

Custom Id Generator

As mentioned in other Small Talks about testing one strategy is to use a custom IdGenerator implementation to create predictable, readable (with a business meaning) and easily selectable (by selenium) component ids.

public class TestingIdGenerator implements IdGenerator {

	public String nextComponentUuid(Desktop desktop, Component comp,
			ComponentInfo compInfo) {
        int i = Integer.parseInt(desktop.getAttribute("Id_Num").toString());
        i++;// Start from 1
        
        StringBuilder uuid = new StringBuilder("");
        
        desktop.setAttribute("Id_Num", String.valueOf(i));
        if(compInfo != null) {
        	String id = getId(compInfo);
        	if(id != null) {
        		uuid.append(id).append("_");
        	}
        	String tag = compInfo.getTag();
        	if(tag != null) {
        		uuid.append(tag).append("_");
        	}
        }
        return uuid.length() == 0 ? "zkcomp_" + i : uuid.append(i).toString();
    }

The IDs will look like this:

  • {id}_{tag}_{##} (for components with a given id)
  • {tag}_{##} (for components without a given id - e.g. listitems in a listbox)
  • {zkcomp}_{##} (for other cases, just to make them unique)

e.g. a button in zul-file

<button id="login" label="login" />

will become something like this in HTML in browser (number can vary):

<button id="login_button_12" class="z-button-os" type="button">login</button>

In order to separate production from test configuration we can include an additional config file at startup to enable the TestingIdGenerator only for testing. In ZK this is possible by setting the library property org.zkoss.zk.config.path (also refer to our Testing Tips)

e.g. via VM argument -Dorg.zkoss.zk.config.path=/WEB-INF/zk-test.xml

Windows:

set MAVEN_OPTS=-Dorg.zkoss.zk.config.path=/WEB-INF/zk-test.xml
mvn jetty:run

Linux:

export MAVEN_OPTS=-Dorg.zkoss.zk.config.path=/WEB-INF/zk-test.xml
mvn jetty:run

TODO: improve using maven profiles...

Now we get nicer and deterministic IDs when recording a test case.

After recording login-test again and we get this after replay.

File:Nicer ids.PNG

still fails to replay :(

Why and how to fix? see next section

ZK specific details

ZK heavily uses JavaScript and Selenium does not record all events by default

in our case here the "blur" events of the input fields have not been recorded, but ZK relies on them to determine possible dirty fields. So we need to manually add selenium commands "fireEvent target blur" unfortunately Selenium does not offer a nice equivalent like "focus"

File:Nicer ids with blur events.PNG

Replaying the script finally works :D, and we see the overview page.

Verify the page url, and some text on the page, to make sure the application redirects correctly after successful login.

Selenium Extensions

if you find the syntax of "fireEvent" ugly or think it is too inconvenient to add an additional line just to update an input field there is a possible solution: custom Selenium extensions

This snippet shows 2 simple actions

blur
convenience action to avoid "fireEvent locator blur"
typeAndBlur
combines the type with a blur event, to make ZK aware of the input change automatically
Selenium.prototype.doBlur = function(locator) {
    // All locator-strategies are automatically handled by "findElement"
    var element = this.page().findElement(locator);
    // Fire the "blur" event
    triggerEvent(element, "blur", false);
};

Selenium.prototype.doTypeAndBlur = function(locator, text) {
    // All locator-strategies are automatically handled by "findElement"
    var element = this.page().findElement(locator);

    // Replace the element text with the new text
    this.page().replaceText(element, text);
    // Fire the "blur" event
    triggerEvent(element, "blur", false);
};

user-extensions.js DOWNLOADable (file contains another extension ... more about that later in "making tests more robust and maintainable to UI updates")

add to Selenium IDE Options/Options... [General Tab] Selenium Core extensions


File:Nicer ids with implicit blur events.PNG


Let's record a test for edit and save an existing "feedback item" in the list, and verify the results.

make tests more robust and maintainable

intelligent waiting for ajax updates

automatically run tests Selenium-RC

different browsers ??