Category Archives: Selenium-RC

WebDriverBackedSelenium example on github now

I had a small WebDriverBackedSelenium example in my Selenium 2 WebDriver API Course codebase.

Since the Selenium API has been deprecated by the main WebDriver project, although it can still be found in the current versions of Selenium WebDriver, I have moved the example to a separate github project.

https://github.com/eviltester/webDriverBackedExample

It isn’t much. It just shows how to get started with the API.

But I’m making the split now, so that:

  • students no longer see the deprecation messages when the compile the code source and think they have a problem
  • my main code won’t break badly on the future version of WebDriver which removes the Selenium API

 

Posted in Migrating to WebDriver, Selenium Simplified Blog, Selenium-RC, WebDriver | Leave a comment

Selenium Simplified Book Source code updated for Selenium 2

I have updated sourcecode for the book to work fully with Selenium 2.1.0

You can download it from here:

Yes, the ‘2ndEdition’ on the back of the filename does mean that I am working on an updated version to update it for Selenium 2.

Current e-book purchasers can already download a beta of the 2nd edition while I finish updating it, from their re-download page.

I suspect that the 2nd edition will have a rebranding and possibly a name change to “Selenium RC Simplified” to reflect the rebranding of Selenium into.

  • Selenium 2 (overall version)
  • Selenium 2.1.0 (specific version)
  • Selenium RC (the Selenium API and RC Server)
  • WebDriver (the WebDriver API)

I will try to use this rebranding from now on when I refer to “Selenium”

Posted in Selenium Simplified, Selenium Simplified Blog, Selenium-RC | Leave a comment

Can I use Selenium Simplified with Selenium 2?

Note: This post was originally written for Selenium 2.0rc2 I have updated it for Selenium 2.0.0 and IDE 1.0.12. And now with supporting video:

link to video Tutorial video on Selenium 2 with Selenium Simplified Book

Selenium has changed since I wrote Selenium Simplified.

Selenium Simplified targets the Selenium 1.0 API. This has not changed in Selenium 2.0

Selenium 2.0 bundles together the Selenium 1.0 server and API and the WebDriver code base.

I recently checked how compatible Selenium Simplified was with Selenium 2.0.0 and was pleasantly surprised.

This blog post describes how to follow the Selenium Simplified book using Selenium 2.0.0 and what you might expect to have happen.

Oh, and it works really well. You don’t have to change very much at all. Once you understand what to download, and how to use the correct jar versions in your build path and ant files, you will be fine.

I’ve structured this blog post so that it maps on to the structure of the book.

Create HTML suites from IDE

The Selenium IDE lets you create HTML suites and save them to html files so we don’t need to create these by hand any more.

Simply add the tests to the side of the ide and save as html.

Then you can run them with -htmlunit as described in the text.

-htmlunit works now

On page 24 there is a big warning saying do not use the -htmlunit to run selenese test suites.

Well this seems to work in Selenium 2 .0.0

I don’t recommend it as a good approach as I think you should move to coding your tests, but if you want to then you should be able to do this in Selenium 2.0.0 much more consistently than in 1.0.3

What to download to use with Selenium 2.0?

From the Selenium Download page

http://seleniumhq.org/download/

You can download either

  • the 0.9.2 build from the previous releases
  • the 1.0.3 build (in the Selenium Client Drivers list (listed in the table as a C# (Selenium RC)). This is the version that the book was written against.
  • the 2.0.0 Standalone Server and the Java 2.0.0 Client Driver (you need to download both if you want to follow through with the Selenium Simplified book, the Client Driver has the source code .jar and the standalone server has everything else)

Since everything works with Selenium 2.0.0 and Selenium 2.0.0 handles the most up to date versions of the browsers you are using, you should probably use this.

If you want to use Selenium 2.0.0 then the jars you need to add to your build paths and ant files is:

Then, any place in the text that talks about:

  • selenium-server.jar, instead use selenium-server-standalone-2.0.0.jar
    • e.g. java -jar selenium-server-standalone-2.0.0.jar

I ran through all the tests and all I did was change the jars that the project used. I used the 2.0.0 jar instead of 1.0.3 jars

SeleneseTestCase has been deprecated

SeleneseTestCase has been deprecated in Selenium 2.0 so you will see it scored through when you work with it using the Selenium 2 jars. This isn’t a problem as Selenium Simplified moves you away from this very quickly.

SaveAs from the IDE instead of view code

On page 38, in previous versions of the IDE you could view source in different languages. Now the view source is always html. You have to saveAs the test case in different formats. So saveAs the test case into Junit4 format.

Attach source-code

Page 57 starts to describe the process of attaching the selenium source code.

The source-code is contained in the Java Client download as selenium-java-2.0.0-srcs.jar, so attach this to your Java project to see the Selenium source-code in the IDE.

Firefox and IE don’t work!

If you are using Selenium 1.0.3 then Firefox 4 probably doesn’t work. I was using Firefox 3.0 and 3.5 with the book

The instructions here might help http://www.qaautomation.net/?p=245 If you want to use early versions of Selenium with Firefox 4.

You could also use one of the compatibility images from Microsoft and install selenium, Eclipse etc. on that – then run against IE6 or IE7, or install a previous version of Firefox.

http://www.microsoft.com/downloads/en/details.aspx?familyid=21eabb90-958f-4b64-b5f1-73d0a413c8ef&displaylang=en

You need Virtual PC to run them

http://www.microsoft.com/windows/virtual-pc/default.aspx

Or, use Selenium 2.0.0 with Firefox 4 or Firefox 5 – this seems to work fine.

I had issues on my Windows 7 machine and running IE 9. I recommend running against Firefox with the Selenium Simplified book. Or running against IE through the virtual machines.

Test Amendments

There are some tests that need to be amended to get working since the underlying apps have changed since they were created.

com.eviltester.seleniumtutorials

MySeleniumTestWithScreenshot.java

Google has changed since the book was released. The next edition will only use pages completely under the authors control.

The following tests will achieve the required results for pages 71 – 76

package com.eviltester.seleniumtutorials;

import com.thoughtworks.selenium.*;

public class MySeleniumTestWithScreenshot extends SeleneseTestCase {

 public MySeleniumTestWithScreenshot(){
  setCaptureScreenShotOnFailure(true);
 }

 public void setUp() throws Exception {
  setUp("http://www.compendiumdev.co.uk", "*firefox");
 }

 public void testAsserts_for_wrong_value() throws Exception {

  selenium.open("/selenium/search.php");
  selenium.type("q", "wrongvalue");
  selenium.click("btnG");
  selenium.waitForPageToLoad("30000");
  assertEquals("selenium-rc - Google Search", selenium.getTitle());
  assertEquals("selenium-rc", selenium.getValue("q"));
 } 

 public void testVerifys_for_wrong_value() throws Exception {

  selenium.open("/selenium/search.php");
  selenium.type("q", "wrongvalue");
  selenium.click("btnG");
  selenium.waitForPageToLoad("30000");
  // verify will not trigger screenshots
  verifyEquals("selenium-rc - Google Search", selenium.getTitle());
  verifyEquals("selenium-rc", selenium.getValue("q"));
 } 

}

All I changed was the base url in setup and the selenium.open links

com.eviltester.seleniumtutorials.chap16

combinedMatches

page 155 CSS selectors

when combining selectors with , the results are not guaranteed to come back in the order of the matched selectors so the test below fails.

@Test
public void combinedMatches(){
 assertEquals("A paragraph of text",selenium.getText("css=p, title"));
 assertEquals("Basic Web Page Title",selenium.getText("css=title, p"));
 assertEquals(3, getCSSCount("title, p"));
 assertEquals(3, getCSSCount("p, title"));
 assertEquals(1, getCSSCount("title"));
 assertEquals(2, getCSSCount("p"));
}

in package com.eviltester.seleniumtutorials.chap16;

Fails on the second assert in Firefox 4.

Interestingly this is different behaviour than the FireFinder plugin in Firefox 4 which would have the first assert fail and the second assert pass.

Using the combining operator the order of the matched items can not be guaranteed. Ignore this section of the text and comment out the first two lines of the test.

com.eviltester.seleniumtutorials.chap20

ShowHTMLOfCombo1WithGetEval

Update: 27/06/2011

I initially thought this was a problem with the getEval. After investigating. It seems to me that a problem exists with assignId using Firefox. The code in the book works fine with *googlechrome

I have raised a bug against this http://code.google.com/p/selenium/issues/detail?id=2121

The getEval does work. If you use “window.document.getElementById(‘combo1’).innerHTML;” This still demonstrates the intent of the test.

If you want to use assignId on firefox then you might have to use getEval to ‘simulate it’ e.g.

Instead of:

selenium.assignId(“xpath//form”, “form1”);

I did:

String result = selenium.getEval(“window.document.forms[0].setAttribute(‘id’,’form1′);”);

To achieve the same effect.

Summary

Pretty much everything worked. This shows that the Selenium team have done a really good job of backwards compatibility.

I suspect that some of the above issues will be resolved by future updates to the drivers.

Once you are past the initial setup sections of the text. You should have no real problems. Unfortunately the initial setup sections is when you need most help, so if you can’t follow the instructions here then email me and I’ll try to help.

Posted in Migrating to WebDriver, Selenium Simplified, Selenium Simplified Blog, Selenium-RC, WebDriver | 2 Comments

Using JUnit Nested Suites with Selenium RC to simulate TestNG suite and grouping annotations

When I use TestNG for my Selenium tests, I really like the BeforeSuite and AfterSuite annotations because then I can share a Selenium session amongst the tests. With nested JUnit suites I can achieve a similar effect, and I can also go some way to grouping my tests to make it easier to create browser specific suites.

(more…)

Posted in Selenium Simplified Blog, Selenium-RC | Tagged , | Leave a comment

Selenium Example – How to Amend the JavaScript of the Web Application Under Test

Why would you want to change the JavaScript of a web application you have to test? Well, if you want to test that events get triggered correctly and the basic structure of the page rather than the actual functionality implemented by the JavaScript. Perhaps the JavaScript does something that you can’t handle with the automation tool, but you still want to cover the basics of the page? This code snippet shows one way to do it.

One of the example applications that I use to illustrate principles in Selenium Simplified has basic Ajax functionality.

This example has a function to validate a value in an input field:

	
	function checkGT30(value) {
		if (value>30)
			alert("Enter a value less than 30");
	}

For the purposes of this example I will change the function so that it always issues an alert saying “changed function” instead of validating the value.

	function checkGT30(value) {
		alert("changed function");
	}

To play along at home you need to setup Java, an IDE, have Selenium Server and Selenium Java Client in your build path. And add Junit 4 to the build path If you don’t know how to do this then the instructions in the preview from Selenium Simplified should help – you can find the preview on Scribd here.

I will include the full source code for the example so you can cut and paste it in to your IDE. This example starts up the selenium server automatically.

package com.eviltester.selenium.junit.javaScript;
import static org.junit.Assert.*;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.server.SeleniumServer;
import com.thoughtworks.selenium.DefaultSelenium;
import com.thoughtworks.selenium.Selenium;

public class ChangeJavaScript {

	static SeleniumServer server;
	Selenium selenium;

	@BeforeClass
	public static void startServer() throws Exception {

		server = new SeleniumServer();
		server.start();

	}

	@Test
	public void overwriteTheJavaScript() {

		selenium = new DefaultSelenium("localhost", server.getPort(),
				"*iexplore", "http://www.eviltester.com");
		selenium.start();
		selenium.open("/selenium/basic_ajax.html");

		// check existing function
		selenium.type("lteq30", "400");
		selenium.fireEvent("lteq30", "blur");
		assertEquals("Enter a value less than 30", selenium.getAlert());

		// change the function by running a script to amend the existing
		// reference in the dom to a new anonymous function

		selenium.runScript("window.checkGT30 = function(value){alert(\"changed function\");};");
		selenium.type("lteq30", "500");
		selenium.fireEvent("lteq30", "blur");
		assertEquals("changed function", selenium.getAlert());
	}

	@After
	public void stopSelenium() {

		selenium.close();
		selenium.stop();

	}

	@AfterClass
	public static void stopServer() {

		server.stop();

	}

}

 

This example:

  • starts up selenium server in the @BeforeClass
  • In the test, starts a selenium session and opens the page
  • checks that the javascript function currently works as expected by generating an alert when the entered value compares greater than 30
  • replaces the code of the function with our own code
  • triggers that function and checks that the alert generated read “changed function”
  • in the @After stops the selenium session
  • in the @AfterClass stops the selenium server

We can do an awful lot with the Javascript functions in Selenium and they make extending the functionality of Selenium-RC easy. Hope that helps.

Posted in Selenium Simplified Blog, Selenium-RC | Leave a comment