selenium webdriver是否支持Struts 2

时间:2013-04-26 06:25:39

标签: selenium struts2 webdriver

我想使用Selenium WebDriver API框架使用Struts2。所以它是否会得到支持。

如果没有,那么我该怎么做。

这意味着,假设我的textfield上有一个jsp,与操作类相关联,我希望在www.google.com上搜索我的textfieldsendKeys元素“q”。

我想做的是

index.jsp:     

<s:form action="test">
<s:textfield name="search" label="Enter Search "></s:textfield>
</s:form>

</body>

struts.xml中:

    <action name="test" class="com.actions.TestAction">
          <result name="SUCCESS">/success.jsp</result>
    </action>

ActionClass:

package com.actions;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;

import com.opensymphony.xwork2.ActionSupport;

public class TestAction extends ActionSupport
{

    private String search;

    public String getSearch() {
        return search;
    }

    public void setSearch(String search) {
        this.search = search;
    }


    private static final long serialVersionUID = -1241657564582564726L;

    @Override
    public String execute() throws Exception {

        WebDriver driver = new HtmlUnitDriver();

        driver.get("http://www.google.com");

        WebElement element = driver.findElement(By.name("q"));


        element.sendKeys(getSearch());

        element.submit();


        System.out.println("Page title is: " + driver.getTitle());


        System.out.println(driver.getPageSource());        

        driver.quit();
        return "SUCCESS";
    }


}

错误:

SEVERE: Exception starting filter struts2
java.lang.NoClassDefFoundError: org/openqa/selenium/WebDriver
    at java.lang.Class.getDeclaredConstructors0(Native Method)
    at java.lang.Class.privateGetDeclaredConstructors(Unknown Source)
    at java.lang.Class.getConstructor0(Unknown Source)
    at java.lang.Class.getConstructor(Unknown Source)
    at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.verifyAction(XmlConfigurationProvider.java:428)
    at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.addAction(XmlConfigurationProvider.java:378)
    at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.addPackage(XmlConfigurationProvider.java:495)
    at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.loadPackages(XmlConfigurationProvider.java:286)
    at org.apache.struts2.config.StrutsXmlConfigurationProvider.loadPackages(StrutsXmlConfigurationProvider.java:112)
    at com.opensymphony.xwork2.config.impl.DefaultConfiguration.reloadContainer(DefaultConfiguration.java:234)
    at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:66)
    at org.apache.struts2.dispatcher.Dispatcher.init_PreloadConfiguration(Dispatcher.java:390)
    at org.apache.struts2.dispatcher.Dispatcher.init(Dispatcher.java:437)
    at org.apache.struts2.dispatcher.FilterDispatcher.init(FilterDispatcher.java:193)
    at org.apache.catalina.core.ApplicationFilterConfig.initFilter(ApplicationFilterConfig.java:278)
    at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:259)
    at org.apache.catalina.core.ApplicationFilterConfig.setFilterDef(ApplicationFilterConfig.java:383)
    at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:104)
    at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4650)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5306)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
    at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.ClassNotFoundException: org.openqa.selenium.WebDriver
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1714)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559)

急切地等待你的回复。 谢谢 !

1 个答案:

答案 0 :(得分:1)

直到有人得到更好的答案......我从来没有直接使用struts,但我认为我对它的作用有基本的了解。

Selenium在前端运行,就像用户一样。我相信struts是一种仅限后端的技术,这意味着浏览器甚至不知道它接收了由struts组装的东西。 Struts只是帮助服务器组装html,css和javascript。

Selenium运行良好的html,javascript和css,因为那是你的服务器发送的内容,它应该都是好的,它应该&#34;支持它&#34;很好!