gpt4 book ai didi

java - 由于 CLASSPATH 不确定性,运行 Java Selenium Headless 测试用例时出错

转载 作者:太空宇宙 更新时间:2023-11-04 09:15:32 26 4
gpt4 key购买 nike

下面是我导出的 java headless selenium 测试用例代码,可以在 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,因为我希望它作为命令行运行。

第二种方法:

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

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

最佳答案

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

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

关于java - 由于 CLASSPATH 不确定性,运行 Java Selenium Headless 测试用例时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59059614/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com