Running Selenium Tests with JUnit 4

by seetaram on March 23, 2011

Step 1: Create a new project Java SugarCRMJunitTests in Eclipse as shown in the below figure.

CreateNewJavaProject

Step 2: Right Click newly created Java Project (SugarCRMJunitTests) –> New –> Package –> Give name “SugarTests”

Step 3: Right Click SugarTests package –> New –> JUnit Test Case (Select checkboxes SetupBeforeClass, tearDownAfterClass)    –> Give the name as “SugarTestScripts”

createJunitTestCase

Step 4: Record “SugarCRM” Login test with Selenium IDE and export that to JUnit 4 format and copy the code into Eclipse JUnit Test Case.  Code should be as shown below:

package SugarTests;

import com.thoughtworks.selenium.*;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

public class SugarTestScripts extends SeleneseTestCase{
    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
        DefaultSelenium selenium = new DefaultSelenium(“localhost”, 4444, “*firefox”, “http://127.0.0.1/”);
        selenium.start();
    }

    @Test
    public void testSugarLogin() throws Exception {
        selenium.open(“http://127.0.0.1/sugarcrm/index.php?action=Login&module=Users”);
        selenium.type(“user_name”, “admin”);
        selenium.type(“user_password”, “admin123”);
        selenium.click(“login_button”);
        selenium.waitForPageToLoad(“30000”);
        verifyTrue(selenium.isTextPresent(“Home”));
        selenium.click(“link=Log Out”);
        selenium.waitForPageToLoad(“30000”);
    }
    @AfterClass
    public static void tearDownAfterClass() throws Exception {
        DefaultSelenium selenium = new DefaultSelenium(“localhost”, 4444, “*firefox”, “http://127.0.0.1/”);
        selenium.stop();
    }

}

Now try to run the JUnit Test Case:

  1. Run Selenium Server by double clicking the Selenium-server.bat file.  Selenium server should be running in the command prompt.
  2. Right click SugarTestScripts.java –> Run As –> JUnit Test
  3. This should open the browser and open the SugarCRM login screen and login.  Then logout.
  4. Browser should be closed

But execution does not generate any results since we have not either written any code to display the results or configured any other tool to generate report.

In the next post we will discuss about Ant and how it helps in generating nice HTML / XML reports

{ 9 comments… read them below or add one }

nitin July 19, 2011 at 9:19 am

Hi,

I have record a script through Selenium IDE :

package example;

import com.thoughtworks.selenium.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class Newtest extends SeleneseTestCase {
@Before
public void setUp() throws Exception {
selenium = new DefaultSelenium(“localhost”, 4444, “*chrome”, “http://www.google.com/”);
selenium.start();
}

@Test
public void newtest () throws Exception {
selenium.open(“/webhp?hl=en”);
selenium.type(“id=q”, “qwe”);
selenium.click(“id=btnG”);
selenium.waitForPageToLoad(“600000”);
}

@After
public void tearDown() throws Exception {
selenium.stop();
}
}

which is running successfully through Selenium IDe but when i am excuting java code for same in Junit through Selenium RC +eclipse its giving error, please let me know where i am doing wrong.Thanks.

Reply

seetaram July 20, 2011 at 6:56 am

Hi Nithin,

In the above code you need to make some changes:
1. Change the package to respective package inside eclipse
2. Replace the Newtest class with the Class you created

Reply

Talja July 25, 2011 at 2:26 pm

Hello, I am new in Selenium, and i have question, I`ve tried to run my testcase exported into JUnit from Selenium IDE 1.0.6 via IntelliJ IDEA, and i always have error CANNT FIND SYMBOL CLASS UTIL. Here is my code which i am trying to run.

package testcase1;

import com.thoughtworks.selenium.*;

import testcase1.java.util.regex.Pattern;

public class java {

public class testcase1 extends SeleneseTestCase {
public void setUp() throws Exception {
setUp(“http://book.theautomatedtester.co.uk/chapter1/”, “*chrome”);
}
public void testTestcase1() throws Exception {
selenium.open(“/”);
selenium.click(“link=Chapter1”);
selenium.waitForPageToLoad(“30000”);
selenium.select(“selecttype”, “label=Selenium Core”);
selenium.click(“verifybutton”);
}
}

}

Can you give me some help with this?

Reply

seetaram July 26, 2011 at 5:21 am

Please remove “import testcase1.java.util.regex.Pattern;” line and then execute. Reason is you are importing testcase1.java.util.regex.pattern means your testcase1 should have java classes (jdk library) in it that is absolutely wrong…

Reply

Mahesh December 15, 2011 at 6:21 am

Hi Seetaram,

Nice Post.
I have some query on this post.
I was return code,

package sugarTests;

import com.thoughtworks.selenium.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.util.regex.Pattern;
@SuppressWarnings({ “unused”, “deprecation” })
public class SugarTestScripts extends SeleneseTestCase {
@Before
public void setUp() throws Exception {
DefaultSelenium selenium = new DefaultSelenium(“localhost”, 4444, “*chrome”, “http://www.google.co.in/”);
selenium.start();
}

@Test
public void testUntitled() throws Exception {
selenium.open(“/”);
assertEquals(“Google”, selenium.getTitle());
selenium.type(“id=lst-ib”, “selenium”);
selenium.click(“name=btnG”);
verifyTrue(selenium.isTextPresent(“Selenium – Web Browser Automation”));
}

@After
public void tearDown() throws Exception {
selenium.stop();
}
}

I included Junit Jar, Selenium-java-2.15.0.jar, Selenium-java-2.15.0-srcs.jar
If i did not include this jar that time i was getting errors on
public class SugarTestScripts extends SeleneseTestCase
And Selenium object on every line.

Please do let me correct if i am wrong.

Reply

Mahesh December 22, 2011 at 8:05 am

Hi All,

Problem resolved i was getting com.google.charsets java class not found exception.
But after add jar appengine-tools-sdk-1.2.1 it get resolved.

Reply

Nipuni September 10, 2012 at 6:16 am

Thanks. This was very helpful 😉

Reply

Nidhi Vyas January 15, 2014 at 8:35 am

I am getting java.lang.NullPointerException on this line
selenium.open(“http://127.0.0.1/sugarcrm/index.php?action=Login&module=Users”);

Reply

seetaram January 29, 2014 at 11:44 am

Do you have that application installed on your system? If so then it does not throw error otherwise it does

Reply

Leave a Comment

Previous post:

Next post:

Do you want to get updates on the recent articles written? Please subscribe to RSS feed or Email