无法在Java中使用phantomJS处理警报 [英] Unable to handle alert using phantomJS in Java

查看:228
本文介绍了无法在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)为

来调用上述方法

TestObject.waitJSResponse((PhantomJSDriver)驱动程序,"window.confirm = function(){返回true;};");

但是其中的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

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.

解决方案

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?

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

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