我习惯用Python开发,但出于工作原因必须用Java来实现.我正面临着一个在Python中无足轻重的任务,我想了解如何以正确的方式在Java中处理这个问题.
我需要解析持续时间字符串.它可以是毫秒(235毫秒)或秒(32秒).它也可以是"<1ms"作为特例.
解析在代码中至少发生三次,所以我想把它分成一个方法.但是我的代码确实需要知道,不仅仅是以ms为单位的结果值,还有它是ms还是s以及是否<1ms(0是不同的值).
在Python中,我只返回一个元组:
return (value_in_milliseconds,is_in_seconds,is_under_1ms)
Run Code Online (Sandbox Code Playgroud)
在CI中将定义这三个值的结构并返回它.在Pascal我定义了一条记录.
在Java中,我无法返回元组,我无法定义记录,所以我该怎么办?
我唯一能想到的是创建一个表示持续时间值的类.构造函数将获取字符串并解析它.该类将具有字段:int milliseconds,boolean inSeconds,boolean小于1ms.
但这听起来非常重量级 - 有更好的解决方案吗?
所以我正在尝试重新定位并解决冲突。git rebase 命令确实列出了它们,但是有什么方法可以让我稍后使用单独的 git 命令查看冲突列表,而无需继续或跳过 rebase?
(我正在编写一个脚本来自动变基以支持一个分支或另一个分支,而不必为每个文件启动编辑器。但是 grep 整个存储库以查找 >>>>>> 和 <<<<<< 是相当耗时,所以我宁愿直接访问列表)
我正在使用Selenium 3.4和Java.使用Chrome,一切正常.但是我需要使用Firefox,并且有些东西会中断.
我正在自动测试Dojo UI,并且需要等待Dojo UI进行大量渲染.所以这就是我所做的,它在Chrome中运行得很好.请注意,通常在我的代码中设置隐式等待20秒.
driver.switchTo().defaultContent();
driver.switchTo().frame(driver.findElement(By.id("contentframe"))); // relying on implicit wait
driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS);
(new WebDriverWait(driver, 120)).
until(ExpectedConditions.elementToBeClickable(By.id("some_id")));
Run Code Online (Sandbox Code Playgroud)
我已经简化了代码,因此您不会看到隐式等待如何设置回20秒.当问题发生时,无论如何它都无法到达那里.WebDriverWait导致异常.例外说TypeError: can't access dead object
等待中有相应的消息:
May 16, 2017 3:36:11 PM org.openqa.selenium.support.ui.ExpectedConditions findElement
WARNING: WebDriverException thrown by findElement(By.id:
some_id)
org.openqa.selenium.WebDriverException: TypeError: can't access dead object
Run Code Online (Sandbox Code Playgroud)
显然,geckodriver还有一些JavaScript错误输出:
JavaScript error: chrome://marionette/content/listener.js, line 1555: TypeError: can't access dead object
*************************
A coding exception was thrown and uncaught in a Task.
Full message: TypeError: can't access dead object
Full stack: find_@chrome://marionette/content/element.js:284:7
element.find/</findElements<@chrome://marionette/content/element.js:255:15
implicitlyWaitFor/</elementSearch@chrome://marionette/content/element.js:600:15
implicitlyWaitFor/<@chrome://marionette/content/element.js:627:5
implicitlyWaitFor@chrome://marionette/content/element.js:593:10 …
Run Code Online (Sandbox Code Playgroud) 我知道,根据一些意见,在完美的情况下,我不应该使用隐式等待。但是我的用户界面很慢;将显式等待添加到所有可能发生渲染的地方将是一项相当大的努力。在驱动程序初始化时设置的标准隐式等待时间为 20 秒,完美地涵盖了 80% 的等待时间。
但接下来我需要处理剩下的 20%。有时我需要等待一个元素消失 - 在这种情况下,隐式等待意味着额外的 20 秒。有时我需要检查一个元素是否存在,但不需要等待 20 秒(因为该元素通常不在正常执行流程中)。
所以,我自然想暂时禁用隐式等待:
driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS);
new WebDriverWait(driver, MY_TIMEOUT)).until(ExpectedConditions.WHATEVER_I_NEED);
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
Run Code Online (Sandbox Code Playgroud)
但这会稳定吗?或者更改隐式等待(在这种情况下,从 20 到 0 再回到 20)是否有某种危险?
编辑:评论者正确地指出,事实上,我正在使用的 HTML 代码都在一个标签内,这似乎对这个特殊情况产生了决定性的影响。
我试图在任何级别检索元素中的所有元素。实际上,我也需要做一些额外的检查,所以我需要使用 By.xpath,而不是 By.tagName。但是 By.xpath 不知何故不起作用,即使没有检查。(当然,其他检查也失败了 - 我只是提供了最小的例子)。
我的代码:
log.info(eventGroup.getAttribute("outerHTML"))
alerts = eventGroup.findElements(By.xpath(".//g"));
log.info(alerts.size());
Run Code Online (Sandbox Code Playgroud)
第一行用于调试,以确保我得到正确的元素。这是它的输出:
<g class="event-group">
<line x1="-105" x2="927.4000244140625" y1="0" y2="0" class="events-bar"/>
<line x1="-105" x2="927.4000244140625" y1="45" y2="45" class="events-bar"/>
<line id="markLine" x1="0" x2="0" y1="0" y2="0" y1save="22.5" y2save="537"/>
<text x="-30" y="26.5" text-anchor="end">Alerts</text><g clip-path="url(#event-clip)">
<g class="one-event">
<use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#9warning" class="event warning" x="240.04638877275397" y="17.5" width="10" height="10" data-id="58fe7efa97d469000d047594_2017-04-24T22:40:37.679Z"/>
<rect class="event-overlay" x="235.04638877275397" y="12.5" width="20" height="20"/>
</g><g class="one-event">
<use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#9warning" class="event warning" x="-4.624127036138464" y="17.5" width="10" height="10" data-id="58fe238d97d469000d0450b3_2017-04-24T16:10:37.710Z"/>
<rect class="event-overlay" x="-9.624127036138464" y="12.5" width="20" height="20"/> …
Run Code Online (Sandbox Code Playgroud) 我需要初始化一个ImmutableMap(番石榴21.0),我需要它来解析一个Map>对于这个例子,我将只使用String.
所以我有:
import com.google.common.collect.ImmutableMap;
public class MyClass {
private Map<String,String> testMap =
ImmutableMap<String,String>.builder().put("a","b").build();
Run Code Online (Sandbox Code Playgroud)
在最后一行,我得到了大量的编译器错误.使用ImmutableMap<String,String>.of()
得到相同的结果.
如果我删除我只是得到一个错误,类型不匹配.
当我想要一个具有显式类型的地图时,如何使用ImmutableMap进行显式(类文字)初始化?