如何在Jenkins中执行Selenium 2测试

时间:2022-10-30 19:24:45
  • I would like to be able to use Selenium 2 with Jenkins.

    我想用Selenium 2和Jenkins一起使用。

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

    我对双方都不熟,所以请原谅我的无知。

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

    我注意到下面的jenkins插件,并安装了它。

  • 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 :

我在Jenkins的Selenium插件上有如下配置:

如何在Jenkins中执行Selenium 2测试

..

. .

如何在Jenkins中执行Selenium 2测试

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

一旦我尝试构建项目并在Jenkins中运行Junit selenium测试,它就成功构建了,但是它自己的测试失败了。(当从命令行运行ant时,运行良好,并将WebDriver改为:driver = new FirefoxDriver();) -使用selenium RC。

This is the console output in Jenkins: 如何在Jenkins中执行Selenium 2测试

这是Jenkins中的控制台输出:

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.

编辑:我刚刚注意到您可以在Jenkins中的构建之后对Junit .xml输出文件进行归档。我得到了一个类没有发现例外?这很奇怪,因为就像我说的,它在使用命令行中的ant时构建得很好。

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!

预先感谢您的指导或帮助!

2 个解决方案

#1


5  

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)

我想我犯了几个错误。为了解决类Not Found Exception,我在ant的构建中添加了以下内容。xml -(记住我是新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.

这使我的java类编译。

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

然后我必须将selenium独立服务器更新到最新版本(selenium-server- standone -2.xx.x.jar),它位于:

jenkins_home_directory\plugins\selenium\WEB-INF\lib

硒jenkins_home_directory \ plugins \ \ 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.)

最后,我尝试在selenium插件中使用错误的配置(我尝试使用自定义RC节点配置,我需要的是自定义web driver节点配置)。

如何在Jenkins中执行Selenium 2测试

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

还要注意:在Red Hat上运行Selenium服务器时,我必须使用Jenkins XVFB插件安装和安装XVFB。

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

我希望这能对以后的其他人有所帮助!好运!

#2


1  

Well if your intention is to simply run the selenium script without Selenium Grid. Then you do not need any plugin. You would only need remote webdriver.

如果你只是想在没有selenium网格的情况下运行selenium脚本。那么你就不需要任何插件了。您只需要远程webdriver。

To launch Selenium 2 from Jenkins the best way would be is to wrap the test process in the pom.xml (if you are using Maven) and then simply create a new job in Maven using "Build a maven2/3 project" in Jenkins.

要从Jenkins启动Selenium 2,最好的方法是在pom中封装测试过程。xml(如果您正在使用Maven),然后使用Jenkins中的“构建一个maven2/3项目”在Maven中创建一个新的作业。

#1


5  

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)

我想我犯了几个错误。为了解决类Not Found Exception,我在ant的构建中添加了以下内容。xml -(记住我是新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.

这使我的java类编译。

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

然后我必须将selenium独立服务器更新到最新版本(selenium-server- standone -2.xx.x.jar),它位于:

jenkins_home_directory\plugins\selenium\WEB-INF\lib

硒jenkins_home_directory \ plugins \ \ 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.)

最后,我尝试在selenium插件中使用错误的配置(我尝试使用自定义RC节点配置,我需要的是自定义web driver节点配置)。

如何在Jenkins中执行Selenium 2测试

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

还要注意:在Red Hat上运行Selenium服务器时,我必须使用Jenkins XVFB插件安装和安装XVFB。

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

我希望这能对以后的其他人有所帮助!好运!

#2


1  

Well if your intention is to simply run the selenium script without Selenium Grid. Then you do not need any plugin. You would only need remote webdriver.

如果你只是想在没有selenium网格的情况下运行selenium脚本。那么你就不需要任何插件了。您只需要远程webdriver。

To launch Selenium 2 from Jenkins the best way would be is to wrap the test process in the pom.xml (if you are using Maven) and then simply create a new job in Maven using "Build a maven2/3 project" in Jenkins.

要从Jenkins启动Selenium 2,最好的方法是在pom中封装测试过程。xml(如果您正在使用Maven),然后使用Jenkins中的“构建一个maven2/3项目”在Maven中创建一个新的作业。