18

timeout exceptionポップアップ ウィンドウが原因で Selenium がスローされるという問題が発生しました

  unexpected alert open
  not provide any stacktrace information)
  Command duration or timeout: 5 milliseconds

アラートにはOKCANCELボタンがあります。これを処理する2つの方法を知っています


最初の方法は、新しいセッションを再開することです

driver.quit();
driver = new ChromeDriver();

2番目の方法は Robot クラスを使用することです

Robot r = new Robot();
r.keyPress(KeyEvent.VK_ENTER);
r.keyRelease(KeyEvent.VK_ENTER);

ただし、この方法は時間効率がよくありません。もっと良い方法はありますか?

4

8 に答える 8

19

Selenium でアラートを処理するメソッド

  1. それぞれ個別に決める

テストの各アラートに対して個別にアクションを実行する必要がある場合、ドライバーは、アラートに切り替えて、それを受け入れるか無視するかを決定するオプションを提供します。

driver.switchTo().alert().accept();

  1. デフォルト設定で処理

すべてのアラートを同じ方法で処理する場合は、テスト実行の開始時にグローバル機能を設定して、アラートが表示されたときにデフォルトでACCEPTINGOREまたはDISMISSアラートにすることができます。

capabilities.setCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR, UnexpectedAlertBehaviour.ACCEPT);

  1. ロボットクラスの使用

または、Robot クラスを使用して、アラートを受け入れる Enter キー イベントを送信することもできます。

Robot r = new Robot();
 
r.keyPress(KeyEvent.VK_ENTER);
r.keyRelease(KeyEvent.VK_ENTER);

于 2015-12-08T01:22:33.367 に答える
4

これを試して、

public boolean isAlertPresent() {

    boolean presentFlag = false;

    try {

        // Check the presence of alert
        Alert alert = driver.switchTo().alert();
        // Alert present; set the flag
        presentFlag = true;
        // if present consume the alert
        alert.accept();
        //( Now, click on ok or cancel button )

    } catch (NoAlertPresentException ex) {
        // Alert not present
        ex.printStackTrace();
    }

    return presentFlag;
}

これがお役に立てば幸いです。

于 2013-10-04T05:15:36.763 に答える
2

多くの場合、この問題は、テスト中のシステムの予測できない場所で発生するという点で厄介です。現時点では、これらの不確実性をすべて webdriver の構成によって自動的に処理する方法が存在するとは思いません。私の一般的なアドバイスは、webDriver を Proxy でラップし、ある種の動的プロキシを使用してすべての webdriver メソッドをラップすることです。このようにして、予測できないアラートを一元的に制御し、ログを作成したり、メソッドのパフォーマンスを評価したり、ランダムな unreachableBrowser 例外を処理したり、ランダムな StaleElementException を処理したりできます。これは、さまざまな状況で非常に役立ちますが、パフォーマンスのペナルティはわずかです。 .

        Class WebDriverProxy implements InvocationHandler{
       WebDriverWrapperImpl impl = new WebDriverWrapperImpl();

        public String clickByXPath(String xpath)  {
            return (String)handleInvocation(impl,"clickByXPath", new Object[]{xpath});
            //  return impl.clickByXPath( xpath) ;
        }


    /**All fail fast strategies could be centralized here., no need of any assertion errors in libraries,
         * However it makes sense to wrap webdriver exceptions as either recoverable or nonrecoverable
         * recoverable ones are like unexpected hangs on the browser, which could be handled at the test runner level, wherein the 
         * whole test can be retried.
         * irrecoverable ones are also mostly handled at the test runner level, but capable of being caught at the test script level *  
         **/
        @Override
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
        {
            Object o = null;
            Throwable target = null;
            try{
                o = method.invoke(proxy, args);
            }       
            catch(InvocationTargetException ee){            
                target = ee.getTargetException();
                throw target;
            }
            return o;
        }

        public Object handleInvocation(Object proxy, String method, Object[] args){     

            Object toReturn = null;
            Method m = null;
            Class[] classes = new Class[args.length];
            for(int i = 0;i<args.length;i++){
                classes[i]=String.class;
            }
            for(Object x:args){
            logBuffer.append(x.toString()+","); 
            }
            log.trace("WebDriverProxy. "+method+"("+logBuffer.toString()+")");
            logBuffer = new StringBuffer();
            try{
                 m = proxy.getClass().getMethod(method,classes);

                toReturn = invoke(proxy,m, args);

            }catch(NoSuchMethodException e){    
                e.printStackTrace();

            }catch( StaleElementReferenceException e){
                log.debug("Exception was of tye "+e.getClass().getCanonicalName());






            }
            catch(UnreachableBrowserException | NoSuchElementException e){
                log.debug("Exception was of tye "+e.getClass().getCanonicalName());
                //If the NoSuchelement is due to suspect Alerts being present, switchToAlert() and alert.accept() here.
            }



            return toReturn;
        }



        }


class WebDriverWrapperImpl {
 WebDriver driver = new ChromeDriver();
  public String clickByXPath(String xpath)  throws Exception{
            driver.findElement(By.Xpath(xpath)).click();
            return driver.getTitle();
        }

}
于 2017-03-01T12:28:24.577 に答える
1
ChromeOptions options = new ChromeOptions();
options.setUnhandledPromptBehaviour(ACCEPT);
WebDriver driver = new ChromeDriver(options);

代わりに、要件に応じて 、次ACCEPTの列挙型定数、、、、を渡すことができます。ACCEPTACCEPT_AND_NOTIFYDISMISSDISMISS_AND_NOTIFYIGNORE

于 2019-09-27T18:46:21.820 に答える
0

TestNG などのフレームワークを使用している場合は、 ITestListener などのリスナーを使用して、BeforeCommand や afterCommand などのメソッドをオーバーライドする必要があります。そのため、BeforeCommand でアラートを解除して美しさを確認するためのコードを実装します。Selenium コマンドが実行されるたびに、この beforeCommand メソッドが自動的に呼び出され、アラートが存在するかどうかがチェックされます。はいの場合、コマンドを閉じて実行します。私はそれがあなたの問題を解決することを願っています

于 2014-10-05T04:15:20.270 に答える