无法为我的测试用例运行java junit硒代码 [英] Unable to run java junit selenium code for my test case

查看:107
本文介绍了无法为我的测试用例运行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登录页面,在该页面中,我具有userNameInput和userPasswordInput字段以及提交登录按钮.

运行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 Browser 会话.

从一开始,您的主要问题似乎就已经存在:

Title of the page is -> null


解决方案

要发送带有网站https://qualysguard.mybank.com/fo/login.php?idm_key=saml2_70d8552f0974 userNameInput userPasswordInput 字段中的字符序列,您必须诱使 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 Client 版本是 2018-10-31T20:09:30 3.141.0 ,该版本的使用时间已超过一年.确保已升级到当前级别版本3.141.59 ./p>


参考文献

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

I recorded the testcase steps using Katalon IDE and I was able to play the recording successfully using private browser session.

I now wish to play the testcase in Linux using headless browser.

Hence, I exported my testcase as Java Junit code as below:

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;
    }
  }

 }

I was able to compile the code using the below command.

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 

The url: https://qualysguard.mybank.com/fo/login.php?idm_key=saml2_70d8552f0974 is suppose to redirect me to the Active Directory ADFS SSO login page where i have userNameInput and userPasswordInput fields and submit button for signing in.

Running the java code test case gives me the below error:

[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)

Post researching on internet; I thought that one should wait for the element to load before the click hence, I added the below code before -> driver.findElement(By.id("userNameInput")).click();

See below:

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!");

However, when I compile and run it simply timesout when it should not. See output below.

[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)

The below commands affirm that the URL is accessible from the server.

[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

Also, the fact that this test case runs fine using Katalon IDE on chrome browser on Windows affirms that the element IDs are correct.

Can you please suggest how can I get this work ?

解决方案

This error message...

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

...implies that the HtmlUnitDriver was unable to initiate/spawn a new Ghost Browser session.

Your main issue is seems to be aparent right from the begining as you see:

Title of the page is -> null


Solution

To send a character sequence with in the userNameInput and userPasswordInput fields of the website https://qualysguard.mybank.com/fo/login.php?idm_key=saml2_70d8552f0974 you have to induce WebDriverWait for the elementToBeClickable() and you can use either of the following solutions:

  • 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();
    

Important: Ensure that HtmlUnitDriver() is not resolved from :

com.gargoylesoftware.htmlunit.BrowserVersion;


Additional Consideration

Your Selenium Client version is 3.141.0 of 2018-10-31T20:09:30 which is almost more then a years older. Ensure that Selenium is upgraded to current levels Version 3.141.59.


References

You can find a couple of relevant discussions in:

这篇关于无法为我的测试用例运行java junit硒代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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