【问题标题】:Getting error running Java Selenium Headless testcase due to CLASSPATH uncertainty由于 CLASSPATH 不确定性,运行 Java Selenium Headless 测试用例时出错
【发布时间】:2020-03-22 09:13:59
【问题描述】:

下面是我导出的 java 无头硒测试用例代码,它在 IDE 中运行良好。

package pack;
import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;
import org.junit.*;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.ui.*;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;

public class QScan { 
private static HtmlUnitDriver driver;  
private static String baseUrl;  
private boolean acceptNextAlert = true;  
private static StringBuffer verificationErrors = new StringBuffer();

public static void setUp() throws Exception {
        driver = new HtmlUnitDriver();
   driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
  }

    public static void testQScan() throws Exception {
   driver.get("https://qualysguard.mybank.com/fo/login.php?idm_key=saml2_70d8552f0974");
WebElement element = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("#userNameInput")));

System.out.println("Title of the page is -> " + driver.getTitle());
System.out.println("Entering userName!");
        driver.findElement(By.id("userNameInput")).click();
        System.out.println("Clear userName!");
        driver.findElement(By.id("userNameInput")).clear();
        System.out.println("Title of the page is 2 -> " + driver.getTitle()); 
}

 public static void main(String[] args) throws Exception
    {
        QScan.setUp();
        QScan.testQScan();
        QScan.tearDown();
    }

 private static boolean isElementPresent(By by) {
    try {
      driver.findElement(by);
      return true;
    } catch (NoSuchElementException e) {
      return false;
    }
  }

  private boolean isAlertPresent() {
    try {
driver.switchTo().alert();
  return true;
    } catch (NoAlertPresentException e) {
return false;
    }
  }

 }

我能够使用下面的命令行快速编译:

 javac -d . -cp /app/lib/selenium-server-standalone-3.141.59.jar:/app/lib/selenium-htmlunit-driver-2.52.0.jar:/app/lib/junit.jar:/app/lib/hamcrest-core-1.2.jar QScan.java

在运行java代码之前,CLASSPATH设置如下:

$ echo $CLASSPATH
/app/lib/guava-25.0-jre.jar:/app/lib/httpclient-4.5.9.jar:/app/lib/selenium-java-3.141.0.jar:/app/lib/selenium-api-3.141.0.jar:/app/lib/selenium-firefox-driver-3.141.0.jar:/app/lib/selenium-support-3.141.0.jar:/app/lib/selenium-htmlunit-driver-2.52.0.jar:/app/lib/selenium-server-standalone-2.53.0.jar:/app/lib/htmlunit-2.36.0.jar:/app/lib/sac-1.3.jar.zip:/app/lib/htmlunit-core-js-2.36.0.jar:/app/lib/sac-1.3/sac.jar:/app/lib/selenium-remote-driver-4.0.0-alpha-3.jar:/app/lib/selenium-remote-driver-2.44.0.jar:/app/lib/commons-logging-1.2.jar:/app/lib/httpclient-4.5.9.jar:/app/lib/httpcore-4.4.11.jar:/app/lib/commons-codec-1.11.jar:/app/lib/htmlunit-cssparser-1.5.0.jar:/app/lib/commons-lang3-3.9.jar:/app/lib/dec-0.1.2.jar:/app/lib/httpmime-4.5.9.jar:/app/lib/xalan-2.7.2.jar:/app/lib/websocket-api-9.4.20.v20190813.jar:/app/lib/websocket-client-9.4.20.v20190813.jar:/app/lib/websocket-common-9.4.20.v20190813.jar:/app/lib/jetty-util-9.4.20.v20190813.jar:/app/lib/commons-io-2.6.jar:/app/lib/xerces-2.9.0.jar:/app/lib/neko-htmlunit-2.36.0.jar:/app/lib/com.google.collections.jar:/app/lib/junit.jar:/app/lib/hamcrest-core-1.2.jar:.

但是,运行 java 测试用例时出现以下错误。

java pack.QScan
Title of the page is -> null
Exception in thread "main" java.lang.IllegalStateException: Unable to locate element by xpath for com.gargoylesoftware.htmlunit.UnexpectedPage@42039326
        at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElementByXPath(HtmlUnitDriver.java:1152)
        at org.openqa.selenium.By$ByXPath.findElement(By.java:353)
        at org.openqa.selenium.htmlunit.HtmlUnitDriver$5.call(HtmlUnitDriver.java:1725)
        at org.openqa.selenium.htmlunit.HtmlUnitDriver$5.call(HtmlUnitDriver.java:1721)
        at org.openqa.selenium.htmlunit.HtmlUnitDriver.implicitlyWaitFor(HtmlUnitDriver.java:1367)
        at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElement(HtmlUnitDriver.java:1721)
        at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElement(HtmlUnitDriver.java:606)
        at org.openqa.selenium.support.ui.ExpectedConditions$7.apply(ExpectedConditions.java:205)
        at org.openqa.selenium.support.ui.ExpectedConditions$7.apply(ExpectedConditions.java:201)
        at org.openqa.selenium.support.ui.ExpectedConditions$22.apply(ExpectedConditions.java:641)
        at org.openqa.selenium.support.ui.ExpectedConditions$22.apply(ExpectedConditions.java:638)
        at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:249)
        at pack.QScan.testQScan(QScan.java:46)
        at pack.QScan.main(QScan.java:183)

经过一番研究,为了解决上述错误,我决定从这里下载htmlunit 2.9 jar及其依赖:

https://jar-download.com/artifacts/net.sourceforge.htmlunit/htmlunit/2.9/source-code

然后我通过在 CLASSPATH 中添加以下新 jar 来更新我的 CLASSPATH:

/app/lib/gaygoyle/cssparser-0.9.5.jar:/app/lib/gaygoyle/commons-collections-3.2.1.jar:/app/lib/gaygoyle/commons-lang-2.6.jar:/app/lib/gaygoyle/htmlunit-2.9.jar:/app/lib/selenium-htmlunit-driver-2.52.0.jar:<previous classpath as mentioned before>

现在运行 java 测试用例解决了以前的错误和其他依赖项,但是,我收到一个新错误,我不知道如何解决。请参阅下面的错误:

java pack.QScan
Exception in thread "main" java.lang.NoSuchMethodError: com.gargoylesoftware.htmlunit.WebClient.getOptions()Lcom/gargoylesoftware/htmlunit/WebClientOptions;
        at org.openqa.selenium.htmlunit.HtmlUnitDriver.createWebClient(HtmlUnitDriver.java:320)
        at org.openqa.selenium.htmlunit.HtmlUnitDriver.<init>(HtmlUnitDriver.java:191)
        at org.openqa.selenium.htmlunit.HtmlUnitDriver.<init>(HtmlUnitDriver.java:181)
        at org.openqa.selenium.htmlunit.HtmlUnitDriver.<init>(HtmlUnitDriver.java:171)
        at org.openqa.selenium.htmlunit.HtmlUnitDriver.<init>(HtmlUnitDriver.java:161)
        at pack.QScan.setUp(QScan.java:32)
        at pack.QScan.main(QScan.java:182)

我不使用或不希望使用任何 IDE,因为我希望它作为命令行运行。

第二种方法:

在毫无头绪之后;我尝试将类路径更改为以下:

export CLASSPATH="/app/lib/commons-lang-2.6.jar:/app/lib/commons-logging-1.2.jar:/app/lib/commons-codec-1.11.jar:/app/lib/httpclient-4.5.9.jar:/app/lib/commons-io-2.6.jar:/app/lib/selenium-remote-driver-2.44.0.jar:/app/lib/gaygoyle/htmlunit-core-js-2.9.jar:/app/lib/gaygoyle/htmlunit-2.9.jar:/app/lib/selenium-htmlunit-driver-2.52.0.jar:/app/lib/selenium-server-standalone-3.141.59.jar:/app/lib/gaygoyle/sac-1.3.jar:."

但是,运行代码给了我以下错误。

[user1@host1 vapt]$ java pack.QScan
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/lang/StringUtils
        at com.gargoylesoftware.htmlunit.util.URLCreator$URLCreatorStandard.toUrlUnsafeClassic(URLCreator.java:66)
        at com.gargoylesoftware.htmlunit.util.UrlUtils.toUrlUnsafe(UrlUtils.java:193)
        at com.gargoylesoftware.htmlunit.util.UrlUtils.toUrlSafe(UrlUtils.java:171)
        at com.gargoylesoftware.htmlunit.WebClient.<clinit>(WebClient.java:162)
        at org.openqa.selenium.htmlunit.HtmlUnitDriver.newWebClient(HtmlUnitDriver.java:353)
        at org.openqa.selenium.htmlunit.HtmlUnitDriver.createWebClient(HtmlUnitDriver.java:319)
        at org.openqa.selenium.htmlunit.HtmlUnitDriver.<init>(HtmlUnitDriver.java:191)
        at org.openqa.selenium.htmlunit.HtmlUnitDriver.<init>(HtmlUnitDriver.java:181)
        at org.openqa.selenium.htmlunit.HtmlUnitDriver.<init>(HtmlUnitDriver.java:171)
        at org.openqa.selenium.htmlunit.HtmlUnitDriver.<init>(HtmlUnitDriver.java:161)
        at pack.QScan.setUp(QScan.java:32)
        at pack.QScan.main(QScan.java:182)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.lang.StringUtils
        at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
        ... 12 more

这里最奇怪的是“org/apache/commons/lang/StringUtils”出现在类路径中的 /app/lib/commons-lang-2.6.jar 中。然而我得到“java.lang.NoClassDefFoundError” 请参阅下面的上述陈述的证明。

[user1@host1 lib]$ find . -name commons-lang-2.6.jar -printf "%f%h" -exec jar tvf {} \; | grep -i 'org/apache/commons/lang/StringUtils'
 37671 Thu Jan 13 23:06:38 IST 2011 org/apache/commons/lang/StringUtils.class
 37671 Thu Jan 13 23:06:38 IST 2011 org/apache/commons/lang/StringUtils.class

我从来不知道事情会变得如此糟糕。有人可以指导我如何让它工作吗?

这篇文章是我在原帖上取得进展后发布的:Unable to run java junit selenium code for my test case

【问题讨论】:

  • ...在原帖上取得了进展...但还没有分享巧克力蛋糕:)

标签: java selenium automated-tests runtime-error headless-browser


【解决方案1】:

这是我使用的库列表。希望这会有所帮助:

byte-buddy-1.8.3.jar

commons-codec-1.10.jar
commons-exec-1.3.jar
commons-io-2.5.jar
commons-logging-1.2.jar

gson-2.8.4.jar
guava-25.0-jre.jar
htmlunit-driver-2.33.3-jar-with-dependencies.jar

httpclient-4.5.5.jar

httpcore-4.4.9.jar
okhttp-3.10.0.jar
okio-1.14.1.jar

selenium-server-standalone-3.141.59.jar

【讨论】:

  • 我将按照您明天给出的顺序尝试这些并在此论坛上更新。感谢您的意见@pcalkins
  • 我也有“serializer-2.7.2.jar”,但我认为这里不需要。
  • 我找不到 htmlunit-driver-2.33.3-jar-with-dependencies.jar,而是找到了 htmlunit-driver-2.33.3.jar。我还必须添加 junit.jar 和 hamcrest-core-1.3.jar 才能编译我的代码。编译后我运行代码并收到此错误:java.lang.NoClassDefFoundError: com/gargoylesoftware/htmlunit/RefreshHandler at pack.QScan.setUp
【解决方案2】:

HtmlUnit 2.9 已经过时了。当前版本是 2.36 (http://htmlunit.sourceforge.net/)

如果您想下载 jar 以及所有必需的依赖项,请从项目下载页面下载这些东西。您可以在https://sourceforge.net/projects/htmlunit/files/htmlunit/2.36.0/ 找到下载,包含所有依赖项的 zip 是 htmlunit-2.36.0-bin.zip。

如果你真的需要这个旧版本,请从https://sourceforge.net/projects/htmlunit/files/htmlunit/的相应子文件夹中下载类似命名的文件

【讨论】:

  • 我明天会试试这个并在这里更新,因为我仍然面临这个问题。
  • 确保您也有匹配的 selenium 和 HtmlUnitDriver 版本。
猜你喜欢
  • 1970-01-01
  • 2020-06-26
  • 2016-10-11
  • 1970-01-01
  • 1970-01-01
  • 2013-06-26
  • 2020-11-06
  • 2016-08-20
  • 1970-01-01
相关资源
最近更新 更多