2011年2月14日 星期一

Excute java Selenium RC test

Requirement:
1. Java JDK
http://www.oracle.com/technetwork/java/javase/downloads/index.html
2. Selenium RC & Selenium Java Client Driver
http://seleniumhq.org/download/
3. Junit
http://www.junit.org/

Installation:
1. Java JDK, C:\Program Files\Java\jdk1.6.0_23
2. Selenium RC, C:\selenium_rc
3. Selenium Java Client Driver, C:\selenium_rc\selenium-2.0b1
4. Junit, C:\junit\junit4.8.1

path:
Add C:\Program Files\Java\jdk1.6.0_23\bin

CLASSPATH:
Add c:\junit\junit4.8.1\junit-4.8.jar;C:\selenium_rc\selenium-2.0b1\selenium-java-2.0b1.jar;C:\selenium_rc\selenium-server-standalone-2.0b1.jar;C:\Program Files\Java\jdk1.6.0_23\bin\javac.exe;C:\Program Files\Java\jdk1.6.0_23\lib\tools.jar;

Example:
You could create a test case from Selenium IDE and export as a JUnit (eg. C:\selenium\tests\GoogleSearch.java).

package com.example.tests;

import com.thoughtworks.selenium.*;
import java.util.regex.Pattern;

public class testSimplesearch extends SeleneseTestCase {
public void setUp() throws Exception {
setUp("http://www.google.com.tw/", "*chrome");
}
public void testTestSimplesearch() throws Exception {
selenium.open("/");
selenium.type("q", "selenium");
selenium.click("btnG");
selenium.waitForPageToLoad("30000");
}
}


This java couldn't execute directly, so we need to add main() and some method to trigger test case.

//package com.example.tests;

import com.thoughtworks.selenium.*;
import java.util.regex.Pattern;
import junit.framework.*;

public class GoogleSearch extends SeleneseTestCase {
public void setUp() throws Exception {
setUp("http://www.google.com.tw/", "*chrome");
}
public void testTestSimplesearch() throws Exception {
selenium.setTimeout("1000000");
selenium.open("/");
selenium.type("q", "selenium");
selenium.click("btnG");
selenium.waitForPageToLoad("30000");
}
public static Test suite() {
//method added
return new TestSuite(GoogleSearch.class);
}
public void tearDown(){
//Added . Will be called when the test will complete
selenium.stop();
}
public static void main(String args[]) {
// Added. Execution will started from here.
junit.textui.TestRunner.run(suite());
}

}


Complier and Execute:
# c:\selenium\tests>javac GoogleSearch.java
# c:\selenium\tests>javac GoogleSearch




沒有留言:

張貼留言