无法为我的测试用例运行java junit selenium代码
我使用KatalonIDE记录了测试用例步骤,并且我能够使用私有浏览器会话成功地播放记录 现在,我希望使用headless浏览器在Linux中播放测试用例 因此,我将测试用例导出为Java Junit代码,如下所示:无法为我的测试用例运行java junit selenium代码,java,selenium,selenium-webdriver,headless,htmlunit-driver,Java,Selenium,Selenium Webdriver,Headless,Htmlunit Driver,我使用KatalonIDE记录了测试用例步骤,并且我能够使用私有浏览器会话成功地播放记录 现在,我希望使用headless浏览器在Linux中播放测试用例 因此,我将测试用例导出为Java Junit代码,如下所示: package pack; import java.util.regex.Pattern; import java.util.concurrent.TimeUnit; import org.junit.*; import static org.junit.Assert.*; imp
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");
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/Katalon/lib/lib/junit.jar:/app/Katalon/lib/lib/hamcrest-core-1.2.jar:/app/Katalon/lib/lib/selenium-java-3.141.0.jar:/app/Katalon/lib/lib/selenium-api-3.141.0.jar:/app/Katalon/lib/lib/selenium-firefox-driver-3.141.0.jar:/app/Katalon/lib/lib/selenium-support-3.141.0.jar:/app/Katalon/lib/lib/selenium-htmlunit-driver-2.52.0.jar:/app/Katalon/lib/lib/selenium-server-standalone-2.53.0.jar QScan.java
url:会将我重定向到Active Directory ADFS SSO登录页面,在那里我有userNameInput和userPasswordInput字段以及用于登录的submit按钮
运行java代码测试用例会出现以下错误:
[user1@myhost vapt]$ java pack.QScan
Title of the page is -> null
Entering userName!
Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element by id for com.gargoylesoftware.htmlunit.UnexpectedPage@30af5b6b
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '3.141.0', revision: '2ecb7d9a', time: '2018-10-31T20:09:30'
System info: host: 'myhost', ip: '10.9.56.26', os.name: 'Linux', os.arch: 'amd64', os.version: '3.10.0-1062.1.2.el7.x86_64', java.version: '1.8.0_221'
Driver info: driver.version: HtmlUnitDriver
at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElementById(HtmlUnitDriver.java:1011)
at org.openqa.selenium.By$ById.findElement(By.java:188)
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 pack.QScan.testQScan(QScan.java:82)
at pack.QScan.main(QScan.java:182)
网上后研究;我认为应该在单击之前等待元素加载,因此,我在->driver.findElement(By.id(“userNameInput”))之前添加了以下代码
见下文:
driver.get("https://qualysguard.mybank.com/fo/login.php?idm_key=saml2_70d8552f0974");
for (int second = 0;; second++) {
if (second >= 3) fail("timeout");
try
{
System.out.println("Title of the page is -> " + driver.getTitle());
System.out.println("second is:" + second);
if (isElementPresent(By.id("submitButton"))) break;
}
catch (Exception e)
{
System.out.println("second2 is:" + second);
System.out.println(e);
e.printStackTrace();
}
Thread.sleep(1000);
System.out.println("AFTER SLEEP");
}
System.out.println("Entering userName!");
driver.findElement(By.id("userNameInput")).click();
System.out.println("Clear userName!");
然而,当我编译并运行它时,它只是在不应该超时的时候超时。请参见下面的输出
[user1@myhost vapt]$ java pack.QScan
Title of the page is -> null
second is:0
AFTER SLEEP
Title of the page is -> null
second is:1
AFTER SLEEP
Title of the page is -> null
second is:2
AFTER SLEEP
Exception in thread "main" java.lang.AssertionError: timeout
at org.junit.Assert.fail(Assert.java:88)
at pack.QScan.testQScan(QScan.java:61)
at pack.QScan.main(QScan.java:182)
下面的命令确认可以从服务器访问URL
[user1@myhost vapt]$ firefox -v
Failed to open connection to "session" message bus: Unable to autolaunch a dbus-daemon without a $DISPLAY for X11
Running without a11y support!
Mozilla Firefox 60.9.0
[user1@myhost vapt]$ curl -Is https://qualysguard.mybank.com/fo/login.php?idm_key=saml2_70d8552f0974
HTTP/1.1 200 Connection established
此外,在Windows上的chrome浏览器上使用Katalon IDE,该测试用例运行良好,这一事实确认了元素ID是正确的
您能建议我如何获得这项工作吗?此错误消息
Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element by id for com.gargoylesoftware.htmlunit.UnexpectedPage@30af5b6b
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '3.141.0', revision: '2ecb7d9a', time: '2018-10-31T20:09:30'
System info: host: 'myhost', ip: '10.9.56.26', os.name: 'Linux', os.arch: 'amd64', os.version: '3.10.0-1062.1.2.el7.x86_64', java.version: '1.8.0_221'
Driver info: driver.version: HtmlUnitDriver
…意味着HtmlUnitDriver无法启动/生成新的重影浏览器会话
正如您所看到的,您的主要问题似乎从一开始就不一致:
Title of the page is -> null
解决方案 在网站的userNameInput和userPasswordInput字段中发送带有的字符序列
https://qualysguard.mybank.com/fo/login.php?idm_key=saml2_70d8552f0974
您必须为元素导入WebDriverWait以使其可伸缩()
,并且您可以使用以下任一解决方案:
:css选择器
WebElement element = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("#userNameInput"))); element.click(); element.clear();
:xpath
WebElement element = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id='userNameInput']"))); element.click(); element.clear();
HtmlUnitDriver()
未从以下位置解析:
com.gargoylesoftware.htmlunit.BrowserVersion;
额外考虑 您的Selenium客户端版本是2018-10-31T20:09:30的3.141.0,比它早了一年多。确保硒升级到当前水平
工具书类 您可以在以下内容中找到一些相关讨论:
com.gargoylesoftware.htmlunit.BrowserVersion;
签出相关讨论。将HtmlUnitDriver()解析为所需的api后,我仍然面临如何使WebDriverWait正常工作的问题。此处发布了进度和面临的新问题: