• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1"""
2 * Copyright (c) 2023 iSoftStone Information Technology (Group) Co.,Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14"""
15from selenium import webdriver
16from selenium.webdriver.common.by import By
17import os
18from selenium.webdriver.support.wait import WebDriverWait
19from selenium.webdriver.support import expected_conditions as EC
20import hashlib
21import time
22class WebView:
23    def __init__(self):
24        self.driver = None
25        self.host = "http://localhost:9515"
26        self.option_name = "androidPackage"
27        self.driver_path = 'C:\\Users\\qianwangdq\\Desktop\\chromedriver.exe'
28    def init_webview(self, test_package):
29        try:
30            print("init_webview")
31            # 启动chromedriver.exe驱动
32            self.init_chromedriver()
33            try:
34                options = webdriver.ChromeOptions()
35                options.add_experimental_option(name=self.option_name,
36                                                value=test_package)
37                self.driver = webdriver.Remote(
38                    command_executor=self.host,
39                    options=options)
40                print("Init_webview Success.{}".format(self.driver))
41            except Exception as error:
42                print(
43                    "connect {} failed {},try again!".format(error, self.host))
44                self.driver = webdriver.Remote(
45                    command_executor=self.host,
46                    options=options)
47        except Exception as error:
48            raise TypeError("Init_webview failed") from error
49
50        return self.driver
51
52    def init_chromedriver(self):
53        chrome_tool_path = self.driver_path
54        if not os.path.isfile(chrome_tool_path):
55            raise FileExistsError("{} not exists!".format(chrome_tool_path))
56        os.startfile(chrome_tool_path)
57
58    def click_by_id(self, value):
59
60        return self.click(key=By.ID, value=value)
61
62    def click_by_name(self, value):
63
64        return self.click(key=By.NAME, value=value)
65
66    def click_by_calss_name(self, value):
67
68        return self.click(key=By.CLASS_NAME, value=value)
69
70    def click_by_tag_name(self, value):
71
72        return self.click(key=By.TAG_NAME, value=value)
73
74    def click_by_link_text(self, value):
75
76        return self.click(key=By.LINK_TEXT, value=value)
77
78    def click_by_partial_link_text(self, value):
79
80        return self.click(key=By.PARTIAL_LINK_TEXT, value=value)
81
82    def click_by_xpath(self, value):
83
84        return self.click(key=By.XPATH, value=value)
85
86    def click_by_css_selector(self, value):
87
88        return self.click(key=By.CSS_SELECTOR, value=value)
89
90    def get(self, url):
91
92        return self.driver.get(url=url)
93
94    def click(self, key, value):
95        driver = self.driver.find_element(by=key, value=value)
96        return driver.click()
97
98    def find_element(self, key, value):
99        return self.driver.find_element(by=key, value=value)
100
101    def send_keys(self, key, value, text):
102
103        return self.driver.find_element(by=key, value=value).send_keys(text)
104
105    def input_text_by_id(self, value, text):
106
107        return self.driver.find_element(by=By.ID, value=value).send_keys(text)
108
109    def input_text_by_name(self, name, text):
110
111        return self.driver.find_element(by=By.NAME, value=name).send_keys(text)
112
113    def input_text_by_xpath(self, xpath, text):
114
115        return self.driver.find_element(by=By.XPATH, value=xpath).send_keys(text)
116
117    def clear(self, key, value):
118        return self.driver.find_element(by=key, value=value).clear()
119
120    def clear_text_by_id(self, value):
121        return self.driver.find_element(by=By.ID, value=value).clear()
122
123    def clear_text_by_name(self, name):
124        return self.driver.find_element(by=By.NAME, value=name).clear()
125
126    def clear_text_by_xpath(self, xpath):
127        return self.driver.find_element(by=By.XPATH, value=xpath).clear()
128
129    def close(self):
130        return self.driver.close()
131
132    def init_runner(self, case_name):
133        self.driver.get('http://web-platform.test:8000/tools/runner/index.html')
134        print('----------' + case_name + 'start' + '----------')
135
136    @staticmethod
137    def runner_end(case_name):
138        print('----------' + case_name + 'end' + '----------')
139
140    def click_js(self):
141        ele = self.driver.find_element(By.ID, 'th')  # 去掉自动化复选框
142        ele.click()
143
144    def click_manual(self):
145        ele1 = self.driver.find_element(By.ID, 'man')  # 去掉手工复选框
146        ele1.click()
147
148    def click_reft(self):
149        ele2 = self.driver.find_element(By.ID, 'ref')  # 去掉refttest
150        ele2.click()
151
152    def send_path(self, file_path):
153        input_path = self.driver.find_element(By.ID, 'path')  # 找到路径输入框
154        input_path.send_keys(file_path)  # 发送路径
155
156    def start_test(self):
157        WebDriverWait(self.driver, 10).until(EC.visibility_of(self.driver.find_element(By.ID, 'testcount')))
158        # 显示等待至testcount出现
159        ele2 = self.driver.find_element(By.XPATH, '/html/body/div/div[2]/div/form/div[6]/div/button[1]')  # 点击开始
160        time.sleep(5)
161        ele3 = self.driver.find_element(By.XPATH, '//*[@id="options"]/div[4]')  # 点击
162        ele3.click()
163        time.sleep(3)
164        self.driver.execute_script("arguments[0].click();",  ele2)
165
166    def click_show_test(self):
167        handles = self.driver.window_handles  # 获取所有窗口的句柄
168        print("全部窗口的句柄", handles)
169        self.driver.switch_to.window(handles[0])  # 切句柄回原窗口
170        WebDriverWait(self.driver, 3000).until(EC.visibility_of(self.driver.find_element(By.XPATH,
171        '//*[@id="manualUI"]/div/div[2]/p[2]/button[1]')))  # 显示等待至showttest出现
172        showTest = self.driver.find_element(By.XPATH, '//*[@id="manualUI"]/div/div[2]/p[2]/button[1]')  # 找到show test
173        # showTest.click()  # 点击show Test
174        self.driver.execute_script("arguments[0].click();", showTest)
175
176    def test_screenshot(self, ele_path, file_name):
177        handles = self.driver.window_handles  # 获取所有窗口的句柄
178        print("全部窗口的句柄", handles)
179        self.driver.switch_to.window(handles[-1])  # 切句柄到新窗口
180        WebDriverWait(self.driver, 3000).until(
181            EC.visibility_of(self.driver.find_element(By.XPATH, ele_path)))  # 显示等待方块出现  '//*[@id="fileDisplay"]'
182        self.driver.get_screenshot_as_file("D:\\share\\" + file_name + ".png")  # 截图
183
184    def test_implicit_expression_screenshot(self, ele_path, file_name):
185        handles = self.driver.window_handles  # 获取所有窗口的句柄
186        print("全部窗口的句柄", handles)
187        self.driver.switch_to.window(handles[-1])  # 切句柄到新窗口
188        self.driver.implicitly_wait(20)  # 设置隐式等待
189        WebDriverWait(self.driver, 3000).until(
190            EC.visibility_of(self.driver.find_element(By.XPATH, ele_path)))  # 显示等待方块出现
191        self.driver.get_screenshot_as_file("D:\\share\\" + file_name + ".png")  # 截图
192
193    def test_file_screenshot(self,file_name):
194        handles = self.driver.window_handles  # 获取所有窗口的句柄
195        print("全部窗口的句柄", handles)
196        self.driver.switch_to.window(handles[-1])  # 切句柄到新窗口
197        self.driver.implicitly_wait(20)  # 设置隐式等待
198        self.driver.get_screenshot_as_file("D:\\share\\" + file_name + ".png")  # 截图
199
200    def implicit_waiting(self, file_name):
201        self.driver.implicitly_wait(20)  # 设置隐式等待
202        self.driver.maximize_window()  # 最大化浏览器
203        self.driver.get_screenshot_as_file("D:\\share\\" + file_name + ".png")  # 截图
204
205    def click_show_ref(self):
206        handles = self.driver.window_handles  # 获取所有窗口的句柄
207        print("全部窗口的句柄", handles)
208        self.driver.switch_to.window(handles[0])  # 切句柄回原窗口
209        showReference = self.driver.find_element(By.XPATH,'//*[@id="manualUI"]/div/div[2]/p[2]/button[2]')  # 找到 show reference
210        # showReference.click()  # 点击show reference
211        self.driver.execute_script("arguments[0].click();", showReference)
212
213    def ref_screenshot(self, ele_path, file_name):
214        handles = self.driver.window_handles  # 获取所有窗口的句柄
215        self.driver.switch_to.window(handles[-1])  # 切句柄到新窗口
216        WebDriverWait(self.driver, 3000).until(
217            EC.visibility_of(self.driver.find_element(By.XPATH, ele_path)))  # 显示等待方块出现
218        self.driver.get_screenshot_as_file("D:\\share\\" + file_name + ".png")  # 截图
219
220    def ref_implicit_expression_screenshot(self, ele_path, file_name):
221        handles = self.driver.window_handles  # 获取所有窗口的句柄
222        self.driver.switch_to.window(handles[-1])  # 切句柄到新窗口
223        self.driver.implicitly_wait(20)  # 设置隐式等待
224        self.driver.maximize_window()  # 最大化浏览器
225        WebDriverWait(self.driver, 3000).until(
226            EC.visibility_of(self.driver.find_element(By.XPATH, ele_path)))  # 显示等待方块出现
227        self.driver.get_screenshot_as_file("D:\\share\\" + file_name + ".png")  # 截图
228
229    def ref_file_screenshot(self, file_name):
230        handles = self.driver.window_handles  # 获取所有窗口的句柄
231        print("全部窗口的句柄", handles)
232        self.driver.switch_to.window(handles[-1])  # 切句柄到新窗口
233        self.driver.implicitly_wait(20)  # 设置隐式等待
234        self.driver.get_screenshot_as_file("D:\\share\\" + file_name + ".png")  # 截图
235
236    def test_assert(self, file_name1, file_name2):
237        file1 = open("D:\\share\\" + file_name1 + ".png", "rb")
238        file2 = open("D:\\share\\" + file_name2 + ".png", "rb")
239        md = hashlib.md5()
240        md.update(file1.read())
241        res1 = md.hexdigest()
242        md = hashlib.md5()
243        md.update(file2.read())
244        res2 = md.hexdigest()
245        self.driver.close()
246        handles = self.driver.window_handles  # 获取所有窗口的句柄
247        print("全部窗口的句柄", handles)
248        self.driver.switch_to.window(handles[0])  # 切句柄回原窗口
249        time.sleep(2)
250        zoom_out = "document.body.style.zoom='0.5'"
251        self.driver.execute_script(zoom_out)
252        time.sleep(5)
253        if res1 == res2:
254            clickPass = self.driver.find_element(By.XPATH, '//*[@id="manualUI"]/div/div[3]/button[1]')  # 点击pass
255            # clickPass.click()
256            self.driver.execute_script("arguments[0].click();", clickPass)
257        else:
258            clickFalse = self.driver.find_element(By.XPATH, '//*[@id="manualUI"]/div/div[3]/button[3]')  # 点击fail
259            # clickFalse.click()
260            self.driver.execute_script("arguments[0].click();", clickFalse)
261
262