Showing posts with label Selenium. Show all posts
Showing posts with label Selenium. Show all posts

Wednesday, August 8, 2012

Running mavenized JUnit Tests from command line

It has been a while again - I was doing lot of work but habit of blogging faded away. Fortunately I started writing things down, specially simple commands that are essential as a tester... Hopefully I will rapidly publish them.

I wasn't a fan of mavenizing java - at least at the start- but it has changed. The thing with these tools is the relative complexity for smaller scale projects. As a tester, I feel this more since we need small and quick JUnit or Java test programs often.

 If the project is mavenized, it is impossible to build the java path manually. You can export the classpath using dependency plugin, and set the classpath. Go to the project folder and give the command,

mvn dependency:build-classpath -Dmdep.outputFile=cp.txt

This will create the file cp.txt with all the dependencies. Now add the output folder of your project, this is usually,

%ProjectDir%\output\classes in a mavenized project. Don’t forget to add the semicolon!

Now open the text file from notepad, press Ctrl+A and then Ctrl+C to copy everything. Go to command prompt and type

Set CLASSPATH=

Without pressing Enter, right click the mouse and select paste. That will copy all the contents of the file in to the command. Now press enter and the classpath is set.

To verify, type

echo %CLASSPATH%

This should print all the contents you had in cp.txt. Now run the junit tests with command,

java org.junit.runner.JUnitCore MyTestClass


Alternatively, you can use the command line switch -classpath too but the command line is going to be very long!

java -classpath (content of cp.txt) org.junit.runner.JUnitCore MyTestClass



Monday, May 17, 2010

Selenium and Visual Studio 2010 C# (CSharp) Express edition

Again, that question - what tool to use for this automation script? It is not a big script so the risk is low even if the selection is not optimal....

Looked at Watir and Selenium. Watir is more command line oriented than Selenium. I was impressed with both as I could execute a single line in interpretive mode and get something to happen in the browser. Amazing!

Selenium had the advantage of having a recorder. It is really handy to get most of the controls without painstakingly going through the source. In couple of sites there was a reference to a recorder for Watir, but never found the tool. This was the main reason to go to Selenium. It didn't take long to realize that the builtin scripting capabilities are not flexible enough to my work. Then, the next stage -went for Selenium RC. It supports many different languages to drive automation. I went for VS C# 2010 Express, which is free from MS.

I was pleasantly surprised of the compatibility of Selenium generated C# script and the VS C#. All I had to do was changing the Target .net framework version in C# project and then copy the class.
  1. After recording, select the 'C# - Selenium RC' from Options->Format. This will generate the C# class for your test.
  2. Note down the class name from 'public class ' statement. This is the name of the test case.
  3. Go to the VS project properties and change the target framework to .Net framework 2.0. This gave me hours of trouble till found!
  4. Remove any references marked with a yellow icon in Solution Explorer, and then any 'using 'statements marked with red underline in program.cs.
  5. From the same dialog, brows and add all the dlls in 'Selenium-dotnet-clientdriver-<ver>' package. This is in Selenium-remote-control <ver>.zip file. I was using version 1.0.3.
  6. Create a new class. Use the name noted in 2. Cut and past the output from Selenium IDE to this class file.
  7. Add the name space of class to program.cs
  8. Simply create the object, and start calling the SetupTest(). Then write your program, you can use/add/edit functions created by Selenium. Call TearDownTest() at the end, which would close everything.
It was a rare situation where two independent software programs agreed with each other so well :-)