vrghost
vrghost

Reputation: 1224

junit and running a selenium test from command line

Having a bit of a nightmare, used Scirocco to record a macro just to test Selenium, and can now not for the life of me figure out how to run it.

To manage to compile the code, I use -classpath .:/usr/share/junit4/lib/junit.jar:/usr/local/share/selenium-2.45.0/selenium-java-2.45.0.jar

This seems to load all that is required and compiles. The test script it self:

import java.util.concurrent.TimeUnit;
import junit.framework.TestCase;
import org.junit.*;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;

public class Test1 extends TestCase {
  private WebDriver driver;
    private String baseUrl;

        @Before
          public void setUp() throws Exception {
              // Download chromedriver (http://code.google.com/p/chromedriver/downloads/list)
              System.setProperty("webdriver.chrome.driver", "/usr/sbin/chromedriver");
              driver = new ChromeDriver();
              baseUrl = "https://www.google.co.uk/?gfe_rd=cr&ei=E4syVcSOK-jN7AbK0YH4Dg&gws_rd=ssl#q=Bengt+Bjorkberg";
              driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
          }

          @Test
          public void test() throws Exception {
               driver.get(baseUrl + "/");
               driver.findElement(By.name("btnG")).click();
               driver.findElement(By.linkText("Ben Bjorkberg | LinkedIn")).click();
               driver.findElement(By.linkText("View Ben's Full Profile")).click();
               driver.findElement(By.linkText("View Ben's Full Profile")).click();
          }

          @After
          public void tearDown() throws Exception {
                driver.quit();
          }
}

Compiles nicely, and in theory should work.

Then I create the following test runner:

package de.vogella.junit.first;

import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;


public class MyTestRunner {
  public static void main(String[] args) {
      Result result = JUnitCore.runClasses(Test1.class);
        for (Failure failure : result.getFailures()) {
                System.out.println(failure.toString());
        }
     }

But if I run

javac -classpath .:/usr/share/junit4/lib/junit.jar:/usr/local/share/selenium-2.45.0/selenium-java-2.45.0.jar MyTestRunner.java 

At this point I get

MyTestRunner.java:10: error: cannot find symbol
      Result result = JUnitCore.runClasses(Test1.class);
                                           ^
  symbol:   class Test1
  location: class MyTestRunner
1 error

Any tips?

Upvotes: 0

Views: 1891

Answers (1)

NamshubWriter
NamshubWriter

Reputation: 24326

I recommend doing your development in an IDE like Eclipse or IntelliJ, using a standard directory layout. The IDE will catch many kinds of problems.

My guess is that Test1 doesn't have a package statement, so it's in the default package. Since MyTestRunner is not in the default package, you get a compile error. That's the kind of problem an IDE would tell you right away.

Once you are ready to build on the command line, I would use a build tool like Maven or Gradle.

A few minor problems with your code:

  • MyTestRunner looks a lot like JUnitCore, except it hard-codes what test to run. Again, I recommend using a build tool. You can easily configure Maven or Gradle to run all of the tests under src/test/java, and they will print out nice stack traces if tests fail. They both work with Continuous Integration tools (JUnit itself uses http://cloudbees.com). If you really want your own runner, you should probably delegate to JUnitCore.main()
  • JUnit4-style tests (that is, tests that use annotations like @Test and @Before) should not extend junit.framework.TestCase, directly or indirectly. In fact, your JUnit4-style tests should not reference any classes in the junit.framework package.

Upvotes: 2

Related Questions