无法为我的测试用例运行 java junit 硒代码



我使用 Katalon IDE 录制了测试用例步骤,并且能够使用专用浏览器会话成功播放录制内容。

我现在希望使用无头浏览器在 Linux 中播放测试用例。

因此,我将我的测试用例导出为 Java Junit 代码,如下所示:

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 

网址:https://qualysguard.mybank.com/fo/login.php?idm_key=saml2_70d8552f0974 应该将我重定向到Active Directory ADFS SSO登录页面,其中我有用户名输入和用户密码输入字段以及用于登录的提交按钮。

运行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"((.click((;

见下文:

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无法启动/生成新的Ghost 浏览器会话。

如您所见,您的主要问题似乎从一开始就是父母:

Title of the page is -> null

溶液

要在网站的用户名输入和用户密码输入字段中发送字符序列https://qualysguard.mybank.com/fo/login.php?idm_key=saml2_70d8552f0974您必须诱导WebDriverWait等待elementToBeClickable(),您可以使用以下解决方案之一:

  • cssSelector

    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客户端版本是3.141.0的2018-10-31T20:09:30,几乎早了一年多。确保已升级到当前级别版本 3.141.59。


引用

您可以在以下位置找到一些相关的讨论:

  • 无法通过Selenium 3.4.0启动HtmlUnitdriver
  • HtmlUnitDriver 在从 url 导航页面时不会加载 javascript

相关内容

  • 没有找到相关文章

最新更新