本文介绍了无法在Java中使用phantomJS处理警报的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下面的Java代码,当我通过PhantomJs运行时遇到不受支持的命令异常",但是如果我通过firefox和chrome运行,则可以正常工作:

注意:使用phantomJs,我们可以执行以下代码中的第3步.我在许多博客中进行了搜索,但是这些答案并不能解决我的问题.

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

此处cvvField和procedToPayBtn是WebElement,而cvcData的值为"111".

错误日志:-

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

命令持续时间或超时:31 milliseconds

 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();
       }
解决方案

由于等待时间而导致的某些问题可能是问题的根源上面的代码可以帮助等待元素可见(因为ngWebDriver或Selenium Webdriver的等待与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;
    }

此代码将等待10毫秒,然后验证元素是否可见,如果有异常,它将再次循环.返回的值必须是文本,对象或任何非null的值.该脚本值必须是您的JS脚本才能获取正确的元素.

希望它能正常工作.

我通过以下方式尝试了上述代码:-

1.创建一个"Test"类,并在其中编写上述方法.2.通过创建一个对象(TestObject)为

来调用上述方法

但是其中的ReturnedValue

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

}

返回null.那么您能帮忙吗?

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

 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();
       }
解决方案

Some issue due to wait time can be the source of your problemThe 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

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?

这篇关于无法在Java中使用phantomJS处理警报的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 09:26
查看更多