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

 

适合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()打开的,要用代理上网的也可以通过此方法实现。

复制代码
  1. 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();
        }

    }


复制代码
  1. 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!\""));
        }

    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值