【问题标题】:How to execute Selenium 2 tests in Jenkins如何在 Jenkins 中执行 Selenium 2 测试
【发布时间】:2013-04-10 21:19:27
【问题描述】:
  • 我希望能够将 Selenium 2 与 Jenkins 一起使用。

  • 我是新手,所以请原谅我的无知。

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

  • 我有一个基类如下:

    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;
        }
      }
    

我在 JenkinsSelenium 插件 上有以下配置:

..

一旦我尝试构建项目并在 Jenkins 中运行 Junit selenium 测试,它就会成功构建,但它自身的测试失败。 (从命令行使用 ant 运行时工作正常 - 并将 WebDriver 更改为: driver = new FirefoxDriver();) - 使用 selenium RC

这是 Jenkins 中的控制台输出:

编辑:我刚刚注意到您可以在 Jenkins 中构建之后归档 Junit .xml 输出文件。我得到一个类未找到异常?这很奇怪,因为就像我说的那样,在命令行中使用 ant 时它构建得很好。

错误如下:

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

提前感谢您提供的任何指导或帮助!

【问题讨论】:

    标签: java ant jenkins selenium-webdriver junit


    【解决方案1】:

    我想我犯了几个错误。为了解决 Class Not Found Exception 我在 ant 的 build.xml 中添加了以下内容 - (记住我是新 Ant)

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

    这让我的 java 类编译好了。

    然后我不得不将 selenium 独立服务器更新到最新版本 (selenium-server-standalone-2.xx.x.jar) 这位于:

    jenkins_home_directory\plugins\selenium\WEB-INF\lib

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

    另请注意:在 Red Hat 上运行 Selenium Server 时,我必须使用 Jenkins Xvfb 插件设置和安装 XVFB。

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

    【讨论】:

    • 我在集成 Selenium 2 + Jenkins 时也遇到了问题。我可以在詹金斯运行我的硒测试。但我无法通过詹金斯启动浏览器。它像无头浏览器一样运行。我可以知道天气有可能吗?如果是,请分享如何做到这一点。如果你有好的教程,请分享。
    • 是的,我可以在 MAC OSX 10.8.3 和 Windows 7 上使用 firefox 来完成。您必须使用我上面所述的自定义 Web 驱动程序节点配置,如果您使用它应该启动浏览器已经设置了一个配置了 firefox 的 selenium 网格。我找不到任何好的教程。祝你好运!
    【解决方案2】:

    如果您的意图是在没有 Selenium Grid 的情况下简单地运行 selenium 脚本。然后你不需要任何插件。您只需要远程 webdriver。

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

    【讨论】:

    • 我想使用 Selenium Grid,不,它不是 Maven 项目。
    • 将此行 driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), DesiredCapabilities.internetExplorer()); 更改为 driver = new RemoteWebDriver(new URL("http://localhost:5555/wd/hub"), DesiredCapabilities.internetExplorer()); 并尝试
    • 将端口更改为 5555 不起作用,我很抱歉这是一个旧屏幕截图,我将 IE 作为配置,我在原始问题中更新了该屏幕截图。
    • 然后将driver = new RemoteWebDriver(new URL("http://localhost:5555/wd/hub"), DesiredCapabilities.internetExplorer()); 更改为driver = new RemoteWebDriver(new URL("http://localhost:5555/wd/hub"), DesiredCapabilities.firefox()); 如果它不起作用,请分享错误消息。
    猜你喜欢
    • 2011-10-07
    • 2013-01-31
    • 1970-01-01
    • 2020-05-25
    • 2013-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多