Unable to run java junit selenium code for my test case

有些话、适合烂在心里 提交于 2020-03-02 07:42:47

问题


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 ?


回答1:


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:

  • Unable to launch HtmlUnitdriver through Selenium 3.4.0
  • HtmlUnitDriver does not load javascript when navigating a page from an url


来源:https://stackoverflow.com/questions/59046080/unable-to-run-java-junit-selenium-code-for-my-test-case

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