Monday, June 18, 2012

Using Grid2 to run tests on multiple machines/browsers

Grid allows you to :

  • Scale by distributing tests on several machines (parallel execution)
  • Manage multiple environments from a central point, making it easy to run the tests against a vast combination of browsers / OS.
  • Minimize the maintenance time for the grid by allowing you to implement custom hooks to leverage virtual infrastructure for instance.

Setup:

  • Download the latest selenium-server-standalone-*.jar from http://code.google.com/p/selenium/downloads/list.
  • Start the Hub - open CMD and run: 
    • java -jar selenium-server-standalone-*.jar -role hub
  • Start the nodes - open CMD and run:
    • java -jar selenium-server-standalone-*.jar -role node  -hub http://localhost:4444/grid/register
It is easier to create *.bat files to run the server. Also bat files can be scheduled to run on desired time on remote machines. Just create a *.txt file and rename it to *.bat:

cd c:\selenium-server (location of your server)
java -jar selenium-server-standalone-*.jar -role hub

Using Grid2 in tests:


DesiredCapabilities capability = new DesiredCapabilities();
capability.setBrowserName("firefox");
driver = new RemoteWebDriver(
    new URL("http://<IP of your remote machine>:4444/wd/hub"),
capability);

More info: http://code.google.com/p/selenium/wiki/Grid2

Thursday, February 9, 2012

Webdriver: TestNG + Eclipse + Java

In the beginning it was enough to Run my tests from Eclipse just by clicking Run button. After some time I realized that I need to run tests with different setup - e.g. different users/passwords, different channels, different browsers, run particular classes/methods in groups etc. I found TestNG is what I need and even more. With TestNG it is possible to:
  • parametrize tests
  • run tests in separate sets grouped by classes or methods
  • run tests in multiple threads
  • generate test reports
  • use annotations
  • and more..

The installation and getting started with testng is very well described on their site testng.org - shortly:
  1. Open Eclipse
  2. Help > Install New Software...
  3. Add http://beust.com/eclipse repository and complete the proccess
  4. Window > Show View > Other.. select Java > TestNG > OK
  5. Create a new TestNG class for existing package (test): right-click on the package > New > TestNG class (name it TestExample)
  6. Copy the following code to the class

package test;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import org.testng.annotations.Parameters;
 

public class TestExample {
 @BeforeTest
 @Parameters({"usrName", "pwd"})
 public void setUp(String usrName, String pwd) {
   // code that will be executed before tests
 }
 @Test
 public void test1() {
      // test1 steps
      System.out.println("test1 passed");
 }
 @Test
 public void test2() {
      // test2 steps
      System.out.println("test2 passed");
 }
}


     7. Modify the XML configureation (testng.xml by default) as follows

<?xml version="1.0" encoding="UTF-8"?>
<suite name="Suite" parallel="false">
 <test name="TestName" preserve-order="true">
      <parameter name="usrName"  value="test"/>
      <parameter name="pwd"  value="1234"/>
     <classes>
                  <class name="test.TestExample">
                        <methods>
                              <include name="test1" />
                              <include name="test2" />
                        </methods>
                  </class>
            </classes>
   </test>
</suite>

    8. Run testng.xml

Console results:

[TestNG] Running:
  C:\workspace\webdriver\testng.xml

test1 passed
test2 passed

===============================================
Suite
Total tests run: 2, Failures: 0, Skips: 0
===============================================

TestNG results:



Thursday, January 26, 2012

Getting Started with Webdriver (Java + Eclipse)


WebDriver is a tool for writing automated tests of websites. It aims to mimic the behaviour of a real user, and as such interacts with the HTML of the application. More info here.

Eclipse installation

  1. Download Eclipse (I use Eclipse Classic - don't really know what is the difference between all of them)
  2. Extract the archive to a folder (e.g. c:\eclipse)
  3. Run Eclipse.exe (I would recommend to create a shortcut and put it to the desktop or taskbar)
  4. Create a new Java Project - File/Add/Java Project. Name the project (e.g. webdriver) and accept the rest of the defaults

Webdriver setup

  1. Download the latest selenium-java-<newest version>.zip here
  2. Unzip the files into the project that you just created. I would suggest to create a folder under the project root called /lib (my example: c:\workspace\webdriver\lib\)
  3. Back in Eclipse, right click on your project in the Package Explorer and choose Properties -> Java Build Path
  4. Choose the Libraries tab and click Add External Jars...
  5. Select all the jars that you just unzipped into the /lib/ folder and click OK

Create a new test with Webdriver

  1. Add a new Package - File/New/Package (name it e.g. test)
  2. Select a new package and add a new class - File/New/Class. Give the class name (e.g. Test) and accept the other defaults, but make sure that public static void main(String[] args) checkbox is enabled. 
  3. Copy the following code into your Eclipse project and Run the test!


package test;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Test {

    public static void main(String[] args) {
        WebDriver driver = new FirefoxDriver();
        driver.get("http://www.google.com");
        WebElement element = driver.findElement(By.name("q"));
        element.sendKeys("webdriver how to find elements");
        element.submit();
        System.out.println("Test passed!");
    }

}


As the result Google page should be opened in new FireFox window. The code in Eclipse should look like this: