Getting error running Java Selenium Headless testcase due to CLASSPATH uncertainty

与世无争的帅哥 提交于 2019-12-11 06:46:52

问题


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

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

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)

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

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>

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)

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

A Second Approach:

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

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


回答1:


Here's a list of libs I use. Hopefully this helps:

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




回答2:


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

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.

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



来源:https://stackoverflow.com/questions/59059614/getting-error-running-java-selenium-headless-testcase-due-to-classpath-uncertain

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!