Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to execute Selenium 2 tests in Jenkins

  • I would like to be able to use Selenium 2 with Jenkins.

  • I am new to both so please excuse any of my ignorance.

  • I noticed the following plugin for jenkins HERE, and installed it.

  • I have a base class as follows:

    public class BaseTestClass {
    
    protected Properties myprops;
    protected String baseurl;
    protected WebDriver driver;
    protected boolean acceptNextAlert = true;
    protected StringBuffer verificationErrors = new StringBuffer();
    
    public BaseTestClass()
    {
        try
        {
            myprops = TestUtil.readProps("src/MyProps.properties");
            baseurl = myprops.getProperty("baseurl");
    
            driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), DesiredCapabilities.fireFox());
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }       
    }
    
    @Before
    public void setUp() throws Exception {
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    }
    
    @After
    public void tearDown() throws Exception {
        driver.quit();
        String verificationErrorString = verificationErrors.toString();
        if (!"".equals(verificationErrorString)) {
            fail(verificationErrorString);
        }
    }
    
    protected boolean isElementPresent(By by) {
        try {
          driver.findElement(by);
          return true;
        } catch (NoSuchElementException e) {
          return false;
        }
      }
    
      protected String closeAlertAndGetItsText() {
        try {
          Alert alert = driver.switchTo().alert();
          if (acceptNextAlert) {
            alert.accept();
          } else {
            alert.dismiss();
          }
          return alert.getText();
        } finally {
          acceptNextAlert = true;
        }
      }
    

I have the following configuration on the Selenium Plugin for Jenkins :

enter image description here

..

enter image description here

Once I try to build the project and run a Junit selenium test in Jenkins, it builds successfully, but the test it self fails. (works fine when running with ant from the command line - and changing the WebDriver to : driver = new FirefoxDriver();) - Using selenium RC

This is the console output in Jenkins: enter image description here

EDIT: I just noticed you can Archive the Junit .xml output file after the build in Jenkins. I am getting a class not found exception? This is weird because like i said, it builds just fine when using ant from the command line.

The error is as follows:

<error message="com.loggedin.CCBreadCrumb" type="java.lang.ClassNotFoundException">
java.lang.ClassNotFoundException: com.loggedin.CCBreadCrumb at
java.net.URLClassLoader$1.run(URLClassLoader.java:366) at 
java.net.URLClassLoader$1.run(URLClassLoader.java:355) at
java.security.AccessController.doPrivileged(Native Method) at
java.net.URLClassLoader.findClass(URLClassLoader.java:354) at
java.lang.ClassLoader.loadClass(ClassLoader.java:423) at
sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308) at
java.lang.ClassLoader.loadClass(ClassLoader.java:356) at java.lang.Class.forName0(Native
Method) at java.lang.Class.forName(Class.java:186)
</error>

Thanks in advance for any direction or help you may have!

like image 848
Curt Avatar asked Apr 17 '13 22:04

Curt


1 Answers

I think i was making several mistakes. To resolve the Class Not Found Exception I added the following to ant's build.xml - (remember I am new Ant)

<target name="compile" depends="init" description="compile the source " >       
    <javac srcdir="src/" destdir="bin" classpathref="SeleniumCC.classpath"/>
</target>       

This got my java classes compiling.

Then I had to update the selenium standalone server to the latest version (selenium-server-standalone-2.xx.x.jar) This is located in:

jenkins_home_directory\plugins\selenium\WEB-INF\lib

Last I was trying to use the wrong configuration in the selenium plug-in (I was trying to use a Custom RC node configuration, what I needed was a Custom web driver node configuration.)

enter image description here

ALSO NOTE : When running Selenium Server on Red Hat I had to setup and install XVFB with Jenkins Xvfb plugin.

I hope this can be of help to others in the future! Good luck!

like image 51
Curt Avatar answered Oct 25 '22 05:10

Curt