【问题标题】:How to resolve UmbrellaException error in java using phantomjs and selenium如何使用phantomjs和selenium解决java中的UmbrellaException错误
【发布时间】:2018-10-05 01:02:07
【问题描述】:

我想使用 selenium 和 phantomJs 自动测试登录页面这是我的代码,它可以在 firefox 上正常工作,但我需要使用 phantomJs

try {
        DesiredCapabilities caps = new DesiredCapabilities();
        caps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, "D:\\phantomjs_2_1_1\\bin\\phantomjs.exe");
        caps.setCapability(PhantomJSDriverService.PHANTOMJS_PAGE_SETTINGS_PREFIX,"Y");
        caps.setCapability("phantomjs.page.settings.userAgent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:16.0) Gecko/20121026 Firefox/16.0");
        caps.setJavascriptEnabled(true);
        caps.setCapability("takesScreenshot", true);

        PhantomJSDriver driver = new PhantomJSDriver(caps);

        driver.get("mypage-login");

        TimeUnit.SECONDS.sleep(3);

        File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);


        FileUtils.copyFile(scrFile, new File("d:\\sample.png"),true);

        System.out.println("FIND ELEMENT [OK] ");


        new WebDriverWait(driver, 120).until(
                ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@id=\"elemId17\"]"))).click();

        scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);

        System.out.println("scrFile := " + scrFile.getAbsolutePath());
        FileUtils.copyFile(scrFile, new File("d:\\sample2.png"),true);

        TimeUnit.SECONDS.sleep(2);

        driver.findElement(By.xpath("//*[@id=\"x-auto-1-input\"]")).sendKeys("username");
        driver.findElement(By.xpath("//*[@id=\"x-auto-5-input\"]")).sendKeys("domain");
        driver.findElement(By.xpath("//*[@id=\"x-auto-4-input\"]")).sendKeys("password");

        new WebDriverWait(driver, 120).until(
                ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@id=\"elemId98\"]"))).click();

        scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(scrFile, new File("d:\\sample3.png"),true);

        if (driver.findElement(By.id("elemId98")) == null)
            System.out.println("OK");
        System.out.println("KO");


    } catch (InterruptedException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }catch (NullPointerException e) {
        e.printStackTrace();
    }
    finally {
        System.out.println("life is good !");
    }

得到的错误是:

ERROR - 2018-04-24T16:23:41.092Z] Session [cb29bc00-47db-11e8-9aae-df14adbccf9e] - page.onError - msg: Error: com.google.gwt.event.shared.UmbrellaException: Exception caught: (TypeError) : null is not an object (evaluating 'result[1]')
phantomjs://platform/console++.js:263 in error
[ERROR - 2018-04-24T16:23:41.092Z] Session [cb29bc00-47db-11e8-9aae-df14adbccf9e] - page.onError - stack: vkb (mypage:326)
 dispatchEvent (:0)
U (:119) $ (:108)
$ (:101)
gh (:141)
sh (:152)
(anonymous function) (:152)
(anonymous function) (:152)
(anonymous function) (:153)
phantomjs://platform/console++.js:263 in error

在屏幕截图中,我可以看到一些问题;在最后一次测试(下面的代码)之后,我在睡眠 10 秒后添加了一个新的 TakesScreenshot,结果与之前在屏幕截图 3 'simple3' 中的结果相同,它似乎在此阶段被阻止(单击登录按钮和动画显示加载索引或错误页面)

 if (driver.findElement(By.id("elemId98")) == null)
            System.out.println("OK");
        System.out.println("KO");

我正在收听的任何其他信息,并提前感谢您:)

【问题讨论】:

    标签: java selenium selenium-webdriver gwt phantomjs


    【解决方案1】:

    UmbrellaException

    UmbrellaExceptiongwtprojectcom.google.web.bindery.event.shared 包中定义的 Java RuntimeException,它将一组子 Throwables 收集在一起.此异常通常在循环之后抛出,所有异常都在该循环期间抛出,但会延迟以使循环完成执行。

    详细的错误堆栈跟踪将帮助我们以更好的方式研究问题。但根据以下讨论:

    com.google.gwt.event.shared.UmbrellaException 的根本原因似乎是 java.lang.NullPointerException

    问题及解决办法

    您需要注意代码中的某些事情,如下所示:

    • 当您尝试调用click() 而不是ExpectedConditions 方法presenceOfElementLocated() 时返回标识为(By.xpath("//*[@id=\"elemId98\"]")) 的元素时,您必须使用elementToBeClickable(),如下所示:

      new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id='elemId98']"))).click();
      

      这应该是等待任何元素可点击时练习的一部分。

    • 如果在 WebDriverWait 之后返回标识为 (By.xpath("//*[@id=\"elemId98\"]")) 的元素,并且随后在下一步中调用 click()findElement() 将无法再次找到前一个元素,如下所示:

      if (driver.findElement(By.id("elemId98")) == null)
      

      这里可能应该提出NoSuchElementExceptionStaleElementReferenceException

    • 接下来,您尝试将 driver.findElement(By.id("elemId98")) 的结果与不符合最佳实践的 null 进行比较。根据findElement() 的文档,明确提到:

    findElement() 不应用于查找不存在的元素,而应使用findElements(By) 并断言零长度响应。

    结论

    所有这些问题/错误都发生在 **try-catch {}** 块中,这些块共同引发 com.google.gwt.event.shared.UmbrellaException。遵守上述步骤即可解决问题。

    【讨论】:

    • 感谢 [stackoverflow.com/users/7429447/debanjanb]DebanjanB 重播,但问题仍然存在我在new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id='elemId98']"))).click(); 后添加System.exit(-2); 错误存在!这意味着问题出在这个点击之前,因为当我添加系统退出时,一切都很好
    • @Mks 让我们调试问题,在 try{} 块外拉出一行并验证 new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id='elemId98']"))) 返回的内容。如果 timesout 问题出在 xpath 上。在这种情况下,使用相关的 HTML 更新问题
    • 我想要以下内容:WebElement obj = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id='elemId98']"))); System.out.println("OBJ Click := " + obj.getText());
    • 那么输出是什么?
    • System.out.println("OBJ Click := " + obj.getText()); 到此为止没关系,输出是:登录(按钮中的文本)但是当我添加 obj.click();错误卷土重来
    猜你喜欢
    • 1970-01-01
    • 2018-01-16
    • 1970-01-01
    • 1970-01-01
    • 2014-05-04
    • 2018-06-29
    • 2021-06-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多