您的位置:首页 > 编程语言 > Java开发

适合selenium rc初学者的一个简单java测试案例

2011-12-16 13:31 537 查看

适合selenium rc初学者的一个简单java测试案例

刚学selenium rc的同学们也许会被rc相对复杂的安装配置所吓到。不用怕!一步一步来,慢慢的你也会成为高手。这里我给初学者介绍一个非常简单的java测试案例,做这个测试案例之前首先要安装selenium-sever.jar和selenium-java-client-driver.jar到Java IDE里,这个测试案例我还用到了testframwork TestNG, 也要安装到Java IDE里,或者使用JUnit也行。流览器使用的是firefox.

测试步骤:

1. 打开google英文页面

2. 在搜索框里输入hello world

3. 点击搜索按钮

4. 点击页面上第一个链接Helloworld program - Wikipedia, the free encyclopedia

5. 检查wikipedia页面上是否有“Hello,World!”出现

这是一个非常简单的测试案例吧,用selenium ide就可以录制它,那么我们怎么通过selenium rc去实现它呢?

我们知道使用selenium rc需要启动selenium server,一般来说我们可以通过cmd打开它,这里我介绍一种直接用java程序打开server的方法,就不必使用cmd了。Java code如下:

测试案例我用了两个java class, 一个叫AbstractTest.java,里面设置了selenium server和selenium的基本配置。另一个叫HelloWorld.java,里面就是我们具体的测试步骤。HelloWorld继承了AbstractTest里的selenium变量。selenium server就是通过AbstractTest里的startSeleniumServer()打开的,要用代理上网的也可以通过此方法实现。

复制代码

package selenium;

import java.io.File;

import org.openqa.selenium.server.RemoteControlConfiguration;

import org.openqa.selenium.server.SeleniumServer;

import org.testng.annotations.AfterClass;

import org.testng.annotations.BeforeClass;

import com.thoughtworks.selenium.DefaultSelenium;

import com.thoughtworks.selenium.Selenium;

public abstract class AbstractTest {

protected Selenium selenium;

protected static SeleniumServer seleniumServer;

String browser = "*chrome";

String url = "http://www.google.com";

@BeforeClass

public void setUp() throws Exception {

startSeleniumServer();

selenium = new DefaultSelenium("localhost", 4444, browser, url);

selenium.start();

}

private void startSeleniumServer() throws Exception {

String template = "C:/workspace/selenium-templates";

RemoteControlConfiguration rcConf = new RemoteControlConfiguration();

rcConf.setPort(4444);

rcConf.setReuseBrowserSessions(true);

rcConf.setBrowserSideLogEnabled(true);

rcConf.setSingleWindow(true);

rcConf.setFirefoxProfileTemplate(new File(template));

seleniumServer = new SeleniumServer(rcConf);

seleniumServer.start();

}

@AfterClass

protected void stop() {

selenium.stop();

seleniumServer.stop();

}

}

复制代码

package selenium;

import org.testng.Assert;

import org.testng.annotations.Test;

public class HelloWorld extends AbstractTest{

private final String DEFAULT_TIMEOUT = "30000";

@Test

public void searchHelloWorldByGoogle(){

selenium.open("http://www.google.com/webhp?hl=en");

selenium.waitForPageToLoad(DEFAULT_TIMEOUT);

selenium.type("//td/input[@title='Google Search']", "hello world");

selenium.click("btnG");

selenium.waitForPageToLoad(DEFAULT_TIMEOUT);

selenium.click("//a[@href='http://en.wikipedia.org/wiki/Hello_world_program']");

selenium.waitForPageToLoad(DEFAULT_TIMEOUT);

Assert.assertTrue(selenium.isTextPresent("\"Hello, World!\""));

}

}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: