|
| 1 | +/* |
| 2 | + * Copyright 2002-2007 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.suren.autotest.web.framework.util; |
| 18 | + |
| 19 | +import java.lang.reflect.Field; |
| 20 | +import java.util.ArrayList; |
| 21 | +import java.util.Arrays; |
| 22 | +import java.util.Comparator; |
| 23 | +import java.util.List; |
| 24 | + |
| 25 | +import org.suren.autotest.web.framework.core.ui.Button; |
| 26 | +import org.suren.autotest.web.framework.core.ui.Element; |
| 27 | +import org.suren.autotest.web.framework.core.ui.Text; |
| 28 | +import org.suren.autotest.web.framework.page.Page; |
| 29 | + |
| 30 | +/** |
| 31 | + * 元素操作工具类,可以批量地操作元素 |
| 32 | + * @author suren |
| 33 | + * @date 2017年5月14日 下午9:10:29 |
| 34 | + */ |
| 35 | +public class ElementUtil |
| 36 | +{ |
| 37 | + /** |
| 38 | + * 批量点击按钮 |
| 39 | + * @param buttons |
| 40 | + */ |
| 41 | + public static void click(Button ...buttons) |
| 42 | + { |
| 43 | + if(buttons == null) |
| 44 | + { |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + List<Button> buttonList = Arrays.asList(buttons); |
| 49 | + buttonList.stream().sorted(getComparator()).forEach(but -> { |
| 50 | + but.click(); |
| 51 | + }); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * 批量填充文本 |
| 56 | + * @param texts |
| 57 | + */ |
| 58 | + public static void fillValue(Text ...texts) |
| 59 | + { |
| 60 | + if(texts == null) |
| 61 | + { |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + Arrays.asList(texts).stream().sorted(getComparator()).forEach(text -> { |
| 66 | + text.fillValue(); |
| 67 | + }); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * 批量对页面中的按钮进行点击操作 |
| 72 | + * @param pages |
| 73 | + */ |
| 74 | + public static void click(Page ...pages) |
| 75 | + { |
| 76 | + if(pages == null) |
| 77 | + { |
| 78 | + return; |
| 79 | + } |
| 80 | + |
| 81 | + for(Page page : pages) |
| 82 | + { |
| 83 | + List<Button> buttonList = new ArrayList<Button>(); |
| 84 | + |
| 85 | + Class<? extends Page> pageCls = page.getClass(); |
| 86 | + for(Field field : pageCls.getDeclaredFields()) |
| 87 | + { |
| 88 | + if(field.getType().equals(Button.class)) |
| 89 | + { |
| 90 | + field.setAccessible(true); |
| 91 | + try |
| 92 | + { |
| 93 | + buttonList.add((Button) field.get(page)); |
| 94 | + } |
| 95 | + catch (IllegalArgumentException | IllegalAccessException e) |
| 96 | + { |
| 97 | + e.printStackTrace(); |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + if(buttonList.size() > 0) |
| 103 | + { |
| 104 | + click(buttonList.toArray(new Button[]{})); |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * 批量对页面中的文本进行填入值操作 |
| 111 | + * @param pages |
| 112 | + */ |
| 113 | + public static void fillValue(Page ...pages) |
| 114 | + { |
| 115 | + if(pages == null) |
| 116 | + { |
| 117 | + return; |
| 118 | + } |
| 119 | + |
| 120 | + for(Page page : pages) |
| 121 | + { |
| 122 | + List<Text> textList = new ArrayList<Text>(); |
| 123 | + |
| 124 | + Class<? extends Page> pageCls = page.getClass(); |
| 125 | + for(Field field : pageCls.getDeclaredFields()) |
| 126 | + { |
| 127 | + if(field.getType().equals(Text.class)) |
| 128 | + { |
| 129 | + field.setAccessible(true); |
| 130 | + try |
| 131 | + { |
| 132 | + textList.add((Text) field.get(page)); |
| 133 | + } |
| 134 | + catch (IllegalArgumentException | IllegalAccessException e) |
| 135 | + { |
| 136 | + e.printStackTrace(); |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + if(textList.size() > 0) |
| 142 | + { |
| 143 | + fillValue(textList.toArray(new Text[]{})); |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + } |
| 148 | + |
| 149 | + /** |
| 150 | + * 根据元素的序号进行排序 |
| 151 | + * @return 元素排序的方法 |
| 152 | + */ |
| 153 | + public static Comparator<Element> getComparator() |
| 154 | + { |
| 155 | + return (e1, e2) -> { |
| 156 | + return e1.getIndex() - e2.getIndex(); |
| 157 | + }; |
| 158 | + } |
| 159 | +} |
0 commit comments