由于CLASSPATH的不确定性,在运行Java Selenium Headless测试用例时出现错误 [英] Getting error running Java Selenium Headless testcase due to CLASSPATH uncertainty

查看:73
本文介绍了由于CLASSPATH的不确定性,在运行Java Selenium Headless测试用例时出现错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的导出的Java无头硒测试用例代码,可以从IDE正常运行.

Below is my exported java headless selenium testcase code that runs fine from 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;
    }
  }

 }

我可以使用以下命令行快速编译此

I was able to compile this real quick using below command-line:

 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设置如下:

The CLASSPATH was set as below before running the java code:

$ 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测试用例时遇到以下错误.

However, I got the below error running the java testcase.

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及其依赖项:

After some research, in order to solve the above error, I decided to download htmlunit 2.9 jar and its dependencies from here:

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

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

I then updated my CLASSPATH by adding the below new jars ahead in the 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测试用例可以解决先前的错误和其他依赖关系,但是,我收到了一个新的错误,我不知道如何解决.看到以下错误:

Now running the java testcase resolve the previous error and other dependencies, however, I get a new error that I have no clue on how to resolve. See error below:

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,因为我希望它可以作为命令行运行.

I don't use or wish to use any IDE as I wish this to run as a command-line.

第二种方法:

一无所知;我尝试将类路径更改为以下内容:

After having no clue; I tried changing the classpath to below:

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:."

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

However, running the code gave me the below error.

[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

这里最奇怪的是在类路径中的/app/lib/commons-lang-2.6.jar中存在"org/apache/commons/lang/StringUtils".但是我得到"java.lang.NoClassDefFoundError" 请参见下面的上述说明的证明.

The most strange thing here was "org/apache/commons/lang/StringUtils" is present in /app/lib/commons-lang-2.6.jar which is in the classpath. Yet I get "java.lang.NoClassDefFoundError" See below s a proof of the above statement.

[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

我从不知道会变得如此糟糕.有人可以指导我如何使它工作吗?

I never knew it is going to be this bad. Can someone please guide how can I get this to work?

本文是我在原始文章上取得进展之后的文章:

This post is after I made progress on the original post here:Unable to run java junit selenium code for my test case

推荐答案

HtmlUnit 2.9确实过时了.当前版本为2.36( http://htmlunit.sourceforge.net/)

HtmlUnit 2.9 is really, really outdated. The current version is 2.36 (http://htmlunit.sourceforge.net/)

如果您想下载jar以及所有必需的依赖项,请从项目下载页面下载内容.您可以在>://sourceforge.net/projects/htmlunit/files/htmlunit/2.36.0/和所有依赖项的zip是htmlunit-2.36.0-bin.zip.

If you like to download the jar together with all required dependencies, please download the stuff from the project download page. You can find the download at https://sourceforge.net/projects/htmlunit/files/htmlunit/2.36.0/ and the zip with all the dependencies is htmlunit-2.36.0-bin.zip.

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

If you really need this old version download the similar named file from the corresponding subfolder of https://sourceforge.net/projects/htmlunit/files/htmlunit/

这篇关于由于CLASSPATH的不确定性,在运行Java Selenium Headless测试用例时出现错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆