Unable to handle alert using phantomJS in Java

2019-07-26 20:30发布

问题:

I have a Java code as below and when I am running through PhantomJs getting "Unsupported Command Exception" but it is working fine if I run through firefox and chrome:-

Note: With phantomJs we could able to execute till 3rd step in below code.I searched in many blogs but those answers didn't solve my problem.

1.      cvvField.sendKeys(cvcData);
2.      proceedToPayBtn.click();
3.      Reporter.log("Card details are submitted from payment UI page");
4.      Alert a1=driver.switchTo().alert();
5.      Reporter.log("Alert with text:"+a1.getText());
6.      a1.accept();   

Here cvvField and proceedToPayBtn are WebElements and cvcData have value as "111".

Error log:-

org.openqa.selenium.UnsupportedCommandException: Invalid Command Method - 

{"headers":{"Accept-Encoding":"gzip,deflate","Cache-Control":"no-cache","Connection":"Keep-Alive","Host":"localhost:30462","User-Agent":"Apache-HttpClient/4.5.1 (Java/1.8.0_101)"},"httpVersion":"1.1","method":"GET","url":"/alert_text","urlParsed":{"anchor":"","query":"","file":"alert_text","directory":"/","path":"/alert_text","relative":"

/alert_text","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/alert_text","queryKey":{},"chunks":["alert_text"]},"urlOriginal":"/session/9e392a50-ce79-11e6-b24a-2b12cf1ec4d6/alert_text"}

Command duration or timeout: 31 milliseconds

I have edited above code as below but same error is coming.Please suggest

 if (driver instanceof PhantomJSDriver)
       {
         JavascriptExecutor je = (JavascriptExecutor) driver; 
         je.executeScript("window.alert = function(){};");
         je.executeScript("window.confirm = function(){return true;};");    
         System.out.println("Alert has been handled");
       } else {
             Alert a1 = driver.switchTo().alert();
             a1.accept();
       }                        

I am getting "Alert has been handled" in output console but alert is not handled.

回答1:

Some issue due to wait time can be the source of your problem The code above can help to wait until element is visible (since the wait of ngWebDriver or Selenium Webdriver are not compatible with PhantomJS)

public static String waitJSResponse(PhantomJSDriver driver, String script) {
        String ReturnedValue = null;
        int sleeper = 10;
        Boolean flag = false;
        int timeOut = 30000;
        int i = 0;
        while ((!flag) & ((i*sleeper)<timeOut)) {
            try {
                Thread.sleep(sleeper);
                ReturnedValue = (String) driver.executeScript(script);

            } catch (Exception e) {
                flag = false;
                i++;
            }
            if (ReturnedValue != null) {
                flag = true;
                System.out.println("Overall wait time is : "+(i * sleeper)+" ms \n\r");
                break;
            }
        }
        return ReturnedValue;
    }

This code will wait 10ms then verify that the element is visble, if there is an exception, it will loop again. The returned value must be a text, an object or anything that is not null. the script value must be your JS script to get the correct element.

Hope it work.

I tried the above code by:-

1.Creating a class "Test" and writing above method in it. 2.Above method is called by creating an object(TestObject) as

TestObject.waitJSResponse((PhantomJSDriver) driver, "window.confirm = function(){return true;};");

But ReturnedValue in

try {
Thread.sleep(sleeper); ReturnedValue = (String) driver.executeScript(script); System.out.println(ReturnedValue);

}

returns null.So Can u please help with this?