UI的自动化测试之Alert弹出框方法
在UI的自动化测试实战中,针对弹出框的处理,主要使用的是Alert的类,在JavaScript的技术体系中,针对弹出框的部分,主要涉及到Alert警告框,Confirm确认框,Prompt消息框。下来主要详细的说下Alert里面每个方法的具体使用,具体源码如下:点击查看代码

# Licensed to the Software Freedom Conservancy (SFC) under one # or more contributor license agreements. See the NOTICE file # distributed with this work for additional information # regarding copyright ownership. The SFC licenses this file # to you under the Apache License, Version 2.0 (the # "License"); you may not use this file except in compliance # with the License. You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, # software distributed under the License is distributed on an # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY # KIND, either express or implied. See the License for the # specific language governing permissions and limitations # under the License. """ The Alert implementation. """ from selenium.webdriver.common.utils import keys_to_typing from selenium.webdriver.remote.command import Command class Alert(object): """ Allows to work with alerts. Use this class to interact with alert prompts. It contains methods for dismissing, accepting, inputting, and getting text from alert prompts. Accepting / Dismissing alert prompts:: Alert(driver).accept() Alert(driver).dismiss() Inputting a value into an alert prompt: name_prompt = Alert(driver) name_prompt.send_keys("Willian Shakesphere") name_prompt.accept() Reading a the text of a prompt for verification: alert_text = Alert(driver).text self.assertEqual("Do you wish to quit?", alert_text) """ def __init__(self, driver): """ Creates a new Alert. :Args: - driver: The WebDriver instance which performs user actions. """ self.driver = driver @property def text(self): """ Gets the text of the Alert. """ return self.driver.execute(Command.W3C_GET_ALERT_TEXT)["value"] def dismiss(self): """ Dismisses the alert available. """ self.driver.execute(Command.W3C_DISMISS_ALERT) def accept(self): """ Accepts the alert available. Usage:: Alert(driver).accept() # Confirm a alert dialog. """ self.driver.execute(Command.W3C_ACCEPT_ALERT) def send_keys(self, keysToSend): """ Send Keys to the Alert. :Args: - keysToSend: The text to be sent to Alert. """ self.driver.execute(Command.W3C_SET_ALERT_VALUE, {'value': keys_to_typing(keysToSend), 'text': keysToSend})
在Alert的类里面,涉及到的方法以及方法的作用主要汇总为如下:
- text:获取弹出框的文本信息
- accept是接受Confirm弹出框
- dismiss是拒绝接受Confirm弹出框
- send_keys是在Prompt消息对话框里面输入想要输入的内容
获取警告框的文本信息
driver.switch_to.alert.text是一个获取弹出框文本内容的语法,当初框中往往有提示内容,可以通过这个来获取提示的内容进行判断分析,
一般针对于confirm()弹出框获取内容使用
弹出框:确定消息
driver.switch_to.alert.accept()是针对于当打开弹框中有两个按钮,一个是确认按钮 一个是取消按钮,
accept()就是使得其点击确认的操作 dismiss()就是使得其点击去取消的操作
消息对话框
driver.switch_to.alert.send_keys(value) 是一个向弹出框中输入内容的方法 其中value是输入的内容,
针对于prompt()弹出框中要输入内容,那么就要用到该方法来模拟输入数据内容
下面针对这部分详细的进行案例代码的演示和说明。
一、警告框
警告框的交互主要为比如在百度搜索设置后,点击保存,就会弹出警告框的交互,具体UI的交互如下:
如上是弹出警告框的信息,下面是通过详细的代码来演示这部分的交互过程,具体代码如下:
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.common.action_chains import ActionChains import time as t driver=webdriver.Chrome() driver.maximize_window() driver.get('http://www.baidu.com') driver.implicitly_wait(30) #获取到百度设置的元素属性 locator=driver.find_element_by_xpath('//*[@id="s-usersetting-top"]') action=ActionChains(driver=driver) action.move_to_element(locator).perform() t.sleep(5) #点击搜索设置 driver.find_element_by_xpath('//*[@id="s-user-setting-menu"]/div/a[1]').click() t.sleep(5) #点击保存设置 driver.find_element_by_xpath('//*[@id="se-setting-7"]/a[2]').click() t.sleep(5) #获取到弹出框的文本信息 print('警告框的文本信息为:',driver.switch_to.alert.text) driver.quit()
结果:警告框的文本信息为: 已经记录下您的使用偏好
二、确认框的处理
针对确认框的交互,一般都是会弹出确定或者是取消的交互,如果是点击确定按钮,调用的的方法是accept,如果是点击取消按钮,调用
的方法是dismiss的方法。如下显示确认框的交互信息,具体如下:
上图是VUE的组件的交互信息。下面通过HTML的代码来编写确认弹出框的源生代码,具体如下:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <script type="text/javascript"> function disp_confirm() { var r=confirm("请您点击按钮!") if(r==true) { document.write("您点击了确认按钮") } else { document.write("您点击了取消按钮") } } </script> </head> <body> <center> <input type="button" onclick="disp_confirm()" value="请点击"> </center> </body> </html>
下面使用UI自动化测试的代码来实现这部分,具体实现的代码为:
from selenium import webdriver import time as t driver=webdriver.Chrome() driver.maximize_window() driver.get('file:///Applications/code/Yun/testDevelop/ui%E6%B5%8B%E8%AF%95/confirm.html') driver.implicitly_wait(30) #点击按钮 driver.find_element_by_xpath('/html/body/center/input').click() #点击确定按钮 t.sleep(5) driver.switch_to.alert.accept() t.sleep(5) #刷新浏览器 driver.refresh() t.sleep(5) #点击按钮 driver.find_element_by_xpath('/html/body/center/input').click() #点击确定按钮 t.sleep(5) driver.switch_to.alert.dismiss() t.sleep(5) driver.quit()
三、消息对话框
消息消息对话框主要显示的是与用户交互的信息,那么调用的方法就是send_keys(),它的交互具体如下:
下面具体来看这部分的HTML的交互代码,具体如下:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <script type="text/javascript"> function disp_prompt() { var name=prompt("请输入您的姓名:","") if(name!=null && name!="") { document.write("Hello "+name+"!") } } </script> </head> <body> <center> <input type="button" onclick="disp_prompt()" value="请点击我!"> </center> </body> </html>
下面使用自动化测试的测试代码来具体查看这个交互过程,通过iOS系统运行具体如下:
from selenium import webdriver import time as t driver=webdriver.Chrome() driver.maximize_window() driver.get('file:///Applications/code/Yun/testDevelop/ui%E6%B5%8B%E8%AF%95/prompt.html') t.sleep(3) driver.find_element_by_xpath('/html/body/center/input').click() t.sleep(3) driver.switch_to.alert.send_keys("测试开发") t.sleep(3) driver.switch_to.alert.accept() t.sleep(3) driver.quit()
消息对话框实战:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <script type="text/javascript"> function disp_prompt() { var name=prompt("请输入您的姓名:","") if(name!=null && name!="") { document.write("Hello "+name+"!") } } </script> </head> <body> <center> <input type="button" onclick="disp_prompt()" value="请点击我!"> </center> </body> </html>
driver=webdriver.Chrome() driver.maximize_window() driver.get('file:///C:/Users/29660/PycharmProjects/pythonProject/UI%E8%87%AA%E5%8A%A8%E5%8C%96%E6%B5%8B%E8%AF%95/%E6%B6%88%E6%81%AF%E5%AF%B9%E8%AF%9D%E6%A1%86(1).html') driver.find_element(By.XPATH,'/html/body/center/input').click() t.sleep(3) driver.switch_to.alert.send_keys('学习测试') t.sleep(3) driver.switch_to.alert.accept() t.sleep(3) # 页面刷新 driver.refresh() t.sleep(3) driver.find_element(By.XPATH,'/html/body/center/input').click() t.sleep(3) driver.switch_to.alert.send_keys('学习测试') t.sleep(2) driver.switch_to.alert.dismiss() t.sleep(3) driver.quit()