线性模型
from selenium import webdriver
from time import sleep
from selenium.webdriver.common.action_chains import ActionChains
driver = webdriver.Chrome()
driver.get("http://192.168.20.12:8080/rdms/login.jsp")
driver.maximize_window()
driver.implicitly_wait(10)
driver.find_element_by_name('username').clear()
driver.find_element_by_name("username").send_keys("用户名")
sleep(2)
driver.find_element_by_name('password').clear()
driver.find_element_by_name('password').send_keys("密码")
sleep(2)
driver.find_element_by_class_name('loginButton').click()
sleep(2)
# 鼠标悬停
above = driver.find_element_by_class_name("topframe-name")
ActionChains(driver).move_to_element(above).perform()
sleep(2)
driver.find_element_by_link_text('退出').click()
sleep(2)
driver.switch_to_alert().accept()
sleep(2)
driver.quit()
模块化驱动测试
from selenium import webdriver
from time import sleep
from selenium.webdriver.common.action_chains import ActionChains
class Login_mk():
def user_login(self,driver):
driver.find_element_by_name('username').clear()
driver.find_element_by_name("username").send_keys("用户名")
sleep(2)
driver.find_element_by_name('password').clear()
driver.find_element_by_name('password').send_keys("密码")
sleep(2)
driver.find_element_by_class_name('loginButton').click()
sleep(2)
def user_logout(self,driver):
# 鼠标悬停
above = driver.find_element_by_class_name("topframe-name")
ActionChains(driver).move_to_element(above).perform()
sleep(2)
driver.find_element_by_link_text('退出').click()
sleep(2)
driver.switch_to_alert().accept()
sleep(2)
数据驱动测试
from selenium import webdriver
from time import sleep
from selenium.webdriver.common.action_chains import ActionChains
class Login_sj():
def user_login(self,driver,username,password):
driver.find_element_by_name('username').clear()
driver.find_element_by_name("username").send_keys(username)
sleep(2)
driver.find_element_by_name('password').clear()
driver.find_element_by_name('password').send_keys(password)
sleep(2)
driver.find_element_by_class_name('loginButton').click()
sleep(2)
def user_logout(self,driver):
# 鼠标悬停
above = driver.find_element_by_class_name("topframe-name")
ActionChains(driver).move_to_element(above).perform()
sleep(2)
driver.find_element_by_link_text('退出').click()
sleep(2)
driver.switch_to_alert().accept()
sleep(2)
调用
from Moxing_mokuai import Login_mk
from Moxing_shuju import Login_sj
from selenium import webdriver
from time import sleep
driver = webdriver.Chrome()
driver.get("http://192.168.20.12:8080/rdms/login.jsp")
driver.maximize_window()
driver.implicitly_wait(10)
# 模块化驱动调用
#Login_mk().user_login(driver)
#Login_mk().user_logout(driver)
#driver.quit()
# 数据驱动调用
Login_sj().user_login(driver,"用户名","密码")
Login_sj().user_logout(driver)
driver.quit()
关键字驱动测试
如IDE、Framework等
# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoAlertPresentException
import unittest, time, re
class Test(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Firefox()
self.driver.implicitly_wait(30)
self.base_url = "http://192.168.20.12:8080/"
self.verificationErrors = []
self.accept_next_alert = True
def test_(self):
driver = self.driver
driver.get(self.base_url + "/rdms/login.jsp")
driver.find_element_by_css_selector("label.placeholder").click()
driver.find_element_by_name("username").clear()
driver.find_element_by_name("username").send_keys(u"用户名")
driver.find_element_by_xpath("//p[3]").click()
driver.find_element_by_xpath("//p[2]/label").click()
driver.find_element_by_name("password").clear()
driver.find_element_by_name("password").send_keys("密码")
driver.find_element_by_css_selector("input.loginButton").click()
driver.find_element_by_css_selector(u"a[title="退出系统"] > span.paddingLeft").click()
def is_element_present(self, how, what):
try: self.driver.find_element(by=how, value=what)
except NoSuchElementException as e: return False
return True
def is_alert_present(self):
try: self.driver.switch_to_alert()
except NoAlertPresentException as e: return False
return True
def close_alert_and_get_its_text(self):
try:
alert = self.driver.switch_to_alert()
alert_text = alert.text
if self.accept_next_alert:
alert.accept()
else:
alert.dismiss()
return alert_text
finally: self.accept_next_alert = True
def tearDown(self):
self.driver.quit()
self.assertEqual([], self.verificationErrors)
if __name__ == "__main__":
unittest.main()