在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消息对话框里面输入想要输入的内容
下面针对这部分详细的进行案例代码的演示和说明。
一、警告框
警告框的交互主要为比如在百度搜索设置后,点击保存,就会弹出警告框的交互,具体UI的交互如下:
那么这部分就是弹出警告框的信息,下面通过详细的代码来演示这部分的交互过程,具体代码如下:
#! /usr/bin/env python
# -*- coding:utf-8 -*-
#author:无涯
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()
如上的代码执行后,调用text的方法,就能够获取到该警告框的文本信息,输出结果信息为:
警告框的文本信息为: 已经记录下您的使用偏好
二、确认框的处理
针对确认框的交互,一般都是会弹出确定或者是取消的交互,如果是确定,调用的的方法是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自动化测试的代码来实现这部分,具体实现的代码为:
#! /usr/bin/env python
# -*- coding:utf-8 -*-
#author:无涯
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>
下面使用自动化测试的测试代码来具体查看这个交互过程,具体如下:
#! /usr/bin/env python
# -*- coding:utf-8 -*-
#author:无涯
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()
如上是UI自动化测试中Alert类中方法的具体应用和案例实战。感谢您的阅读和关注,后续会持续更新!