如何在selenium中加载primefaces对话框?

时间:2013-06-29 20:01:17

标签: selenium primefaces webdriver selenium-webdriver

我正在尝试在 selenium 测试中加载 primefaces对话框,如下所示:

- 主要代码:

   <p:commandLink id="iam_interested1" styleClass="iam_interested"
   oncomplete="signUpDialog.show()"
   actionListener="#{leadsBean.initSaveAction()}"
   update=":betasignup">
   I'm interested
   </p:commandLink>

- 以上primefaces代码的生成代码是:

<a id="headerForm:iam_interested1" href="#" class="ui-commandlink ui-widget iam_interested" onclick="PrimeFaces.ab({source:'headerForm:iam_interested1',update:'betasignup',oncomplete:function(xhr,status,args){signUpDialog.show();}});return false;">
                            I'm interested
                        </a> 

硒代码:

    @Test
    public void test() throws InterruptedException {
        driver.get("http://localhost:8080/MyApp/index.xhtml");
        WebElement dialogLink = driver.findElement(By
                .className("iam_interested"));
        dialogLink.click();
        String currentWindowHandle = driver.getWindowHandle();
        String popupWindowHandle  =  getPopupWindowHandle(driver,dialogLink);
        driver.switchTo().window(popupWindowHandle);
        (new WebDriverWait(driver, 5)).
        until(ExpectedConditions.elementToBeClickable(By.className("ui-dialog-titlebar-close")));
        driver.findElement(By.className("ui-dialog-titlebar-close")).click();
        driver.switchTo().window(currentWindowHandle);


    }

    String getPopupWindowHandle(WebDriver driver, WebElement element) {

        String popupHandle = null;
        int maxTimeToWait = 5000;
        int timeInterval = 1000;
        int timeElasped = 0;
        int attempt;

        // get all the window handles before the popup window appears
        Set<String> beforePopup = driver.getWindowHandles();

        // click the link which creates the popup window
        element.click();
        System.out.println("element clicked");

        attempt = 1;
        while (timeElasped < maxTimeToWait) {

            // get all the window handles after the popup window appears
            Set<String> afterPopup = driver.getWindowHandles();

            // remove all the handles from before the popup window appears
            afterPopup.removeAll(beforePopup);

            // there should be only one window handle left
            if (afterPopup.size() == 1) {
                System.out.println("Popup window found");
                popupHandle = (String) afterPopup.toArray()[0];
                break;
            }

            try {
                Thread.sleep(timeInterval);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            timeElasped = timeInterval * attempt;
            attempt++;
        }
        if (popupHandle.isEmpty() || popupHandle == null) {
            System.out.println("No Popup Window found");
        }

        return popupHandle;

    } 

但是对话框没有加载,而是我得到了异常:

org.openqa.selenium.interactions.MoveTargetOutOfBoundsException: Element cannot be scrolled into view:http://localhost:8080/MyApp/betasignup_index.xhtml#
    Command duration or timeout: 40 milliseconds
    Build info: version: '2.33.0', revision: '4ecaf82108b2a6cc6f006aae81961236eba93358', time: '2013-05-22 12:00:17'
    System info: os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.7.0_25'
    Session ID: 1ff0ca5a-5906-412f-b2a7-6e170a7d8932
    Driver info: org.openqa.selenium.firefox.FirefoxDriver
    Capabilities [{platform=XP, databaseEnabled=true, cssSelectorsEnabled=true, javascriptEnabled=true, acceptSslCerts=true, handlesAlerts=true, browserName=firefox, browserConnectionEnabled=true, nativeEvents=true, webStorageEnabled=true, rotatable=false, locationContextEnabled=true, applicationCacheEnabled=true, takesScreenshot=true, version=21.0}]
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
        at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:191)
        at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:145)
        at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:554)
        at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:268)
        at org.openqa.selenium.remote.RemoteWebElement.click(RemoteWebElement.java:79)
        at com.motivosity.selenium.HomeTest.addingOneUser(HomeTest.java:32)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:606)
        at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
        at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
        at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
        at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
        at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
        at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
        at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
        at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
        at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
        at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
        at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
        at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
        at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
        at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.runTestClass(JUnitTestClassExecuter.java:80)
        at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.execute(JUnitTestClassExecuter.java:47)
        at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassProcessor.processTestClass(JUnitTestClassProcessor.java:69)
        at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.processTestClass(SuiteTestClassProcessor.java:49)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:606)
        at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
        at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
        at org.gradle.messaging.dispatch.ContextClassLoaderDispatch.dispatch(ContextClassLoaderDispatch.java:32)
        at org.gradle.messaging.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:93)
        at com.sun.proxy.$Proxy2.processTestClass(Unknown Source)
        at org.gradle.api.internal.tasks.testing.worker.TestWorker.processTestClass(TestWorker.java:103)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:606)
        at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
        at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
        at org.gradle.messaging.remote.internal.hub.MessageHub$Handler.run(MessageHub.java:355)
        at org.gradle.internal.concurrent.DefaultExecutorFactory$StoppableExecutorImpl$1.run(DefaultExecutorFactory.java:66)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
        at java.lang.Thread.run(Thread.java:724)

请告知如何解决。

1 个答案:

答案 0 :(得分:0)

问题是生成的<a>组件在声明的组件旁边有多个生成的类,所以当我尝试通过id获取链接元素时,并且做正常点击,一切正常:

WebElement dialogLink = driver.findElement(By
                .id(dialogLinkId));
        dialogLink.click();