python+selenium测试用例参考

来源:互联网 发布:淘宝客新手怎么推广 编辑:程序博客网 时间:2024/04/28 18:21
#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
原创粉丝点击