How to execute a Selenium test in Java How to execute a Selenium test in Java selenium selenium

How to execute a Selenium test in Java


A good way to run Selenium Java code in Eclipse is to run them as JUnit tests.

1. Create a Maven Project in your Eclipse.
If you haven't done this before, see:

2. Add the following dependencies to your pom.xml file:

<dependency>    <groupId>junit</groupId>    <artifactId>junit</artifactId>    <version>4.7</version>    <scope>test</scope></dependency>    <dependency>        <groupId>org.seleniumhq.selenium</groupId>        <artifactId>selenium-java</artifactId>        <version>2.25.0</version>           </dependency>    <dependency>    <groupId>org.seleniumhq.selenium</groupId>    <artifactId>selenium-firefox-driver</artifactId>    <version>2.33.0</version></dependency> <dependency><groupId>org.seleniumhq.selenium</groupId>    <artifactId>selenium-server</artifactId>    <version>2.25.0</version>    </dependency>

3. Copy your exported Java file into the Maven Project.

4. Add the following imports to the file:

import static org.junit.Assert.*;import org.junit.After;import org.junit.Before;import org.junit.Test;

5. Run the Java file as a JUnit test, like so:

Example from my Eclipse (Version is Kepler)


The previous answer are all legitimate. But to run directly from your eclipse you need do some modifications to your code. You dont need public void main to run a junit code. So Here are the steps to enable you to just copy over the code and paste it in eclipse and run as JUnit test:

  1. Install JUnit in eclipse->Help->eclipse market place-> search for JUnit and install it, restart eclipse.

  2. Create a project in eclipse and a new package, then create a new class with the same name as your selenium IDE exported code, delete everything except the package line.

  3. copy and paste the code from selenium IDE to that class, remove the package line.

  4. Right click in your code area and run as JUnit test.


My answer to converting selenium in to a j unit test, is quite simple.

1st you need to set up the code in the perspective workbench the use the editor by clicking on in the tool bar >go to shown in>then press on one of these editor > you see Java editor or window editor and so on, this will convert your code. then press go back to the class and highlight it, right click on the mouseand run it as Java application. you should be able to see your design/and source code. anymore questions