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

How to Configure Selenium Webdriver in Eclipse and execute some simple test script.

2014-03-05 13:54 603 查看
Steps to install Selenium web driver in Eclipse:

Step
1: Install Java on your computer.

Download and install the Java Software Development Kit (JDK). You download the JDK file from this location:http://www.oracle.com/technetwork/java/javase/downloads/index.html









Note: This
JDK version comes bundled with Java Runtime Environment (i.e. JRE) so we don't need to install the JRE separately.

Step
2: Install Eclipses IDE

Now download “Eclipse IDE for Java Developers”. You can download eclipses from this location: http://www.eclipse.org/downloads/





Make sure to choose correct link for downloading eclipses which corresponds to your OS i.e. for Windows 32 Bit
and 64 Bit versions.

You should be able to download a ZIP file named “eclipse-java-kepler-SR1-win32-x86_64.zip”.





Inside the ZIP file, there is an “eclipse” folder which contains all the application files. So we can extract the
“eclipse” folder anywhere in your PC. Let say we want to extract it to C drive.





Step
3 – Download the Selenium Java Client Driver

You can download the Selenium Java Client Driver from this location:http://docs.seleniumhq.org/download/



Click
on Download link which is associated with java release. Download the jar files. You will find client drivers for other languages there, but only choose the one for Java as shown above in the screenshot.

After downloading you will get a folder which should contain two jar files related to selenium, a lib folder which
also contains jar files inside it. Beside this, there is a change log file as well. Refer to the screenshot below.





Create a new folder with name say 'Selenium' in C drive and extract the jar files to 'Selenium' folder.

Step
4: Create a New project in Eclipse:

Navigate to C drive and open eclipse folder where we have extacted all the application files related to eclipse.
In Eclipse folder you would find a 'eclipse.exe' file. Now click on the .exe file

This would ask you to select a workspace, you may accept the default location or simply create a new workspace
inside C drive and confirm OK.





- Now in Eclipse create a new project and for creating click on File menu –> New –> Java Project -> Name the project
as “testproject” and click finish.





- Create a new Java class, for doing this again click on File menu –> New -> Class then name it as “testclass”
and select the checkbox for 'public static void main (String[] args) and click finish.





On clicking 'Finish' in 'Java Class' wizard, your Eclipse IDE should look like the image below.



-
Now Right-click on testproject and select Properties.

- On the Properties window, click on “Java Build Path”.

- Click on the Libraries tab, and then click “Add External JARs..”

- Navigate to C:\selenium-2.37.0\ (or any other location where you saved the extracted contents of “selenium-2.37.0.zip”
in step 3).

- Add all the JAR files inside and outside the “libs” folder. Your Properties window after adding all the jar file
should now look similar to the image below.





- Finally, click OK and we have completed importing Selenium libraries into our project.

Step
5: Time to Code!!

Now it’s time to write some code. For this, let use the wikipedia site to create a sample script for test execution.

Example
1:









Example
2:

======================================

import java.util.regex.Pattern;

import java.util.concurrent.TimeUnit;

import org.junit.*;

import static org.junit.Assert.*;

import static org.hamcrest.CoreMatchers.*;

import org.openqa.selenium.*;

import org.openqa.selenium.firefox.FirefoxDriver;

import org.openqa.selenium.support.ui.Select;

public class testClass {

private WebDriver driver;

@Before

public void setUp() throws Exception {

driver = new FirefoxDriver();

}

//
Search using keyword through Google search

@Test

public void testtestclass() throws Exception {

//Open
Home Page

driver.get("http://www.google.com");

//Enter
text in search box

driver.findElement(By.name("q")).sendKeys("selenium");

Thread.sleep(1000);

//Click
Search button

driver.findElement(By.name("btnG")).click();

Thread.sleep(10000);

}

@After

public void tearDown() throws Exception {

driver.quit();

}

}

=====================================

Happy testing :)

Thanks & Regards,

Pinaki Mohapatra

Quality Assurance Engineer

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