public void operationAndCloseWindows(String operation,String result,HashMap<String, String> xpathMap) { //获取所有窗口句柄 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; } sleep(8); try { //执行操作 common.clickXpath(xpathMap.get(operation)); } catch (Exception e) { clickButtonText("关闭"); //执行操作 common.clickXpath(xpathMap.get(operation)); } if(result.equals("服务连接管理")) { clickButtonText("确定"); } else if(result.equals("协同设计中心")){ clickButtonText("取消"); } else if(result.equals("用户体验创新中心")) { clickButtonText("取消"); } else if(result.equals("研发运营基础云平台")) { clickButtonText("取消"); } sleep(2); //断言 assertText(result); driver.getDriver().close(); } driver.getDriver().switchTo().window(homeHandles); }
时间: 2024-04-17 22:29:25 浏览: 28
这是一个名为`operationAndCloseWindows`的公共方法,它接受三个参数:`operation`,`result`和`xpathMap`。
该方法的目的是执行一系列操作,并关闭除首页以外的所有窗口。具体步骤如下:
1. 获取所有窗口句柄。
2. 遍历所有窗口句柄,如果当前窗口的标题包含"远光九天",则将其保存为首页的句柄,并继续下一次循环。
3. 在当前窗口执行操作,通过`xpathMap`参数中的映射获取操作对应的XPath,并点击该元素。
4. 如果操作失败,则点击一个名为"关闭"的按钮,然后再次执行操作。
5. 根据`result`参数的值执行不同的操作:
- 如果`result`为"服务连接管理",则点击一个名为"确定"的按钮。
- 如果`result`为"协同设计中心"、"用户体验创新中心"或"研发运营基础云平台",则点击一个名为"取消"的按钮。
6. 等待2秒后进行断言,验证文本是否与`result`参数的值匹配。
7. 关闭当前窗口。
8. 切换回首页的窗口。
请注意,以上代码只是一个方法的部分实现,无法全面判断其功能是否完整和正确。如果需要更多信息,请提供更多上下文或完整的代码。
相关问题
在Java中用Dom4j把这一段xml内容转成Map对象:<?xml version="1.0" encoding="UTF-8"?> <java version="1.6.0_26" class="java.beans.XMLDecoder"> <object class="java.util.HashMap"> <void method="put"> <string>att_20160719180419281NEk9</string> <string>cx</string> </void> <void method="put"> <string>att_20160511094248589ZKYq</string> <string>cnmm</string> </void> <void method="put"> <string>att_20161018153614188aTus</string> <string>zsx;fj</string> </void> <void method="put"> <string>att_20160422110906494XuEf</string> <null/> </void> <void method="put"> <string>att_20160511130210612UULE</string> <string>fjyxapp;jjgjapp;wx;jd;dxjg;tb;wsjy</string> </void> <void method="put"> <string>att_20160719180542840LnFm</string> <string></string> </void> <void method="put"> <string>att_20160511130025203ssle</string> <string>zdgsszta</string> </void> <void method="put"> <string>att_20160810131359346N0ra</string> <string>szjys</string> </void> <void method="put"> <string>att_20160511130031927equg</string> <string></string> </void> <void method="put"> <string>att_20160422110456036qp7Y</string> <null/> </void> <void method="put"> <string>att_20160422110543585f7iI</string> <string>cpjbxx</string> </void> </object> </java>
您可以使用以下代码将该XML转换为Map对象:
```java
import java.io.StringReader;
import java.util.HashMap;
import java.util.Map;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class XmlToMap {
public static void main(String[] args) throws Exception {
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?> <java version=\"1.6.0_26\" class=\"java.beans.XMLDecoder\"> <object class=\"java.util.HashMap\"> <void method=\"put\"> <string>att_20160719180419281NEk9</string> <string>cx</string> </void> <void method=\"put\"> <string>att_20160511094248589ZKYq</string> <string>cnmm</string> </void> <void method=\"put\"> <string>att_20161018153614188aTus</string> <string>zsx;fj</string> </void> <void method=\"put\"> <string>att_20160422110906494XuEf</string> <null/> </void> <void method=\"put\"> <string>att_20160511130210612UULE</string> <string>fjyxapp;jjgjapp;wx;jd;dxjg;tb;wsjy</string> </void> <void method=\"put\"> <string>att_20160719180542840LnFm</string> <string></string> </void> <void method=\"put\"> <string>att_20160511130025203ssle</string> <string>zdgsszta</string> </void> <void method=\"put\"> <string>att_20160810131359346N0ra</string> <string>szjys</string> </void> <void method=\"put\"> <string>att_20160511130031927equg</string> <string></string> </void> <void method=\"put\"> <string>att_20160422110456036qp7Y</string> <null/> </void> <void method=\"put\"> <string>att_20160422110543585f7iI</string> <string>cpjbxx</string> </void> </object> </java>";
Map<String, String> map = xmlToMap(xml);
System.out.println(map);
}
public static Map<String, String> xmlToMap(String xml) throws Exception {
Map<String, String> map = new HashMap<>();
SAXReader reader = new SAXReader();
Document document = reader.read(new StringReader(xml));
Element root = document.getRootElement().element("object");
for (Element element : root.elements("void")) {
Element keyElement = element.element("string");
Element valueElement = element.elements().get(1);
String key = keyElement.getText();
String value = valueElement == null ? null : valueElement.getText();
map.put(key, value);
}
return map;
}
}
```
该代码使用Dom4j库解析XML,并将其转换为Map对象。输出结果为:
```
{att_20160719180419281NEk9=cx, att_20160511094248589ZKYq=cnmm, att_20161018153614188aTus=zsx;fj, att_20160422110906494XuEf=null, att_20160511130210612UULE=fjyxapp;jjgjapp;wx;jd;dxjg;tb;wsjy, att_20160719180542840LnFm=, att_20160511130025203ssle=zdgsszta, att_20160810131359346N0ra=szjys, att_20160511130031927equg=, att_20160422110456036qp7Y=null, att_20160422110543585f7iI=cpjbxx}
```
HashMap<String, String>类型如何转化成HashMap<String, Object>
可以通过Java的泛型来实现HashMap<String, String>类型转化为HashMap<String, Object>类型,具体方法如下:
```
HashMap<String, String> stringMap = new HashMap<>();
HashMap<String, Object> objectMap = new HashMap<>();
// 将stringMap中的元素转化为objectMap中的元素
for (Map.Entry<String, String> entry : stringMap.entrySet()) {
objectMap.put(entry.getKey(), (Object) entry.getValue());
}
```
这里通过遍历stringMap中的元素,将每个元素的键和值转化为objectMap中的键和值,并将值强制转化为Object类型。这样就完成了HashMap<String, String>类型到HashMap<String, Object>类型的转化。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)