python+selenium测试用例参考

#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.webdriver.common.action_chains import ActionChains
import unittest,time,re


class Goodboy(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Firefox()
        self.base_url = "http://www.baidu.com"
        self.verificationErrors = []
        self.accept_next_alert = True


    def is_alert_present(self):
        try: self.driver.switch_to_alert()
        except NoAlertPresentException, 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 login(self):
        driver = self.driver
        driver.get(self.base_url)
        driver.find_element_by_xpath('//*[@id="accountInfo"]/li/a[1]').click()
        driver.find_element_by_id('username').click()
        driver.find_element_by_id('username').send_keys('p_goodbaby1@163.com')
        time.sleep(2)
        driver.find_element_by_id('password').click()
        driver.find_element_by_id('password').send_keys('123456')
        driver.find_element_by_xpath('//*[@id="loginBtn"]').click()
        time.sleep(3)
        self.username = driver.find_element_by_xpath('//*[@id="accountInfo"]/li[1]/dl/dt/a[1]').text
        if(self.username == u'百度用户名'):
            print 'user matched!'
        else:
            raise NameError('user is not matched!')


    def logout(self):
        driver = self.driver
        t = driver.find_element_by_xpath('//*[@id="accountInfo"]/li[1]/dl/dt/a[1]')
        ActionChains(driver).move_to_element(t).perform()
        time.sleep(3)
        driver.find_element_by_xpath('//*[@id="accountInfo"]/li[1]/dl/dd/div/p[6]/a[1]/b').click()
        time.sleep(2)


    def test_login_logout(self):
        self.login()
        self.logout()       


    def tearDown(self):
        self.driver.quit()
        self.assertEqual([], self.verificationErrors)




if __name__ == "__main__":
    unittest.main()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值