web自动化测试第10步:获取浏览器弹窗alert、自定义弹窗以及其操作

来源:互联网 发布:js调用支付宝扫一扫 编辑:程序博客网 时间:2024/06/11 04:33

在平时的测试中,有时候会遇到弹窗的问题,有的是浏览器弹窗(alert)、有的是自定义弹窗;这节我们主要来讨论一下关于浏览器弹窗和简单的自定义弹窗。

1.关于alert弹窗的方法

switch_to_alert(): 定位到alert弹窗,返回一个弹窗的对象

dismiss(): 对弹窗对象的取消操作(相当于点击弹窗上的取消按钮)

accept():对弹窗对象的确定操作(相当于点击弹窗上的确定按钮)

text:对弹窗对象,获取弹窗内的文本

send_keys(key):对弹窗对象内的输入框输入数据(如果弹窗的格式有输入框的话可以使用)

authenticate(name, pass):对于身份认证弹窗,输入用户名和密码并自动提交(一般可能会用于本地搭建的一些系统)如图:

认证信息


2.alert包的源码展示

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.GET_ALERT_TEXT)["value"]    def dismiss(self):        """        Dismisses the alert available.        """        self.driver.execute(Command.DISMISS_ALERT)    def accept(self):        """        Accepts the alert available.        Usage::        Alert(driver).accept() # Confirm a alert dialog.        """        self.driver.execute(Command.ACCEPT_ALERT)    def send_keys(self, keysToSend):        """        Send Keys to the Alert.        :Args:         - keysToSend: The text to be sent to Alert.        """        if self.driver.w3c:            self.driver.execute(Command.SET_ALERT_VALUE, {'value': keys_to_typing(keysToSend)})        else:            self.driver.execute(Command.SET_ALERT_VALUE, {'text': keysToSend})    def authenticate(self, username, password):        """        Send the username / password to an Authenticated dialog (like with Basic HTTP Auth).        Implicitly 'clicks ok'        Usage::        driver.switch_to.alert.authenticate('cheese', 'secretGouda')        :Args:         -username: string to be set in the username section of the dialog         -password: string to be set in the password section of the dialog        """        self.driver.execute(            Command.SET_ALERT_CREDENTIALS,            {'username': username, 'password': password})        self.accept()

3.实例展示:w3c的alert页面

因为目前使用alert页面的网站实在是不多,所以只好用w3c的页面来示范一下alert包中的方法,具体代码如下

alert-w3c

from selenium import webdriverfrom time import sleepdriver = webdriver.Chrome()driver.get("http://www.w3school.com.cn/tiy/t.asp?f=jseg_prompt")# 通过frame的name值来定位driver.switch_to_frame("i")# 点击按钮触发弹窗ele = driver.find_element_by_css_selector("body > input[type='button']")ele.click()sleep(2)# 定位到到弹窗a = driver.switch_to_alert()print(driver)# 获取弹窗的内容print(a.text)# 触发取消按钮a.dismiss()sleep(2)# 再次点击按钮触发弹窗ele.click()# 在弹窗中的输入框输入数据a.send_keys("许西城")sleep(2)# 触发确认按钮a.accept()

这样就是alert的具体演示了

4.普通的简单弹窗

平时的话,我们一般遇到的都是自定义弹窗,所以说一般不是不用到alert的,但是还是要拿出来说一下的;一般这种自定义弹窗是自定义的div层,然后是隐藏的,所以当你触发了这个弹窗后,它就会显示出来,这时我们通过正常的定位方式是可以正常定位到的。

下面就主要看一下百度的登录弹窗

百度-登录


代码展示:

from selenium import webdriverfrom time import sleep# 打开谷歌浏览器driver = webdriver.Chrome()# 输入网址并访问driver.get("https://www.baidu.com/")# 点击登录按钮driver.find_element_by_css_selector("#u1 > a.lb").click()sleep(2)# 定位登录弹窗的输入框,并输入数据name_box = driver.find_element_by_css_selector("#TANGRAM__PSP_10__userName")name_box.send_keys("name")


阅读全文
0 0
原创粉丝点击