【多窗口,弹出框】UI自动化测试

 目录

一、弹出框实战

二、Sina实战

三、QQ邮箱错误信息的验证

四、新浪邮箱错误信息的验证

五、忘记密码的验证 多窗口


一、弹出框实战

1、在UI自动化测试中经常会遇到Alert弹出框的场景。Alert类是对话框的处理,主要是对alert警告框。confirm确认框,promp消息对话框。

  text():获取alert的文本

  dismiss ():点击取消

  accept():接受

  send-keys():输入

from selenium import webdriver
from selenium.webdriver.common.alert import Alert
import time as t
"""获取alert警告框的文本信息"""
driver=webdriver.Chrome()
driver.maximize_window()
driver.get('file:///D:/code/app/01.html')
print('获取alert的文本信息:',driver.switch_to.alert.text)
t.sleep(3)
driver.switch_to.alert.accept()
t.sleep(3)
driver.quit()

获取alert的文本信息: 无涯课堂为您服务!

from selenium import webdriver
from selenium.webdriver.common.alert import Alert
import time as t

"""确认框的处理"""
driver=webdriver.Chrome()
driver.maximize_window()
driver.get('file:///D:/code/app/confi.html')
driver.find_element_by_xpath('/html/body/input').click()
driver.switch_to.alert.accept()
t.sleep(5)
# 刷新页面
driver.refresh()
t.sleep(3)
driver.find_element_by_xpath('/html/body/input').click()
driver.switch_to.alert.dismiss()
t.sleep(3)
driver.quit()

from selenium import webdriver
from selenium.webdriver.common.alert import Alert
import time as t

"""消息对话框输入信息"""
driver=webdriver.Chrome()
driver.maximize_window()
driver.get('file:///D:/code/app/prompt.html')
driver.find_element_by_xpath('/html/body/input').click()
driver.switch_to.alert.send_keys('无涯课堂为您服务!')
t.sleep(5)
# 点击取消
driver.switch_to.alert.dismiss()
t.sleep(5)
driver.find_element_by_xpath('/html/body/input').click()
# 再一次输入
driver.switch_to.alert.send_keys('无涯课堂为您服务!')
t.sleep(5)
driver.switch_to.alert.accept()
t.sleep(5)
driver.quit()

二、Sina实战

from selenium import webdriver
import time as t

"""sina实战"""
driver=webdriver.Chrome()
driver.maximize_window()
driver.get('https://mail.sina.com.cn/')
# 输入用户名
driver.find_element_by_id('freename').send_keys('[email protected]')
t.sleep(3)
# 输入密码
driver.find_element_by_xpath('//*[@id="freepassword"]').send_keys('admin123')
t.sleep(3)
# 点击登录按钮
driver.find_element_by_xpath('/html/body/div[3]/div/div[2]/div/div/div[4]/div[1]/div[1]/div[7]/div[1]/a[1]').click()
t.sleep(3)
# 获取nick
nick=driver.find_element_by_xpath('//*[@id="navParent"]/li[1]/a/span/em').text
print('nick信息:',nick)
assert nick=="邮箱首页"
# 点击写信按钮
driver.find_element_by_xpath('//*[@id="coreBtn"]/ul/li[1]/a').click()
t.sleep(3)
# 输入收件人 断言
inputText=driver.find_element_by_xpath('//*[@id="tr_to"]/td/ul/li/input')
inputText.send_keys('[email protected]')
assert inputText.get_attribute('value')=='[email protected]'

driver.switch_to.frame
t.sleep(3)
driver.quit()

三、QQ邮箱错误信息的验证

from selenium import webdriver
import time as t

driver=webdriver.Chrome()
driver.maximize_window()
driver.get('https://mail.qq.com/')
driver.switch_to.frame('login_frame')
driver.find_element_by_id('u').send_keys('734398143')
t.sleep(3)
driver.find_element_by_xpath('//*[@id="p"]').send_keys('asdf')
driver.find_element_by_xpath('//*[@id="login_button"]').click()
t.sleep(3)
errorText=driver.find_element_by_xpath('//*[@id="err_m"]')
assert errorText.text=='你输入的帐号或密码不正确,请重新输入。'
driver.quit()

四、新浪邮箱错误信息的验证

1、账户密码都为空

from selenium import webdriver
import time as t


driver=webdriver.Chrome()
driver.maximize_window()
driver.get('https://mail.sina.com.cn/#')
"""错误信息验证 用户名和密码都为空"""
driver.find_element_by_xpath('/html/body/div[3]/div/div[2]/div/div/div[4]/div[1]/div[1]/div[7]/div[1]/a[1]').click()
errorText=driver.find_element_by_xpath('/html/body/div[3]/div/div[2]/div/div/div[4]/div[1]/div[1]/div[1]/span[1]')
assert errorText.text=='请输入邮箱名'
t.sleep(3)
driver.quit()

2、密码为空

from selenium import webdriver
import time as t

driver=webdriver.Chrome()
driver.maximize_window()
driver.get('https://mail.sina.com.cn/#')
"""密码为空"""
driver.find_element_by_id('freename').send_keys('[email protected]')
t.sleep(3)
driver.find_element_by_xpath('/html/body/div[3]/div/div[2]/div/div/div[4]/div[1]/div[1]/div[7]/div[1]/a[1]').click()
errorText=driver.find_element_by_xpath('/html/body/div[3]/div/div[2]/div/div/div[4]/div[1]/div[1]/div[1]/span[2]')
assert errorText.text=='请输入密码'
t.sleep(3)
driver.quit()

3、账户为空

from selenium import webdriver
import time as t

driver=webdriver.Chrome()
driver.maximize_window()
driver.get('https://mail.sina.com.cn/#')
"""邮箱名为空"""
driver.find_element_by_xpath('//*[@id="freepassword"]').send_keys('admin123')
t.sleep(3)
driver.find_element_by_xpath('/html/body/div[3]/div/div[2]/div/div/div[4]/div[1]/div[1]/div[7]/div[1]/a[1]').click()
errorText=driver.find_element_by_xpath('/html/body/div[3]/div/div[2]/div/div/div[4]/div[1]/div[1]/div[1]/span[1]')
assert errorText.text=='请输入邮箱名'
t.sleep(3)
driver.quit()

五、忘记密码的验证 多窗口

from selenium import webdriver
import time as t

"""测试忘记密码"""
driver=webdriver.Chrome()
driver.maximize_window()
driver.get('https://mail.sina.com.cn/#')
nowHandler=driver.current_window_handle
driver.find_element_by_link_text('忘记密码?').click()
allHandler=driver.window_handles
for handler in allHandler:
    if handler!=nowHandler:
          driver.switch_to.window(handler)
          url=driver.current_url
          print(url.endswith('entry=freemail'))
          assert url.endswith('entry=freemail')==True
          driver.close()
driver.switch_to.window(nowHandler)
t.sleep(3)
driver.quit()

你可能感兴趣的:(chrome,python,爬虫)