Set<String> windowHandles = driver.getDriver().getWindowHandles(); String homeHandles = ""; for (String windowHandle : windowHandles) { driver.getDriver().switchTo().window(windowHandle); if(driver.getDriver().getTitle().contains("远光九天")) { homeHandles=windowHandle; continue; }
时间: 2024-04-17 16:28:42 浏览: 18
这段代码用于获取当前打开的所有窗口的句柄,并在遍历过程中找到标题包含"远光九天"的窗口句柄。
具体步骤如下:
1. 使用`driver.getDriver().getWindowHandles()`方法获取当前打开的所有窗口的句柄,并将其保存在一个`Set<String>`类型的变量`windowHandles`中。
2. 定义一个空字符串变量`homeHandles`,用于保存首页的窗口句柄。
3. 使用`for`循环遍历`windowHandles`中的每个窗口句柄。
4. 在每次循环中,使用`driver.getDriver().switchTo().window(windowHandle)`方法切换到当前窗口。
5. 使用`driver.getDriver().getTitle()`方法获取当前窗口的标题,并使用`contains("远光九天")`判断标题是否包含"远光九天"。
6. 如果当前窗口的标题包含"远光九天",则将该窗口句柄保存到`homeHandles`变量中,并使用`continue`跳过后续代码的执行,进入下一次循环。
7. 循环结束后,`homeHandles`变量中保存了标题包含"远光九天"的窗口句柄。
请注意,以上代码只是一部分实现,缺少后续的操作。如果需要更多信息,请提供完整的代码或提供更多上下文。
相关问题
Set<String>keys=m.keySet();作用
这段代码的作用是获取一个包含Map m中所有键的Set集合。其中,m是一个Map对象,keys是一个Set<String>类型的变量,表示存放m中所有键的Set集合。可以通过遍历keys集合来访问m中所有的键。例如:
```java
Map<String, Integer> m = new HashMap<>();
m.put("a", 1);
m.put("b", 2);
m.put("c", 3);
Set<String> keys = m.keySet();
for (String key : keys) {
System.out.println(key + " : " + m.get(key));
}
```
输出结果为:
```
a : 1
b : 2
c : 3
```
可以看到,通过遍历keys集合,可以访问Map m中所有的键,并通过m.get(key)方法获取对应的值。
优化 private void addReferencedKpi(List<AnalysisTableDto> list, List<String> codeList, List<Map<String, List<String>>> referencedList) { if (CollectionUtils.isEmpty(list) || CollectionUtils.isEmpty(codeList) || CollectionUtils.isEmpty(referencedList)) { return; } // 转换结构 key1-table key2-column value-codeList Map<String, Map<String, List<String>>> tableColumnWithCodeListMap = Maps.newHashMap(); int size = referencedList.size(); for (int i = 0; i < size; i++) { String code = codeList.get(i); Map<String, List<String>> referencedPerMap = referencedList.get(i); if (MapUtils.isEmpty(referencedPerMap)) { continue; } referencedPerMap.forEach((table, columns) -> { tableColumnWithCodeListMap.putIfAbsent(table, Maps.newHashMap()); for (String column : columns) { tableColumnWithCodeListMap.get(table).putIfAbsent(column, Lists.newArrayList()); tableColumnWithCodeListMap.get(table).get(column).add(code); } }); } // 装饰引用kpi for (AnalysisTableDto simulationTableDto : list) { String simTableName = simulationTableDto.getTableName(); for (SimulationColumnDto column : simulationTableDto.getColumns()) { String simTitle = column.getTitle(); if (!tableColumnWithCodeListMap.containsKey(simTableName) || !tableColumnWithCodeListMap.get(simTableName).containsKey(simTitle)) { continue; } List<String> referencedCodeList = tableColumnWithCodeListMap.get(simTableName).get(simTitle); column.setReferencedKpis(referencedCodeList); } } }
这段代码的主要功能是将一个数据结构进行转换,并将转换后的结果装饰到数据模型中。下面是一些优化建议:
1. 使用stream API来简化代码,提高可读性和可维护性。
2. 使用Optional类来避免对null进行判断。
3. 使用更具有描述性的变量名来提高可读性。
4. 将List转换为Set来提高查找效率。
下面是优化后的代码:
```
private void addReferencedKpi(List<AnalysisTableDto> tableDtoList, List<String> codeList, List<Map<String, List<String>>> referencedList) {
if (CollectionUtils.isEmpty(tableDtoList) || CollectionUtils.isEmpty(codeList) || CollectionUtils.isEmpty(referencedList)) {
return;
}
Map<String, Map<String, Set<String>>> tableColumnWithCodeMap = new HashMap<>();
IntStream.range(0, referencedList.size())
.filter(i -> MapUtils.isNotEmpty(referencedList.get(i)))
.forEach(i -> {
String code = codeList.get(i);
referencedList.get(i).forEach((table, columns) -> {
Map<String, Set<String>> columnWithCodeMap = tableColumnWithCodeMap.computeIfAbsent(table, k -> new HashMap<>());
columns.forEach(column -> columnWithCodeMap.computeIfAbsent(column, k -> new HashSet<>()).add(code));
});
});
tableDtoList.forEach(tableDto -> {
String tableName = tableDto.getTableName();
tableDto.getColumns().stream()
.filter(column -> tableColumnWithCodeMap.containsKey(tableName) && tableColumnWithCodeMap.get(tableName).containsKey(column.getTitle()))
.forEach(column -> column.setReferencedKpis(tableColumnWithCodeMap.get(tableName).get(column.getTitle())));
});
}
```