java 如何在 Jenkins 中执行 Selenium 2 测试

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16071443/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 21:49:03  来源:igfitidea点击:

How to execute Selenium 2 tests in Jenkins

javaantjenkinsselenium-webdriverjunit

提问by Curt

  • 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;
        }
      }
    
  • 我希望能够在 Jenkins 中使用 Selenium 2。

  • 我对两者都是新手,所以请原谅我的无知。

  • 我注意到 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;
        }
      }
    

I have the following configuration on the Selenium Pluginfor 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 WebDriverto : driver = new FirefoxDriver();) - Using selenium RC

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

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

这是 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 antfrom 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.run(URLClassLoader.java:366) at 
java.net.URLClassLoader.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!

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

采纳答案by Curt

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

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

<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-standalone-2.xx.x.jar) 这位于:

jenkins_home_directory\plugins\selenium\WEB-INF\lib

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

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

enter image description here

在此处输入图片说明

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

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

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

我希望这可以在未来对其他人有所帮助!祝你好运!

回答by Amey

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 Grid 的情况下简单地运行 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 中简单地创建一个新作业。