Automating web flow with Selenium and Eclipse IDE

本文详细介绍了如何在Eclipse环境中设置并运行Selenium脚本进行自动化测试,包括下载并导入必要组件,创建测试用例以及通过Eclipse执行测试的过程。此外,文章还展示了如何将Selenium测试用例导出为Java代码,并提供了更新后的代码示例来实现循环搜索功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Some of you may already know Selenium. Basically it’s free and open source tool for web application functional testing. It’s mainly know as replacement for HP’s QuickTestProfessional. I’ve recently discovered Selenium as very helpful for automating data setup for load testing.

Here is a small description howto setup Eclipse and run Selenium script through it.

1) Download Eclipse IDE from http://www.eclipse.org/downloads/
2) Download Selenium RC from http://seleniumhq.org/download/
3) Download Junit from http://www.junit.org/
4) Create new project in Eclipse:

  • Go to File -> New -> Java Project
  • enter project name e.g. “Sample”
  • Click “Finish” button

5) Import Selenium and JUnit packages into the project:

  • In Package Explorer right click project “Sample” and select Properties
  • Go to “Java Build Path” then select “Libraries” tab
  • Click “Add External JARs” and import junit-4.7.jar and selenium-java-client-driver.jar

Now our Eclipse environment should be ready. Next step is to prepare a test case in Selenium IDE. I’ve prepared small test that searches for “linux” word in google. To export test case as Java, in Selenium IDE go to Options -> Format -> Java (Junit) Selenium RC. As a result you should get something like this:

 

package com.example.tests;
 
import com.thoughtworks.selenium.*;
import java.util.regex.Pattern;
 
public class Untitled extends SeleneseTestCase {
        public void setUp() throws Exception {
                setUp("http://change-this-to-the-site-you-are-testing/", "*chrome");
        }
        public void testUntitled() throws Exception {
                selenium.open("/");
                selenium.type("q", "linux");
                selenium.click("btnG");
        }
}

 

Default Junit code generated by Selenium doesn’t use Selenium RC. Because of that I updated the code a little bit to connect to localhost Selenium RC on port 4444. Here is updated version:

 

package com.example.tests;
 
import com.thoughtworks.selenium.*;
import java.util.regex.Pattern;
 
public class Untitled extends SeleneseTestCase {
        public DefaultSelenium selenium;
        public void setUp() throws Exception {
                selenium = new DefaultSelenium("localhost", 4444, "*iexplore", "http://www.google.com/");
                selenium.start();
        }
        public void testUntitled() throws Exception {
                selenium.open("/");
                selenium.type("q", "linux");
                selenium.click("btnG");
        }
}

 

 

Now, lets add it in our Eclipse project:

  • In Package Explorer right click on our “Sample” project and select New -> Class
  • Enter class name “Untitled” and package name “com.example.tests”
  • Click “Finish” button

Before we run our test, we need to start Selenium RC. For that open command line and under selenium-remote-control-1.0.1/selenium-server-1.0.1 run command “java -jar selenium-server.jar”. It will run Selenium RC server on default port 4444.

Now, to start the test just select Run -> Run As -> Junit test. It should open two IE browsers. One for Selenium RC and second with Google results for “linux” word.

Because we run Java code, it’s only up to us how we want to run the test. Below is our example update to search for “linux” word in a loop 5 times with 5 seconds intervals.

 

package com.example.tests;
 
import com.thoughtworks.selenium.*;
import java.util.regex.Pattern;
 
public class Untitled extends SeleneseTestCase {
        public DefaultSelenium selenium;
        public void setUp() throws Exception {
                selenium = new DefaultSelenium("localhost", 4444, "*iexplore", "http://www.google.com/");
                selenium.start();
        }
        public void testUntitled() throws Exception {
                for(int i = 0; i < 5; i++)
                {
                        selenium.open("/");
                        selenium.type("q", "linux");
                        selenium.click("btnG");
                        selenium.waitForPageToLoad("10000");
                        Thread.sleep(5000);
                }
                
        }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值